Search Terms Starting With G

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
Google

Problem

The search relevance team is debugging why queries starting with 'g' return fewer results than expected. Pull all such search terms alongside their results count.

Queries starting with 'g'.

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 10 rows
search_termresults_count
gaming mouse68
gaming mouse408
gaming mouse248
gming mouseNULL
gming mouseNULL

Worked solution and explanation

This is a prefix filter that has to leave the evidence alone. Matching terms that begin with 'g' is the easy half. LIKE 'g%' does it, while = 'g' matches nothing and '%g%' drags in 'ergonomic chair' and 'fast charger'. The half that separates candidates is restraint. The team is chasing 'g' searches that come back short, and the rows with no results_count are exactly those searches. Add a reflexive IS NOT NULL, a results_count > 0, a DISTINCT or a GROUP BY, and the report shows healthy 'gaming mouse' rows and no problem at all.

Getting to the query

Step 1: Anchor the pattern at the start of search_term

'Starting with g' is a prefix, so the wildcard goes after the letter: LIKE 'g%'. Equality only matches the one-letter string 'g', which nobody searched. A wildcard on both sides finds a 'g' anywhere and over-includes 'rgb lighting', 'cable management' and half the catalog.

Step 2: Keep every matching search as its own row

Ten searches match: eight 'gaming mouse' and two 'gming mouse'. The repeats are separate searches, and both 'gming mouse' searches logged no results_count. No DISTINCT, no GROUP BY and no null filter: each one erases part of what the team is debugging.

Step 3: Return the term and its count, nothing more

Project search_term and results_count and stop. query_id, user_id and query_time would help a later drill-down, but they are not what was asked for.

Every search that starts with 'g', as logged
SELECT search_term, results_count
FROM search_queries
WHERE search_term LIKE 'g%'
Cleaned up
WHERE search_term LIKE 'g%' AND results_count IS NOT NULL returns eight 'gaming mouse' rows with counts such as 68, 408 and 248. Every 'g' search looks fine.
As logged
WHERE search_term LIKE 'g%' alone returns ten rows. The two extra rows are 'gming mouse' with an empty count, and that typo is where the results go missing.

Common follow-up questions