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

api_calls: 10 of 670 rows
call_idendpointmethodstatuslatencyuser_idcall_timeerr_msg
8079/api/v1/ordersPOST2004.21002026-02-02 01:07:00NULL
8158/api/v2/productsPUT2007.91972026-03-03 02:14:00NULL
8237/api/v1/searchDELETE20111.62942026-04-04 03:21:00NULL
8316/api/v1/auth/loginPATCH20415.33912026-05-05 04:28:00NULL
8395/api/v1/auth/logoutget30119NULL2026-06-06 05:35:00NULL
8474/api/v2/analyticspost40022.75852026-07-07 06:42:00Not found
8553/api/v1/paymentsGET40126.46822026-08-08 07:49:00Unauthorized
8632/api/v1/users/POST40330.17792026-09-09 08:56:00Rate limit exceeded
8711/api/v2/products/PUT40433.88762026-01-10 09:03:00Internal server error
8790/api/v1/usersDELETE50037.5NULL2026-02-11 10:10:00Bad gateway

Expected output

First 5 of 10 rows
endpointavg_daily_active_users
/api/v1/auth/login2
/api/v1/auth/logout0
/api/v1/orders1
/api/v1/payments1
/api/v1/users0

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

AVG DAILY ACTIVE USERS PER ENDPOINT
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