Deploy Count by Service
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
- Asana
Problem
Every deployment attempt counts here, whether it succeeded, failed, rolled back or is still in progress. An engineer scoping a deployment automation project needs each service's total deployment count from deploy_logs, most frequently deployed first.
Some services deploy constantly. Others barely at all.
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
| 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 |
|---|---|
| user-svc | 26 |
| search-api | 26 |
| notif-svc | 24 |
| payment-api | 23 |
| payment-api | 1 |
Worked solution and explanation
What this really is
Strip off the automation-project framing and this is a per-service tally with the sort pointed at the tally, not at the name. Everyone writes COUNT(*). What separates answers is what they quietly add or leave out: a WHERE status = 'success' that throws away every failed and rolled-back attempt, or an ORDER BY svc_name that lists services alphabetically. Either one and the busiest services stop sitting at the top, so the automation work starts in the wrong place.
Step 1: Count every attempt, not every success
status mixes 'failed', 'rolled_back', 'in_progress' and a few case variants. Each row is still a deployment someone ran, so nothing gets filtered. COUNT(*) counts rows, which is exactly the unit the question is about.
Step 2: Collapse to one row per svc_name
GROUP BY svc_name turns 200 log rows into one row per service name. Without it, COUNT(*) next to svc_name either errors or folds the whole table into one total. SELECT DISTINCT svc_name gives you the names with no tally at all.
Step 3: Sort by the tally, biggest first
ORDER BY deploy_count DESC sorts on the alias. 'gateway' and 'search-api' both land on 26, and the question says nothing about which comes first. Either order is correct, so no tiebreaker is owed.
SELECT svc_name, COUNT(*) AS deploy_count
FROM deploy_logs
GROUP BY svc_name
ORDER BY deploy_count DESCCommon follow-up questions
- `payment-api` appears in `svc_name` under four spellings, two of them padded with spaces. How would you fold them into one service, and what does that do to the top of the list? (Normalizing a dirty grouping key before counting.)
- How would you return only the top 5 services, and what if the fifth and sixth tie? (A `LIMIT` that cuts between tied rows needs a stated tie rule.)
- If you also had a service catalog table, how would you include services with zero deployments? (An outer join from the catalog, counting a column that is `NULL` when there is no match.)
- How would you show each service's deployment count per month using `deploy_at`? (Adding a truncated date to the grain.)