Q2 Search Volume

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
Meta

Problem

How many queries in search_queries were received from April 1 through June 30 2026, the last day included, for the search team's Q2 benchmark against the prior quarter?

Q2 search volume. The numbers.

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 1 of 1 rows
q2_volume
40

Worked solution and explanation

What this really is

This is a timestamp boundary problem dressed up as a quarterly count. The COUNT(*) is free. The skill is bounding query_time, which carries a time of day, so that "through June 30 inclusive" keeps all of June 30. The reflex is BETWEEN '2026-04-01' AND '2026-06-30', and that upper bound means midnight: every query stamped later that day drops out, and the quarter quietly loses most of a day with no error to warn you.

This sample happens to hold no queries late on June 30, so here the shortcut and the correct filter return the same number. A production log will not be so kind, which is why the interviewer listens to how you write the bound and not just whether the count matches.

Building it

Step 1: Read the column before the calendar

query_time holds values like '2026-04-04 03:33:00': a date plus a time. Any bound written as a bare date therefore compares against the very first instant of that day, not the whole day.

Step 2: Close the range at the next quarter's start

query_time >= '2026-04-01' AND query_time < '2026-07-01' is inclusive at midnight on April 1 and excludes exactly July 1. Every instant of June 30 sits between them, whatever precision the timestamps carry.

Step 3: Count rows, not users

Volume is every received query. A quarter of the rows have a NULL user_id (signed-out searches), so COUNT(user_id) would understate the total. COUNT(*) counts the rows themselves.

The solution

Filter to q2 date range to find q2 search volume
SELECT COUNT(*) AS q2_volume
FROM search_queries
WHERE query_time >= '2026-04-01'
  AND query_time < '2026-07-01'
Closed range on a date
BETWEEN '2026-04-01' AND '2026-06-30' stops at '2026-06-30 00:00:00'. Stored as text, '2026-06-30 14:05:00' also sorts after '2026-06-30', so either way the last day is cut to its midnight.
Half-open range
>= '2026-04-01' and < '2026-07-01' hold every instant of the quarter, and the same pair of bounds works unchanged for any quarter you plug in.

Common follow-up questions