Data Analyst Course · SQL metrics lesson
SQL for Data Analyst Interviews: Business Metrics
Compute monthly active customers from a small event table and learn grain, distinct counts and funnel checks.
Learn the concept, work through an example, and check your answer.
Core idea
A query is only correct when its row unit matches the metric.
Group by month
Reconcile output
The direct answer
Write SQL that matches the business metric’s grain.
Analyst SQL interviews often ask for active users, funnels, cohorts or retention. The syntax matters, but the metric definition matters first. This lesson counts distinct monthly active customers from a synthetic event table and checks the output by hand.
The example query uses SQLite-compatible date strings for a runnable fixture. Adapt month extraction to the database dialect and timezone in the actual task. The PostgreSQL aggregate documentation is a primary source for aggregate behavior. Course pricing follows this introduction.
Course access
Choose your next step in Data Analyst preparation.
Review the current course options and access details here.
Heycademy Courses · Coming Soon
Master Data Analyst with Heycademy
We’re preparing an interactive learning experience with practical exercises, skill assessments and personalized learning to help you build confidence and master new skills.
We’re putting the finishing touches on this course. Please check back for updates.
Define monthly activity
One customer can have many events.
| event_id | customer_id | event_date | event_type |
|---|---|---|---|
| E1 | C1 | 2026-01-02 | login |
| E2 | C1 | 2026-01-05 | login |
| E3 | C2 | 2026-01-09 | purchase |
| E4 | C1 | 2026-02-01 | login |
| E5 | C3 | 2026-02-03 | login |
Define active as a customer with at least one qualifying login or purchase event in a calendar month. The output has one row per month. January has C1 and C2, so 2 active customers despite three events. February has C1 and C3, so 2. If “active” only means purchase, the output changes; clarify that before querying.
Runnable SQL
Count distinct customers within each month.
SELECT substr(event_date, 1, 7) AS month,
COUNT(DISTINCT customer_id) AS active_customers
FROM events
WHERE event_type IN ('login', 'purchase')
GROUP BY substr(event_date, 1, 7)
ORDER BY month;| month | active_customers |
|---|---|
| 2026-01 | 2 |
| 2026-02 | 2 |
The query counts IDs, not event rows. Date strings are ISO format in this fixture, so the first seven characters identify the month. In production, use the database’s date functions and the agreed timezone. A customer ID should be non-null and stable; otherwise define how to handle anonymous events.
Avoid inflated counts
Inspect the join before aggregating.
Suppose customer C1 has two subscriptions. Joining events to subscriptions on customer_id creates two rows for every C1 event. COUNT(*) inflates, and even SUM(amount) may double. A distinct customer count can mask the duplication for one metric but will not repair other measures. State the grain of both tables and join only after deciding what the output row represents.
| Step | Rows for C1 | Risk |
|---|---|---|
| Original events | E1, E2, E4 | Known event grain. |
| Join to two subscription rows | Each event repeated twice. | Counts/sums may inflate. |
| Preaggregate or select one subscription rule | One defined match. | Metrics align with contract. |
A good SQL answer includes a small fixture with expected output, not only a query that runs.
Keep building this skill
Turn the example into a skill you can use.
Return to the course options whenever you are ready.
Funnel and retention extension
Define eligibility before the ratio.
A purchase funnel can count distinct checkout IDs that started and those that completed. Do not divide raw purchase-event rows by customer sessions if the units differ. For retention, define a cohort entry event, an observation window and a return action. Customers who have not yet had enough time to return should not be treated as failures without an exposure rule.
These extensions require more tables and time logic than the five-row fixture. The method remains the same: document grain, eligibility, deduplication and expected output before writing joins or window functions.
Changed input
Add a duplicate event safely.
Add E6 for C3 on 2026-02-04 with event_type login. What should February active customers be?
Show the answer
Still 2: C1 and C3. COUNT(DISTINCT customer_id) ignores C3’s second qualifying event for the monthly customer metric. The event count rises, but the active-customer count does not.
Next, change E6 to customer C4. February becomes 3. These checks distinguish event volume from active customers.
Interview answer checklist
Show your validation work.
| Question | Your answer should show |
|---|---|
| What is the grain? | One row per event in input; one row per month in output. |
| What counts as active? | Qualifying types and unique customer rule. |
| Does the query run? | Test fixture with expected two-month output. |
| Could a join inflate it? | Inspect one-to-many paths. |
| What could change the result? | Timezone, null IDs and event definition. |
FAQ
Questions about this skill.
Why COUNT(DISTINCT)?
One customer can have multiple qualifying events in a month.
Does this SQL work in every database?
No. Date extraction syntax differs; the example is SQLite-compatible.
Can DISTINCT repair every join problem?
No. It may hide duplication for one count while sums remain inflated.
Continue with the course
Build reliable data analyst skills.
Review the course options and choose your next learning step.