Long Searches Containing 'er'

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
Amazon

Problem

An analyst's typeahead report for the letter 'm' keeps coming up short, and she suspects the queries people typed with capitals are slipping past it. From search_queries, pull every query whose term runs longer than 12 characters and contains an 'm' in any casing, with all of its fields and the term exactly as the user typed it.

Long queries with 'er'. A pattern?

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 34 rows
query_iduser_idsearch_termresults_countclicked_resultquery_time
2355488mechanical keyboard8562026-06-06 05:55:00
3136NULLergonomic chair27272026-08-17 16:56:00
32071652monitor arm12328982026-09-06 17:07:00
33491846cable management323102026-02-20 19:29:00
37752428Mechanical Keyboard42562026-08-26 01:35:00

Worked solution and explanation

What this really is

This problem is two filters on one column, and the second filter is a case problem. The length cut is easy, and almost everyone gets it right. What separates candidates is whether they notice that search_queries stores the same query under more than one capitalization. The report is coming up short because a raw match for 'm' cannot see an 'M'. Fold case inside the comparison and only there. If you skip it, the capitalized spellings vanish. If you fold case in the SELECT list, you return text that no longer matches the table.

Step 1: Measure the whole term with LENGTH

LENGTH(search_term) > 12 counts every character, spaces included, so a two-word query is judged on its full text. This is the long-tail cut, and it does real work: several short terms also contain an 'm' and must fall out here.

Step 2: Fold case inside the predicate

LOWER(search_term) LIKE '%m%' compares a lowercased copy of the term. Any spelling of the query, whether it starts with 'M' or is typed in all capitals, now matches the same pattern. The two conditions are independent, so join them with AND.

Step 3: Return the rows as stored

Select the six columns directly. The LOWER call exists only for the comparison, so search_term comes back exactly as the user typed it, capitals and all. Nothing in the ask is about order, so no sort is needed.

Long queries containing an 'm', any casing
SELECT query_id, user_id, search_term, results_count, clicked_result, query_time
FROM search_queries
WHERE LENGTH(search_term) > 12
    AND LOWER(search_term) LIKE '%m%'
Raw match
search_term LIKE '%m%' keeps only the spellings that happen to be lowercase where the 'm' sits. The capitalized copies of the same query drop out silently, and the query still runs without error.
Folded match
LOWER(search_term) LIKE '%m%' evaluates every spelling as one form. Every copy of the query is counted, and the returned search_term is untouched because the fold never leaves the WHERE clause.

Common follow-up questions