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

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_min
gateway77.966666666666667
auth-svc75.766666666666667
user-svc69.666666666666666
analytics67.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.

Lowercasing the environment over-counts
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 DESC

The 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.

Per-service production deploys from April onward
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 DESC

4 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