Rollback Roulette

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
Virgin Group

Problem

Count how many deployments in deploy_logs ended in a failed status, across every service and environment the log covers. Several CI tools have written to that log over the years, so a failure counts however the tool happened to spell its status.

Some ships sink before they leave the harbor.

Tables

deploy_logs

Columns: log_id INTEGER, svc_name TEXT, version TEXT, env_name TEXT, status TEXT, deploy_at TEXT, dur_secs INTEGER, author TEXT

deploy_logs: 10 of 200 rows
log_idsvc_nameversionenv_namestatusdeploy_atdur_secsauthor
131payment-apiv1.0.2stagingfailed2026-02-02 01:11:0023bob
162user-svcv1.1.0devrolled_back2026-03-03 02:22:0036charlie
193search-api2.0.0canaryin_progress2026-04-04 03:33:0049dana
224gatewayv2.1.0ProductionSuccess2026-05-05 04:44:0062eve
255notif-svc2.1.1STAGINGFAILED2026-06-06 05:55:0075frank
286analyticsv3.0.0-rc1productionsuccess2026-07-07 06:06:0088Alice
317ml-servingv1.0.3stagingfailed2026-08-08 07:17:00NULLBOB
348auth-svcv1.0.1devRolled_Back2026-09-09 08:28:00114alice
379Payment-APIv1.0.2canaryin_progress2026-01-10 09:39:00127bob
410user-svcv1.1.0ProductionSuccess2026-02-11 10:50:00140charlie

Expected output

First 1 of 1 rows
failed_deployments
66

Worked solution and explanation

What this is really asking

This is a data-cleaning question wearing an incident-review costume. Counting rows behind one predicate is trivial; the skill is noticing that status is not one vocabulary but several, written by different CI tools, and that 'failed' and 'FAILED' are the same outcome. Anyone writes COUNT(*) with a WHERE. What separates candidates is folding case on the column before comparing. Skip it and the count comes back short by exactly the failures one tool chose to shout.


Building it

Step 1: Fold status to one case

Wrap the column, not the value: LOWER(status) maps 'FAILED', 'Failed' and 'failed' onto one string, so a single comparison catches every tool's spelling, including one nobody has written yet.

Step 2: Match only the failed outcome

Compare the folded value to 'failed' and nothing else. 'rolled_back' and 'in_progress' are different outcomes; a rollback means the failure was handled, and an in-flight deploy has not ended at all.

Step 3: Count the rows that survive

COUNT(*) over the surviving rows, aliased to failed_deployments. Every row is one deploy and log_id is unique, so there is nothing to deduplicate.

Failed deployment count
SELECT COUNT(*) AS failed_deployments
FROM deploy_logs
WHERE LOWER(status) = 'failed'
Exact match
WHERE status = 'failed' returns a believable number and never errors. It also skips every row a tool logged as 'FAILED', so the total is quietly low.
Folded match
WHERE LOWER(status) = 'failed' treats every casing as the same outcome, so the count reflects what actually happened rather than which tool happened to log it.

Common follow-up questions

Common follow-up questions