Who's Holding Up Traffic
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L4
- Asked in
- Workday
Problem
Find the average number of unique users hitting each endpoint on a given day of June 2026, with endpoints listed alphabetically so the result works as a lookup reference. Endpoint paths come straight from the gateway log, where a path ending in a slash is routed separately from the same path without one, so the two stay separate endpoints.
Some endpoints carry the product. Others are barely touched.
Tables
api_calls
Columns: call_id INTEGER, endpoint TEXT, method TEXT, status INTEGER, latency REAL, user_id INTEGER, call_time TEXT, err_msg TEXT
| call_id | endpoint | method | status | latency | user_id | call_time | err_msg |
|---|---|---|---|---|---|---|---|
| 8079 | /api/v1/orders | POST | 200 | 4.2 | 100 | 2026-02-02 01:07:00 | NULL |
| 8158 | /api/v2/products | PUT | 200 | 7.9 | 197 | 2026-03-03 02:14:00 | NULL |
| 8237 | /api/v1/search | DELETE | 201 | 11.6 | 294 | 2026-04-04 03:21:00 | NULL |
| 8316 | /api/v1/auth/login | PATCH | 204 | 15.3 | 391 | 2026-05-05 04:28:00 | NULL |
| 8395 | /api/v1/auth/logout | get | 301 | 19 | NULL | 2026-06-06 05:35:00 | NULL |
| 8474 | /api/v2/analytics | post | 400 | 22.7 | 585 | 2026-07-07 06:42:00 | Not found |
| 8553 | /api/v1/payments | GET | 401 | 26.4 | 682 | 2026-08-08 07:49:00 | Unauthorized |
| 8632 | /api/v1/users/ | POST | 403 | 30.1 | 779 | 2026-09-09 08:56:00 | Rate limit exceeded |
| 8711 | /api/v2/products/ | PUT | 404 | 33.8 | 876 | 2026-01-10 09:03:00 | Internal server error |
| 8790 | /api/v1/users | DELETE | 500 | 37.5 | NULL | 2026-02-11 10:10:00 | Bad gateway |
Expected output
| endpoint | avg_daily_active_users |
|---|---|
| /api/v1/auth/login | 2 |
| /api/v1/auth/logout | 0 |
| /api/v1/orders | 1 |
| /api/v1/payments | 1 |
| /api/v1/users | 0 |
Worked solution and explanation
What this is really asking
This is a per-day aggregation wearing an API-metrics costume. The trap: group by endpoint in one pass and you average over the wrong denominator. You have to count the unique users within each calendar day first, then average those daily counts per endpoint. Skip the inner day bucket and a user who hits /search on ten days of June collapses into a single distinct value, dragging the answer down toward unique-visitors-over-active-days instead of true daily traffic.
Break down the requirements
Step 1: Filter to June of the target year
strftime('%Y-%m', call_time) = '2026-06' keeps exactly June of the target year. It reads cleanly, but wrapping the column in a function hides it from partition pruning; on the production table you would write the half-open range call_time >= '2026-06-01' AND call_time < '2026-07-01' instead.
Step 2: Aggregate per (endpoint, day)
Inner query computes COUNT(DISTINCT user_id) grouped by endpoint and DATE(call_time). That gives one row per endpoint per active day, the daily unique-user count.
Step 3: Average across days
Outer query takes AVG(daily_users) grouped by endpoint. Days where the endpoint had zero calls are absent, so the denominator is days-active, not a fixed 30.
The solution
SELECT endpoint,
AVG(daily_users) AS avg_daily_active_users
FROM (
SELECT endpoint,
DATE(call_time) AS call_day,
COUNT(DISTINCT user_id) AS daily_users
FROM api_calls
WHERE strftime('%Y-%m', call_time) = '2026-06'
GROUP BY endpoint, DATE(call_time)
) sub
GROUP BY endpoint
ORDER BY endpoint;COMMON FOLLOW-UP QUESTIONS
Common follow-up questions
- How would you change this if the team wanted the average over all 30 calendar days, including days with zero traffic? (Cross join endpoints to a generated date series, left join the per-day counts, and `COALESCE` missing days to zero before averaging.)
- What if `user_id` can be null for unauthenticated traffic? (`COUNT(DISTINCT user_id)` already ignores NULLs, but the team may want anonymous sessions bucketed by IP or `session_id` as a separate identity column.)
- How would you rank the endpoints by week-over-week change in this metric? (Compute the daily-uniques aggregation per ISO week, then `LAG` the average per endpoint ordered by week to get the delta.)