Candidate telemetry diagnostic, error autopsy, and step-by-step query construction walkthrough.
Live aggregated metrics across candidate sandbox attempts
12 solved
First attempt fail
Evaluated submissions
Median time to solve
Unlocked answer
Compute cohort retention: for each customer signup-month (first invoice), how many of that cohort were active in month 0, month 1, month 2, and month 3 thereafter.
| Column | Type |
|---|---|
| CustomerId | INTEGER (PK) |
| FirstName | TEXT NOT NULL |
| LastName | TEXT NOT NULL |
| Company | TEXT |
| Country | TEXT |
| TEXT NOT NULL | |
| SupportRepId | INTEGER (FK → Employee) |
| Column | Type |
|---|---|
| InvoiceId | INTEGER (PK) |
| CustomerId | INTEGER (FK) |
| InvoiceDate | TIMESTAMP NOT NULL |
| BillingCountry | TEXT |
| Total | NUMERIC(10,2) NOT NULL |
YYYY-MM)CohortMonth, CohortSize, M0_Active, M1_Active, M2_Active, M3_ActiveYour query should return 8 rows with 6 columns: | cohortmonth | cohortsize | m0_active | m1_active | m2_active | m3_active | |-------------|------------|-----------|-----------|-----------|-----------| | 2009-01 | 6 | 6 | 1 | 0 | 1 | | 2009-02 | 6 | 6 | 1 | 0 | 2 | | 2009-03 | 6 | 6 | 1 | 0 | 2 | | 2009-04 | 5 | 5 | 1 | 0 | 2 | | 2009-05 | 4 | 4 | 1 | 0 | 1 | | ... | ... | ... | ... | ... | ... |
Attempting to filter window function output directly inside the WHERE clause (e.g., WHERE ROW_NUMBER() OVER (...) <= 3). Because SQL executes WHERE before evaluating window functions, this raises a syntax error or produces invalid groupings. The calculation must be staged in a CTE or subquery first.
Interviewers use this question to verify whether you understand the exact SQL execution order (FROM -> WHERE -> GROUP BY -> HAVING -> WINDOW -> SELECT -> ORDER BY), how to choose correctly between ROW_NUMBER, RANK, and DENSE_RANK when handling ties, and how to partition datasets without collapsing rows.
Construct the solution logically from first principles to avoid typical edge case pitfalls.
Determine whether the ranking or running total resets per customer, department, or genre (PARTITION BY), or spans the entire table.
OVER (PARTITION BY <group_col> ORDER BY <order_col> DESC)
Write a WITH clause to calculate the window metric alongside the base columns, ensuring all join and filter conditions are applied.
WITH RankedData AS (
WITH first_purchase AS (
SELECT CustomerId, DATE_TRUNC('month', MIN(InvoiceDate)) AS cohort_month
FROM Invoice
GRO...
)Select from the CTE and apply the outer predicate (e.g., WHERE rnk = 1 or WHERE rnk <= N) to extract the final result set.
SELECT <columns> FROM RankedData WHERE rnk = 1 ORDER BY <columns>;
WITH first_purchase AS (
SELECT CustomerId, DATE_TRUNC('month', MIN(InvoiceDate)) AS cohort_month
FROM Invoice
GROUP BY CustomerId
),
activity AS (
SELECT fp.cohort_month,
i.CustomerId,
(EXTRACT(YEAR FROM i.InvoiceDate) * 12 + EXTRACT(MONTH FROM i.InvoiceDate))::int
- (EXTRACT(YEAR FROM fp.cohort_month) * 12 + EXTRACT(MONTH FROM fp.cohort_month))::int AS month_offset
FROM first_purchase fp
JOIN Invoice i ON i.CustomerId = fp.CustomerId
),
sizes AS (
SELECT cohort_month, COUNT(*) AS CohortSize
FROM first_purchase
GROUP BY cohort_month
)
SELECT TO_CHAR(a.cohort_month, 'YYYY-MM') AS CohortMonth,
s.CohortSize,
COUNT(DISTINCT CASE WHEN a.month_offset = 0 THEN a.CustomerId END) AS M0_Active,
COUNT(DISTINCT CASE WHEN a.month_offset = 1 THEN a.CustomerId END) AS M1_Active,
COUNT(DISTINCT CASE WHEN a.month_offset = 2 THEN a.CustomerId END) AS M2_Active,
COUNT(DISTINCT CASE WHEN a.month_offset = 3 THEN a.CustomerId END) AS M3_Active
FROM activity a
JOIN sizes s ON a.cohort_month = s.cohort_month
GROUP BY a.cohort_month, s.CohortSize
HAVING s.CohortSize >= 3
ORDER BY a.cohort_month ASC;Real code patterns candidates submit that fail the grading suite.
SELECT * FROM table_name WHERE ROW_NUMBER() OVER (ORDER BY amount DESC) <= 5;
SELECT department_id, employee_id, salary,
RANK() OVER (ORDER BY salary DESC) as rank
FROM employees;Three recurring syntax and semantic traps relevant to this problem domain.
Window functions cannot appear in WHERE or HAVING clauses. Filtering on a rank or running total requires wrapping the query in a CTE or subquery.
SELECT *, RANK() OVER (ORDER BY points DESC) as rnk FROM candidates WHERE RANK() OVER (ORDER BY points DESC) <= 5; -- ❌ Syntax Error
WITH Ranked AS ( SELECT *, RANK() OVER (ORDER BY points DESC) as rnk FROM candidates ) SELECT * FROM Ranked WHERE rnk <= 5; -- ✅ Correct
Using RANK() skips rank positions on ties (1, 2, 2, 4), whereas DENSE_RANK() retains consecutive integers (1, 2, 2, 3). Using ROW_NUMBER() arbitrarily breaks ties.
SELECT name, RANK() OVER (ORDER BY score DESC) as rnk ... -- ❌ Might miss 3rd rank if 2nd ties
SELECT name, DENSE_RANK() OVER (ORDER BY score DESC) as rnk ... -- ✅ Guaranteed continuous ranks
Forgetting the PARTITION BY clause causes ranking or rolling metrics to compute across the entire dataset rather than resetting per group/customer.
ROW_NUMBER() OVER (ORDER BY sale_date DESC) -- ❌ Global row number
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY sale_date DESC) -- ✅ Per-customer rank
Launch our in-browser coding environment. Run queries, view execution plans, and get instant comparative diff grading with no setup.