Candidate telemetry diagnostic, error autopsy, and step-by-step query construction walkthrough.
Live aggregated metrics across candidate sandbox attempts
25 solved
First attempt fail
Evaluated submissions
Median time to solve
Unlocked answer
Identify playlists with the broadest genre diversity for the "Discover" carousel.
| Column | Type |
|---|---|
| PlaylistId | INTEGER (PK) |
| Name | TEXT |
| Column | Type |
|---|---|
| PlaylistId | INTEGER (FK) |
| TrackId | INTEGER (FK) |
| Column | Type |
|---|---|
| TrackId | INTEGER (PK) |
| Name | TEXT NOT NULL |
| AlbumId | INTEGER (FK → Album) |
| MediaTypeId | INTEGER (FK → MediaType) |
| GenreId | INTEGER (FK → Genre) |
| Composer | TEXT |
| Milliseconds | INTEGER NOT NULL |
| Bytes | INTEGER |
| UnitPrice | NUMERIC(10,2) NOT NULL |
| Column | Type |
|---|---|
| GenreId | INTEGER (PK) |
| Name | TEXT |
PlaylistName, TrackCount, DistinctGenresYour query should return 5 rows with 3 columns: | playlistname | trackcount | distinctgenres | |--------------|------------|----------------| | Music | 3290 | 20 | | Music | 3290 | 20 | | 90’s Music | 1477 | 16 | | TV Shows | 213 | 5 | | TV Shows | 213 | 5 |
Putting aggregated filter conditions in the WHERE clause instead of HAVING, or including un-aggregated columns in SELECT without listing them in GROUP BY. Postgres strictly enforces that every non-aggregated projection column must appear in the GROUP BY expression.
Interviewers verify whether you understand the distinction between row-level filtering (WHERE) versus post-aggregation partition filtering (HAVING), as well as SQL standard group syntax.
Construct the solution logically from first principles to avoid typical edge case pitfalls.
Determine the attributes that define unique summary rows (e.g. Artist, Country, or Category).
GROUP BY entity_id, entity_name
Apply SUM, AVG, COUNT, or conditional aggregations over each bucket.
SELECT entity_name, COUNT(*) AS total_items, SUM(amount) AS total_revenue
Filter only the groups that satisfy minimum aggregate thresholds.
HAVING COUNT(*) >= 10 ORDER BY total_revenue DESC;
SELECT pl.Name AS PlaylistName,
COUNT(t.TrackId) AS TrackCount,
COUNT(DISTINCT g.GenreId) AS DistinctGenres
FROM Playlist pl
JOIN PlaylistTrack pt ON pl.PlaylistId = pt.PlaylistId
JOIN Track t ON pt.TrackId = t.TrackId
JOIN Genre g ON t.GenreId = g.GenreId
GROUP BY pl.PlaylistId, pl.Name
HAVING COUNT(DISTINCT g.GenreId) >= 5
ORDER BY DistinctGenres DESC, TrackCount DESC
LIMIT 10;Real code patterns candidates submit that fail the grading suite.
SELECT country, SUM(total) FROM Invoice WHERE COUNT(InvoiceId) > 10 GROUP BY country;
Three recurring syntax and semantic traps relevant to this problem domain.
WHERE operates on individual rows before grouping occurs. Aggregate functions like COUNT(), SUM(), AVG() can only be filtered in HAVING.
SELECT genre_id, COUNT(*) FROM tracks WHERE COUNT(*) > 50 GROUP BY genre_id; -- ❌ Syntax Error
SELECT genre_id, COUNT(*) FROM tracks GROUP BY genre_id HAVING COUNT(*) > 50; -- ✅ Correct
Every non-aggregated column in the SELECT list must appear in the GROUP BY clause.
SELECT artist_id, artist_name, COUNT(album_id) FROM albums GROUP BY artist_id; -- ❌ artist_name missing
SELECT artist_id, artist_name, COUNT(album_id) FROM albums GROUP BY artist_id, artist_name; -- ✅ Correct
COUNT(*) counts every row in the group including NULLs. COUNT(column) counts only non-null instances.
SELECT department, COUNT(commission_pct) FROM employees GROUP BY department; -- ❌ Ignores 0-commission staff
SELECT department, COUNT(*) FROM employees GROUP BY department; -- ✅ Accurate total count
Launch our in-browser coding environment. Run queries, view execution plans, and get instant comparative diff grading with no setup.