First Among Results

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

Domain
SQL
Difficulty
hard
Seniority
L4
Asked in
ESPN

Problem

The search relevance team wants to know whether longer queries push shoppers onto the very first hit. In search_queries, look only at searches that ended in a click: clicked_result holds the position of the result clicked, where 1 is the top result and an empty value means no click. For each search term length, show how many of these clicked searches there were and how many landed on that top result.

When queries run long, does the top hit still win?

Tables

search_queries

Columns: query_id INTEGER, user_id INTEGER, search_term TEXT, results_count INTEGER, clicked_result INTEGER, query_time TEXT

search_queries: 10 of 200 rows
query_iduser_idsearch_termresults_countclicked_resultquery_time
2071100laptop deals1722026-02-02 01:11:00
2142197phone case iphone 153432026-03-03 02:22:00
2213294usb-c cable 6ft51NULL2026-04-04 03:33:00
2284NULLgaming mouse6852026-05-05 04:44:00
2355488mechanical keyboard8562026-06-06 05:55:00
24265854k monitor102NULL2026-07-07 06:06:00
2497682standing desk11982026-08-08 07:17:00
2568NULLwebcam 1080p13692026-09-09 08:28:00
2639876bluetooth speaker153NULL2026-01-10 09:39:00
2710973smart watch17012026-02-11 10:50:00

Expected output

First 5 of 13 rows
term_lengthquery_counttop_click_count
840
1050
11114
19157
2111

Worked solution and explanation

What this problem is really testing

On the surface this is a click-through report by search term length. Underneath, clicked_result is not a yes/no flag: it is the position the shopper clicked, and an empty value means they clicked nothing at all. Two edges bite. First, the report is about searches that ended in a click, so the no-click rows have to leave before you count, or your per-length totals quietly include searches nobody acted on. Second, only position 1 is the top result: reach for COUNT(clicked_result) and a click on the ninth result counts exactly like a click on the first, inflating the top-result number on every length.

COUNT(`clicked_result`)
Counts every non-null value, so clicks on position 2, 5, or 9 all count. This answers 'how many clicked anything', not 'how many clicked the top result', and it inflates the intended number on this data.
SUM(CASE WHEN `clicked_result` = 1 THEN 1 ELSE 0 END)
Counts only the rows where the top result was clicked. Every other position contributes 0. This is the metric the prompt actually asks for.

Walking through it

Step 1: Keep only clicked searches

Keep only searches that ended in a click with WHERE clicked_result IS NOT NULL. The no-click rows have an empty position and would pad the per-length total with searches nobody acted on, changing what query_count means.

Step 2: Derive the length dimension

Group by LENGTH(search_term). The length itself is the reporting bucket, so compute it once and reuse the alias in the GROUP BY and ORDER BY.

Step 3: Count the clicked searches

COUNT(*) gives the total clicked searches at each length. Because the no-click rows are already gone, this denominator means 'searches that ended in a click', which is what the team compares against.

Step 4: Conditionally count top-result clicks

SUM(CASE WHEN clicked_result = 1 THEN 1 ELSE 0 END) counts only clicks on the top result. This is the line that separates candidates from the COUNT(clicked_result) crowd who never checked what the column actually holds.

Step 5: Sort the output

ORDER BY term_length returns shortest to longest, matching the preview so the trend by length reads top to bottom.


The solution

Top-result clicks among clicked searches, by term length
SELECT LENGTH(search_term) AS term_length, COUNT(*) AS query_count, SUM(CASE WHEN clicked_result = 1 THEN 1 ELSE 0 END) AS top_click_count
FROM search_queries
WHERE clicked_result IS NOT NULL
GROUP BY term_length

Common follow-up questions