First Deploy Attribution
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L4
- Asked in
- Tesla
Problem
Give each service in deploy_logs its total_deploys and its first_time_deploys, the deployments that were some author's first-ever deployment to that service: the onboarding-velocity number a release engineer tracks. Service names were typed with inconsistent letter case and surrounding spaces, and author names with inconsistent letter case, so each still means one service or one person.
The first deploy per service.
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 | total_deploys | first_time_deploys |
|---|---|---|
| analytics | 24 | 1 |
| auth-svc | 24 | 1 |
| gateway | 26 | 1 |
| ml-serving | 24 | 1 |
| payment-api | 26 | 1 |
Worked solution and explanation
What this is really asking
This is a two-grain count presented as an onboarding metric. The total lives at the service grain, but 'first' lives at the (author, svc_name) grain. Anyone can write COUNT(*) per service. The trap is judging 'first' per service alone, which pins first_time_deploys to 1 everywhere. That erases every engineer after the first who onboarded onto a service, and that signal is the reason the report exists.
Break down the requirements
Step 1: Rank within (author, svc_name)
Clean the names first: LOWER(TRIM(svc_name)) folds ' payment-api', 'Payment-API' and 'payment-api ' into one service, and LOWER(author) makes 'Alice' and 'alice' one engineer. Then ROW_NUMBER() OVER (PARTITION BY LOWER(author), LOWER(TRIM(svc_name)) ORDER BY deploy_at) tags each row. rn = 1 marks that author's first deploy to that service, so a service three engineers onboarded onto carries three of them.
Step 2: Aggregate per service
Group by svc_name. COUNT(*) is the total, and SUM(CASE WHEN rn = 1 THEN 1 ELSE 0 END) is the first-deploy subset. Both are counted in the same pass over every row.
The solution
SELECT
dl.svc_name,
COUNT(*) AS total_deploys,
SUM(CASE WHEN dl.rn = 1 THEN 1 ELSE 0 END) AS first_time_deploys
FROM (
SELECT
LOWER(TRIM(svc_name)) AS svc_name,
ROW_NUMBER() OVER (
PARTITION BY LOWER(author), LOWER(TRIM(svc_name))
ORDER BY deploy_at
) AS rn
FROM deploy_logs
) dl
GROUP BY dl.svc_name
;COMMON FOLLOW-UP QUESTIONS
Common follow-up questions
- What if two deploys by the same author share a `deploy_at`? (`ROW_NUMBER` picks one of the tied rows arbitrarily. Add `log_id` to the window's `ORDER BY`, or use `RANK` if both rows should count as first.)
- How would you also report the share of first-time deploys per service? (Wrap with `1.0 * first_time_deploys / total_deploys`, or use AVG over the CASE expression.)
- How would you restrict to successful deploys only? (Add `WHERE status = 'success'` inside the subquery so that failed attempts do not count as someone's first.)