Shared Endpoints

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3
Asked in
Dropbox

Problem

Raw call counts in api_calls make some endpoints look widely used when a few chatty users drive most of the traffic. For the platform team's deprecation review, surface the endpoints called by more than 60 different users, with the headcount for each.

Shared credentials across endpoints.

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 4 of 4 rows
endpointuser_count
/api/v1/orders100
/api/v1/search70
/api/v1/users100
/api/v2/products100

Worked solution and explanation

What this really is

Strip the deprecation story and this is a per-endpoint headcount with a gate on it. Anyone can bucket api_calls by endpoint and count something. What separates candidates is refusing to count rows. COUNT(*) folds in every repeat call and every anonymous hit with a null user_id, so an endpoint a few chatty clients hammer looks widely adopted. On this data /api/v1/auth/login takes more than 60 calls, but no more than 60 people stand behind them. Count rows and it stays on the list; count people and it drops off. The number you show and the number you gate on are the same COUNT(DISTINCT user_id).

Step 1: Measure reach in people, not calls

Bucket the calls by endpoint and measure reach with COUNT(DISTINCT user_id). The dedup collapses repeat calls from one person to a single head. Aggregates skip nulls, so the unauthenticated rows fall out without a separate filter.

Step 2: Gate after the grouping on the same headcount

Keep only the widely used endpoints with HAVING COUNT(DISTINCT user_id) > 60. HAVING runs after the grouping, so the headcount exists to test. The dedup has to appear here too: drop it and the gate quietly reverts to counting calls.

The solution

Endpoints with more than 60 different users
SELECT endpoint, COUNT(DISTINCT user_id) AS user_count
FROM api_calls
GROUP BY endpoint
HAVING COUNT(DISTINCT user_id) > 60
Gate on calls
HAVING COUNT(*) > 60 counts every row, so repeat calls and null user_id hits push /api/v1/auth/login over the bar, and every headcount is inflated.
Gate on people
HAVING COUNT(DISTINCT user_id) > 60 counts each identified user once, so only the four endpoints genuinely used by more than 60 people survive.

Common follow-up questions