D8Loop

SQL

Problem library

71 SQL interview scenarios.

Need a refresh?Review SQL foundations ->
Foundations
25 min

Session retention breakdown

Write a query that traces returning-user retention by signup cohort and active week.

Window functions
Retention
Joins
Intermediate
35 min

Order anomaly audit

Identify duplicates, negative revenue rows, and broken order-state transitions.

CTEs
Debugging
Data quality
Advanced
45 min

Revenue waterfall reconstruction

Rebuild a multi-stage revenue view while preserving clear intermediate logic.

Attribution
Aggregations
Tradeoffs
Foundations
10 min

First look at the orders table

Inspect paid orders by selecting explicit columns, filtering, and ordering the newest first.

SELECT
WHERE
ORDER BY
Foundations
10 min

Count at the right grain

From item-grain rows, count item rows, distinct orders, and distinct products.

Grain
COUNT
DISTINCT
Foundations
15 min

Filter January's bigger orders

Combine a status set, a half-open date range, and an amount threshold with correct precedence.

WHERE
IN
Date ranges
Foundations
10 min

Bucket missing countries

Label missing countries as 'unknown' and keep only active customers.

NULL
COALESCE
IS NULL
Foundations
15 min

Label orders by value band

Bucket orders into value bands with CASE and return a deterministic ordering.

CASE
Aliases
Sorting
Foundations
10 min

Read the status distribution

Group orders by status to sanity-check the table the way an engineer would.

GROUP BY
COUNT
Validation
Foundations
12 min

Buyer country on each shipped order

Join shipped orders to their buyer to attach the buyer's country.

INNER JOIN
ON
Aliases
Foundations
12 min

Customers who never ordered

Use a LEFT JOIN and an IS NULL test to find customers with no orders.

LEFT JOIN
IS NULL
Anti-join
Foundations
12 min

Line items per order

Join orders to order_items and count the line items for each order that has any.

JOIN
GROUP BY
Grain
Foundations
15 min

Revenue by buyer country

Total order revenue grouped by buyer country, labelling a missing country as 'unknown'.

JOIN
GROUP BY
COALESCE
Foundations
12 min

Orders with no line items

Use a LEFT JOIN and IS NULL to find orders that have no rows in order_items.

LEFT JOIN
IS NULL
Referential check
Foundations
12 min

Customers with a shipped order

Use EXISTS to return customers who have at least one shipped order, without duplicates.

EXISTS
Semi-join
Intermediate
15 min

Paid order CTE decomposition

Use named CTE layers to filter paid or shipped orders and aggregate buyer revenue.

WITH
GROUP BY
Layering
Intermediate
12 min

Above average order amount

Use a scalar subquery to return orders above the overall average amount.

Subquery
AVG
Filtering
Intermediate
12 min

Customers with shipped orders

Use a correlated EXISTS subquery to return customers with shipped order activity.

EXISTS
Correlation
Intermediate
15 min

Buyer revenue derived table

Aggregate buyer revenue in a derived table, then filter the outer query.

FROM subquery
Aggregation
Intermediate
12 min

Clean order status CTE

Normalize order status buckets once, then count orders by the cleaned status.

CASE
CTE
COUNT
Intermediate
18 min

Readable revenue query

Compose a readable multi-CTE country revenue query with clear intermediate names.

CTEs
JOIN
Narration
Intermediate
15 min

Deduplicate order snapshots

Keep the newest snapshot per order_id using ROW_NUMBER.

ROW_NUMBER
Dedupe
CTE
Intermediate
15 min

Top buyers by revenue

Rank buyers by total revenue and return the leading buyers.

RANK
GROUP BY
Revenue
Intermediate
15 min

Days between buyer orders

Use LAG to calculate day gaps between each buyer order and the previous order.

LAG
Date math
Sequence
Intermediate
15 min

Running daily revenue

Return daily revenue with cumulative running revenue over time.

SUM OVER
Running total
Intermediate
15 min

First and latest order per buyer

Return first and latest order timestamps for each buyer.

ROW_NUMBER
First/last
Intermediate
20 min

Simple cohort retention slice

Create a compact retention slice by cohort day and activity day offset.

Cohorts
Retention
Offsets
Intermediate
12 min

Daily events by event time

Count events by the day they happened, not the day they arrived.

Timestamps
GROUP BY
Intermediate
15 min

Deterministic event sequence

Assign each user event a stable sequence number using event and ingestion time.

ROW_NUMBER
Events
Intermediate
15 min

Pacific daily active users

Bucket UTC events into a Pacific-style reporting day using a fixed offset.

Time zones
COUNT DISTINCT
Intermediate
12 min

Extract device from JSON

Extract device and campaign fields from JSON event payloads.

JSON
json_extract
Intermediate
15 min

Expand checkout items

Expand checkout item arrays into one row per event-item pair.

JSON arrays
json_each
Intermediate
15 min

Late arriving event audit

Count late-arriving events by the original event day.

Late data
Auditing
Advanced
20 min

Classify incremental load actions

Compare staged customer rows to the current dimension and classify insert, update, unchanged, and delete candidates.

Incremental loads
CASE
MERGE prep
Advanced
20 min

Dedupe staging before merge

Keep one deterministic staged row per customer key before a target merge.

ROW_NUMBER
Dedupe
Staging
Advanced
20 min

