Where the Exports Land

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
Vesta Innovations

Problem

Export jobs have been flaky lately, so for every batch job whose name contains 'export', count how many landed in each completion status.

Every export run ends somewhere. Tally the endings, loudest first.

Tables

batch_jobs

Columns: job_id INTEGER, job_name TEXT, status TEXT, rows_done INTEGER, started TEXT, ended TEXT, priority INTEGER, retries INTEGER

batch_jobs: 10 of 200 rows
job_idjob_namestatusrows_donestartedendedpriorityretries
1047user_exportrunning972026-02-02 01:00:002026-02-02 03:11:0030
1094data_cleanupfailed1942026-03-03 02:00:002026-03-03 04:22:0060
1141email_blastqueued2912026-04-04 03:00:002026-04-04 05:33:0090
1188index_rebuildcanceled3882026-05-05 04:00:00NULL20
1235cache_warmCompleted4852026-06-06 05:00:002026-06-06 07:55:0050
1282log_archiveFAILEDNULL2026-07-07 06:00:002026-07-07 08:06:0080
1329metric_aggcompleted6792026-08-08 07:00:002026-08-08 09:17:0010
1376backup_dbrunning7762026-09-09 08:00:00NULL40
1423sync_elasticfailed8732026-01-10 09:00:002026-01-10 11:39:0075
1470daily_reportqueued9702026-02-11 10:00:002026-02-11 12:50:0000

Expected output

First 5 of 7 rows
statusoccurrences
Completed2
FAILED2
canceled4
completed4
failed2

Worked solution and explanation

What this problem is really testing

Strip the business framing and this is a filtered frequency distribution: count how often each status appears, but only among the rows whose job_name carries the 'export' substring. Anyone can write COUNT(*) with a GROUP BY. What separates candidates is getting the filter in the right place and the right shape: a substring LIKE '%export%' in the WHERE clause, evaluated before the grouping. Push that predicate into a HAVING, or match on equality instead of a substring, and the counts quietly include the wrong rows or drop the export jobs entirely.


Build it in three moves

Step 1: Filter before you group

Put the substring match in WHERE: job_name LIKE '%export%'. The percent signs on both sides matter, because names like user_export and nightly_export only match with a leading wildcard. Filtering here shrinks the set before any counting happens, so the aggregate never sees a non-export row.

Step 2: Count per status

GROUP BY status with COUNT(*) gives one row per distinct status and its occurrence count. COUNT(*) is right here because you want every matching row, not just the non-null ones, and status never goes null in this data.

Step 3: Order so the most common leads

ORDER BY occurrences DESC puts the most common status on top. Add status as a secondary sort so that two statuses with equal counts come back in a stable order instead of whatever the engine happens to emit.


The solution

Filtered frequency count, most common first
SELECT status, COUNT(*) AS occurrences
FROM batch_jobs
WHERE job_name LIKE '%export%'
GROUP BY status

Common follow-up questions