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, 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 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 4 of 4 rows
svc_namedeploy_countmax_duration
gateway7478
auth-svc7346
user-svc6580
analytics6424

Worked solution and explanation

What this problem is really testing

This is a recurring calendar window dressed up as a deployment audit. The skill being probed: can you select April through December in every year of a log that spans five years, rather than one stretch of dates starting at a single April? Anyone can write COUNT(*) and MAX(dur_secs). What separates people is 'any year'. Compare deploy_at to a fixed date and you keep either one year alone or one year's late months plus everything after it. Either way your counts, your longest durations and the order of the services all shift.


The trap

The log covers five calendar years. 'April through December of any year' means months 4 to 12 inside each of them, not a continuous window starting from one April. A filter like deploy_at >= '2026-04-01' keeps only the latest year's late months and quietly drops the four earlier years. deploy_at >= '2022-04-01' swings the other way and keeps every January to March after that first spring.


Building the query

Step 1: Keep only the deploys that qualify

Two conditions go in the WHERE: env_name = 'production' for the environment, and the month of deploy_at at 4 or higher. strftime('%m', deploy_at) returns text such as '04', so cast it to an integer before comparing. Left as text, it never compares as the number you meant, and every month slips through.

Step 2: Roll the survivors up per service

GROUP BY svc_name collapses each service to one row. COUNT(*) counts every qualifying deploy, including the ones with no recorded duration. MAX(dur_secs) skips those NULL durations, so a missing value never wins.

Step 3: Put the busiest services on top

ORDER BY deploy_count DESC is the whole ordering the question asks for. Services that tie on count may come back in either order. Nothing in the ask separates them, so a second sort key would answer a question nobody posed.

Per-service production deploys from April onward
SELECT svc_name,
       COUNT(*) AS deploy_count,
       MAX(dur_secs) AS max_duration
FROM deploy_logs
WHERE env_name = 'production'
  AND CAST(strftime('%m', deploy_at) AS INTEGER) >= 4
GROUP BY svc_name
ORDER BY deploy_count DESC
Pins a single year
WHERE deploy_at >= '2026-04-01' looks reasonable, but it hard-codes one year and drops every other year's April to December deploys. On a multi-year log it silently undercounts.
Tests the month alone
CAST(strftime('%m', deploy_at) AS INTEGER) >= 4 keeps months 4 through 12 in every year, which is exactly what 'any year' asks for.

Common follow-up questions