After the Cutoff
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
- Microsoft
Problem
deploy_logs records one row per deploy across several years, with the environment it shipped to, when it ran and how long it took. Only rows whose environment reads exactly 'production', in lowercase, are live deploys. For those that ran in April through December of any year, show each service with its number of deploys and its longest deployment duration in minutes, busiest services first.
The calendar turned but prod kept moving. See which services never stopped shipping.
Tables
deploy_logs
Columns: log_id INTEGER, svc_name TEXT, version TEXT, env_name TEXT, status TEXT, deploy_at TIMESTAMP, dur_secs INTEGER, author TEXT
| log_id | svc_name | version | env_name | status | deploy_at | dur_secs | author |
|---|---|---|---|---|---|---|---|
| 131 | payment-api | v1.0.2 | staging | failed | 2026-02-02 01:11:00 | 23 | bob |
| 162 | user-svc | v1.1.0 | dev | rolled_back | 2026-03-03 02:22:00 | 36 | charlie |
| 193 | search-api | 2.0.0 | canary | in_progress | 2026-04-04 03:33:00 | 49 | dana |
| 224 | gateway | v2.1.0 | Production | Success | 2026-05-05 04:44:00 | 62 | eve |
| 255 | notif-svc | 2.1.1 | STAGING | FAILED | 2026-06-06 05:55:00 | 75 | frank |
| 286 | analytics | v3.0.0-rc1 | production | success | 2026-07-07 06:06:00 | 88 | Alice |
| 317 | ml-serving | v1.0.3 | staging | failed | 2026-08-08 07:17:00 | NULL | BOB |
| 348 | auth-svc | v1.0.1 | dev | Rolled_Back | 2026-09-09 08:28:00 | 114 | alice |
| 379 | Payment-API | v1.0.2 | canary | in_progress | 2026-01-10 09:39:00 | 127 | bob |
| 410 | user-svc | v1.1.0 | Production | Success | 2026-02-11 10:50:00 | 140 | charlie |
Expected output
| svc_name | deploy_count | max_duration_min |
|---|---|---|
| gateway | 7 | 7.966666666666667 |
| auth-svc | 7 | 5.766666666666667 |
| user-svc | 6 | 9.666666666666666 |
| analytics | 6 | 7.066666666666666 |
Worked solution and explanation
This is a filter-then-aggregate over one table, and nearly all of the difficulty sits in the WHERE clause. The statement hands you 2 row rules: env_name must read exactly 'production', and the deploy must fall in April through December of whichever year it ran. Once those 2 predicates are right, the rest is a GROUP BY on svc_name.
Which rows are live deploys
env_name holds 7 spellings in this data: 'staging', 'dev', 'canary', 'Production', 'STAGING', 'production' and 'Dev'. Mixed casing usually invites a cleanup, but the statement defines the lowercase spelling as the live marker, so plain equality env_name = 'production' is the whole rule. Wrapping the column in LOWER readmits the 'Production' rows the statement rules out.
SELECT svc_name,
COUNT(*) AS deploy_count,
CAST(MAX(dur_secs) AS DOUBLE) / 60 AS max_duration_min
FROM deploy_logs
WHERE LOWER(env_name) = 'production'
AND strftime('%m', deploy_at) >= '04'
GROUP BY svc_name
ORDER BY deploy_count DESCThe sample's log_id 224, a gateway deploy tagged 'Production' in May, now passes, so gateway rises above its graded 7.
April through December, any year
- Range pinned to one year
- A lower bound such as
deploy_at >= '2026-04-01'answers a different question: everything since April of this year. Earlier years vanish, and January through March of any later year slip in. - Month read off the timestamp
strftime('%m', deploy_at)returns the month as 2-digit text for every year in the log, and>= '04'keeps '04' through '12'.
Deploy count and longest minutes
Step 1: Count rows with COUNT(*)
dur_secs is NULL on 28 of the 200 rows. A deploy with no recorded duration is still a deploy, so count rows. COUNT(dur_secs) would skip any live deploy whose duration is missing.
Step 2: Take MAX(dur_secs) before converting
MAX ignores NULLs, so a missing duration can never win or blank out a service's maximum. Aggregate in seconds, then convert the one value each service keeps.
Step 3: Divide as a double
dur_secs is an INTEGER. On an engine that truncates integer division, gateway's longest deploy of 478 seconds would come back as 7 minutes; the graded value is 7.966666666666667. CAST(MAX(dur_secs) AS DOUBLE) / 60 guarantees fractional minutes.
SELECT svc_name,
COUNT(*) AS deploy_count,
CAST(MAX(dur_secs) AS DOUBLE) / 60 AS max_duration_min
FROM deploy_logs
WHERE env_name = 'production'
AND strftime('%m', deploy_at) >= '04'
GROUP BY svc_name
ORDER BY deploy_count DESC4 services come back: gateway and auth-svc with 7 deploys each, user-svc and analytics with 6. ORDER BY deploy_count DESC delivers busiest first and leaves the order inside each tie to the engine. The cased and padded payment-api spellings in svc_name never reach the output, because none of their rows passes both filters.
Common follow-up questions
- Gateway and auth-svc tie on
deploy_count. How do you make the order deterministic?Add a tiebreaker such as
svc_nameafterdeploy_count DESC. Without it a report can reshuffle the 7-7 and 6-6 pairs between runs, which looks like a data change to whoever reads it. - Should failed or rolled-back deploys count?
As written every live row counts whatever its
status; to count only successful ones, add astatuspredicate and handle that column mixing 'success' and 'Success'. Attempts and shipped releases are different metrics, so confirm which one the reader expects. - What changes if a
svc_namevariant like 'Payment-API' passes the filters?Each spelling becomes its own group, so you would group on
LOWER(TRIM(svc_name))once the owners confirm they are one service. A service split across several rows ranks lower than it should in a busiest-first list. - A service has live deploys in range but every
dur_secsis NULL. What does its row show?deploy_countis still its row count andmax_duration_minis NULL, becauseMAXover only NULLs is NULL and dividing NULL by 60 stays NULL. Decide with consumers whether a blank or a default belongs in the report.