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

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 5 of 8 rows
svc_nametotal_deploysfirst_time_deploys
analytics241
auth-svc241
gateway261
ml-serving241
payment-api261

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

FIRST DEPLOY ATTRIBUTION
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