The Ones Nobody Calls

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
L3
Asked in
Microsoft

Problem

Our POST counts per endpoint in api_calls keep coming out low, because the method field is sometimes logged in lowercase and a 'post' is still a POST. To shortlist endpoints for sunsetting, list the ones in the three lowest positions by POST call volume, with each endpoint's call count and its position.

The POST paths the traffic forgot.

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
endpointcall_countrnk
/api/v2/products11
/api/v2/products/22
/api/v1/users/33
/api/v2/analytics33

Worked solution and explanation

What this problem is really testing

This is a bottom-N-with-ties problem wearing a deprecation costume, and the data hides a second trap: the method column is logged in mixed case, so the same endpoint shows up under both 'POST' and 'post'. Filter on method = 'POST' and you quietly drop every lowercase row, undercounting endpoints and reshuffling the whole bottom of the standings. The dense-rank piece is the puzzle everyone sees. The casing is what separates people who inspect their data from people who assume it is clean.


Building it step by step

Step 1: Filter to POST, case-insensitively

Filter to POST traffic, but fold the case first: WHERE UPPER(method) = 'POST'. The seed logs the same verb as 'POST' and 'post', and a case-sensitive equality keeps only one of them.

Step 2: Count calls per endpoint

GROUP BY endpoint and take COUNT(*) as the call volume. This is the number the shortlist is built on.

Step 3: Position by ascending volume

Apply DENSE_RANK() OVER (ORDER BY COUNT(*) ASC). Ascending order puts the quietest endpoints at position 1, and equal counts share a position with no skipped number after them.

Step 4: Keep the three lowest positions

Keep rnk <= 3 in a wrapping query. The filter has to live outside the SELECT that computes the window, because a window alias cannot be referenced in that same WHERE.


The solution

Bottom endpoints by POST volume
SELECT endpoint, call_count, rnk
FROM (
    SELECT
        endpoint,
        COUNT(*) AS call_count,
        DENSE_RANK() OVER (ORDER BY COUNT(*) ASC) AS rnk
    FROM api_calls
    WHERE UPPER(method) = 'POST'
    GROUP BY endpoint
) ranked
WHERE rnk <= 3
Case-sensitive filter (wrong)
WHERE method = 'POST' keeps only rows logged in exact uppercase. Every endpoint ever logged as 'post' loses those calls, so counts come out low and the bottom positions shift to the wrong endpoints.
Case-normalized filter (correct)
WHERE UPPER(method) = 'POST' folds every casing into one bucket. Counts reflect all POST traffic, and the standings hold regardless of how the logger cased the verb.

Common follow-up questions