Build merge source delta

Return only staged rows that should affect the target, excluding unchanged records.

Delta logic
MERGE
CTEs
Advanced
15 min

Detect upsert key violations

Find null and duplicate unique keys that would make an upsert unsafe.

Upserts
Unique keys
HAVING
Advanced
25 min

SCD2 change boundaries

Detect changed dimension rows and compute close/open validity boundaries.

SCD2
History
Validity windows
Advanced
20 min

Current-row snapshot audit

Compare current snapshot rows against incoming source state and classify drift.

Snapshots
Auditing
Current rows
Advanced
20 min

Backfill replay window plan

Identify event partitions that need replay because late or corrected records arrived.

Backfills
Watermarks
Replay windows
Advanced
20 min

Load reconciliation summary

Produce insert, update, delete, and unchanged counts for a load audit report.

Reconciliation
Auditing
Counts
Foundations
10 min

Revenue and orders by status

Summarise order count and revenue per status with GROUP BY.

GROUP BY
COUNT
SUM
Foundations
15 min

Average order value by country

Average order amount per buyer country, labelling a missing country as 'unknown'.

JOIN
AVG
COALESCE
Foundations
12 min

High-value buyers

Return buyers whose total revenue is at least 500 using HAVING.

GROUP BY
HAVING
SUM
Foundations
10 min

Distinct buyers per status

Count the number of different buyers per status.

COUNT(DISTINCT)
GROUP BY
Foundations
15 min

Paid vs refunded per buyer

Per buyer, return paid revenue and refunded amount in one row using conditional aggregation.

SUM(CASE)
GROUP BY
Foundations
15 min

Paid rate by country

Per country, the share of orders that are paid, using decimal-safe division.

Ratios
Float cast
COALESCE
Advanced
12 min

Duplicate snapshot keys

Find every order_id stored more than once in order_snapshots, with its count.

Duplicates
GROUP BY
HAVING
Advanced
12 min

Customer null completeness

Report total customers, the count missing a country, and the missing rate.

Completeness
IS NULL
Safe ratio
Advanced
15 min

Referential integrity audit

Report orders without line items and orphan line items as two counted checks.

Referential
Anti-join
UNION ALL
Advanced
18 min

Reconcile order totals

Surface orders whose stored total disagrees with the sum of their line items.

Reconciliation
CTE
JOIN
Advanced
12 min

Flag outlier orders

Flag orders whose total is more than twice the average order value.

Anomalies
Scalar subquery
AVG
Advanced
20 min

Data-quality audit summary

Union four named checks into one pass/fail audit evidence table.

Auditing
UNION ALL
CASE
Advanced
18 min

Interview country revenue contract

Produce an interview-ready country revenue output with exact columns, ordering, and an unknown bucket.

Contracts
Aggregation
COALESCE
Advanced
20 min

Interview fanout-safe order revenue

Join orders to item totals without multiplying order-level revenue.

Fanout
Joins
Reconciliation
Advanced
18 min

Interview latest snapshot status

Keep the latest order snapshot per order with deterministic window logic.

ROW_NUMBER
Dedupe
Snapshots
Advanced
18 min

Interview top buyers with ties

Rank buyers by paid revenue and include tied ranks with stable ordering.

RANK
Ties
Revenue
Advanced
25 min

Interview cohort retention slice

Build a compact cohort retention slice from first order day and later activity days.

Cohorts
Dates
COUNT DISTINCT
Advanced
20 min

Interview event funnel sequence

Find users whose checkout happened after a view using event-time ordering.

Funnels
Events
Sequence
Advanced
18 min

Interview JSON array expansion

Extract JSON attributes and expand checkout item arrays into item-grain rows.

JSON
Arrays
Grain
Advanced
25 min

Interview incremental action plan

Classify deduped staged customer rows as insert, update, delete, or unchanged.

Incremental
Dedupe
CASE
Advanced
20 min

Interview SCD2 current-row check

Detect current SCD2 rows whose staged attributes require a close/open history change.

SCD2
Auditing
History
Advanced
18 min

Interview late-data replay check

Identify event partitions that need replay because of late or corrected records.

Late data
Replay
Events
Advanced
18 min

Interview query-cost triage

Flag expensive, wide, and full-scan query runs from warehouse metadata.

Warehouse cost
Full scans
Triage
Advanced
25 min

Interview audit union capstone

Union multiple named quality checks into one interview-ready audit evidence table.

Auditing
UNION ALL
Quality
Advanced
15 min

Partition pruning scan

Show partitions and MB scanned for a date range against the table totals.

Pruning
Scalar subquery
SUM
Advanced
12 min

Most expensive queries

Rank the query history by bytes scanned and return the three most expensive.

Cost
RANK
LIMIT
Advanced
12 min

Full-scan detector

Find queries that scanned every partition of the table (no pruning).

Plans
Scalar subquery
Audit
Advanced
15 min

Scan cost by analyst

Sum bytes scanned per analyst and convert it to an estimated dollar cost.

Cost
GROUP BY
printf
Advanced
12 min

Hot partition skew

Flag each partition as hot or normal versus the average row count.

Clustering
CASE
AVG
Advanced
15 min

Wide-scan efficiency

Rank queries by bytes scanned per partition to expose wide SELECT-* scans.

Partitions
Safe ratio
printf