Everybody Wants a Bigger Screen

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
Airbnb

Problem

Count how many times each user searched for a mechanical keyboard in our search_queries log, including terms where the phrase sits inside a longer query. Terms are stored as typed, so 'Mechanical Keyboard' and 'mechanical keyboard' are the same search.

The search bar never lies about what people actually want.

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 5 rows
user_idkeyboard_searches
4882
24282
43682
63082
82482

Worked solution and explanation

What this really is

This is a per-user tally of a fuzzy text filter wearing a demand-sizing costume. The skill being probed: can you count rows per user while making a substring match survive capitalization? Anyone can type GROUP BY user_id. The catch is that 'Mechanical Keyboard' has to count exactly like 'mechanical keyboard'. Miss the case fold and you silently undercount real shoppers, the exact people the team is trying to size.


Break it down

Step 1: Filter to keyboard intent, case and all

Normalize the text before you match it: LOWER(search_term) LIKE '%mechanical keyboard%'. The leading and trailing wildcards let the phrase sit anywhere in the term, including the rows logged with stray spaces around it, and the LOWER() fold is what pulls in 'Mechanical Keyboard' alongside the lowercase hits.

Step 2: Count per user

Collapse the surviving rows to one row per shopper with GROUP BY user_id, and COUNT(*) the searches inside each group. COUNT(*) rather than a count of unique terms, because a repeat search is a return visit and is the signal. Users who never searched the phrase fall out because WHERE runs before the grouping, and the logged-out searches land together in one NULL group.


The solution

Count keyboard-intent searches per user
SELECT user_id, COUNT(*) AS keyboard_searches
FROM search_queries
WHERE LOWER(search_term) LIKE '%mechanical keyboard%'
GROUP BY user_id
Case-blind filter (wrong)
WHERE search_term LIKE '%mechanical keyboard%' only catches the lowercase spellings. A user who typed 'Mechanical Keyboard' vanishes, and one who mixed spellings shows half their real count.
Case-folded filter (correct)
WHERE LOWER(search_term) LIKE '%mechanical keyboard%' folds every spelling to the same case first, so both users come out with their real counts.

Common follow-up questions