Trim Search Terms Left

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

A data quality check flagged leading whitespace in some search terms. Return a cleaned-up list showing each search term with the leading spaces stripped, by query ID.

Leading whitespace. Clean it up.

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 200 rows
query_idsearch_term
2071laptop deals
2142phone case iphone 15
2213usb-c cable 6ft
2284gaming mouse
2355mechanical keyboard

Worked solution and explanation

Why this problem exists in real interviews

This tests basic string function knowledge. Interviewers use data quality cleanup scenarios to check that you know LTRIM and distinguish it from TRIM and RTRIM.


Break down the requirements

Step 1: Apply LTRIM to strip leading spaces

LTRIM(search_term) removes leading whitespace from each search term.

Step 2: Return by query ID

Select query_id and the cleaned search term.


The solution

LTRIM for leading whitespace cleanup
SELECT query_id, LTRIM(search_term) AS search_term
FROM search_queries

Common follow-up questions