The Most Recent Word

A hard SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
hard
Seniority
L4
Asked in
City and County of San Francisco

Problem

Our data quality system appends a new row every time it runs a check against a table. For each table, return the rule from its most recent check, when that check ran, and the number of different calendar months in which the table has been checked.

Every table keeps a record. Only the latest verdict still speaks.

Tables

dq_checks

Columns: check_id INTEGER, tbl_name TEXT, col_name TEXT, rule TEXT, passed INTEGER, fail_pct REAL, run_at TEXT, severity TEXT

dq_checks: 10 of 200 rows
check_idtbl_namecol_namerulepassedfail_pctrun_atseverity
131usersemailunique1NULL2026-02-02 01:00:00high
162productsamountrange1NULL2026-03-03 02:00:00medium
193transactionsstatusformat1NULL2026-04-04 03:00:00low
224eventscreated_atreferential1NULL2026-05-05 04:00:00Critical
255sessionspricefreshness0152026-06-06 05:00:00HIGH
286ad_impressionsregionNot_Null1NULL2026-07-07 06:00:00critical
317ordersuser_idUNIQUE102026-08-08 07:00:00high
348usersemailnot_null1NULL2026-09-09 08:00:00medium
379productsamountunique1NULL2026-01-10 09:00:00low
410transactionsstatusrange0302026-02-11 10:00:00Critical

Expected output

First 5 of 7 rows
tbl_namerulerun_atmonths_checked
ad_impressionsrange2026-09-07 18:00:0020
eventsfreshness2026-09-02 05:00:0020
ordersformat2026-09-08 11:00:0021
productsreferential2026-09-05 20:00:0021
sessionsunique2026-09-06 17:00:0020

Worked solution and explanation

What this really is

This is a latest-row-per-table pick stapled to a per-table coverage count, both wearing a data-quality badge. Two skills are probed at once: for each table pull the single newest row and carry its OWN rule and run_at along, and separately count how many different calendar months that table appears in. Anyone can find the newest timestamp per table. The trap is keeping the rule that belongs to that timestamp while ALSO counting months across every row of the table, not just the one that survived. Collapse the rows first and the month count drops to one; count rows instead of months and a table checked twice in March looks like two months of coverage.


Walking through it

Step 1: Rank each table's checks by recency

Number each table's checks newest-first inside a derived table: ROW_NUMBER() OVER (PARTITION BY tbl_name ORDER BY run_at DESC). Ordering on the full run_at timestamp (not just the date) makes recency exact, and ranking the whole row means the winning rule and run_at come along automatically.

Step 2: Keep only the newest check

Filter the derived table to rn = 1, so exactly one row per table survives: the most recent check with its own rule and run_at intact.

Step 3: Count distinct months per table

For each surviving row, compute month coverage with a correlated (SELECT COUNT(DISTINCT STRFTIME('%Y-%m', c.run_at)) FROM dq_checks c WHERE c.tbl_name = latest.tbl_name GROUP BY c.tbl_name). Scoping by tbl_name and grouping on it rolls the table's whole history into one bucket; STRFTIME maps each timestamp to its calendar month and DISTINCT collapses repeat checks in the same month, so coverage is measured in months, not rows. Because this subquery reads the table's RAW rows, collapsing to the newest row never shrinks it.


The solution

Latest verdict plus month coverage
SELECT
    latest.tbl_name,
    latest.rule,
    latest.run_at,
    (
        SELECT COUNT(DISTINCT STRFTIME('%Y-%m', c.run_at))
        FROM dq_checks c
        WHERE c.tbl_name = latest.tbl_name
        GROUP BY c.tbl_name
    ) AS months_checked
FROM (
    SELECT
        tbl_name,
        rule,
        run_at,
        ROW_NUMBER() OVER (
            PARTITION BY tbl_name
            ORDER BY run_at DESC
        ) AS rn
    FROM dq_checks
) latest
WHERE latest.rn = 1
COUNT(*) over the rows
Counts raw check rows, so a table checked twice in the same calendar month reports two months of coverage. It conflates how OFTEN a table was checked with how many months it was checked in.
COUNT(DISTINCT month)
Buckets each run_at to its year-month with STRFTIME and dedupes, so two checks in March count as one month. This is true month coverage, independent of check frequency.

Common follow-up questions