SQL Quest › SQL Interview Questions › Subqueries & CTEs
Better Than Its Own Genre
A 7.5 horror film and a 7.5 crime film are not the same achievement. Crime averages 8.48 in this table; horror averages 7.35. The programming desk wants every film that beats the average of its own genre — each row judged against its own crowd, not against the table.
Show exactly these 3 columns, in this order: title, genre, rating. No aliases, no rounding. Order by genre ascending, then rating descending, then title ascending.
The new idea, and the only one on this card: the inner query reads a value from the outer row. Give the outer table an alias, give the inner one a different alias, and join them in the inner WHERE — WHERE m2.genre = m.genre. That single line is what makes it *correlated*: the subquery no longer runs once, it runs once per row, and each row gets its own average.
Compare it with Movies Rated Above the Average, which used one average for everything. That query returns 61 rows, this one returns 52, and they are not nested lists: 5 films are above their own genre's average but below the table's, and 14 are above the table's but not above their own genre's. Different questions, different answers.
One genre returns nothing at all. Mystery has a single film, so its average *is* that film's rating — and nothing is strictly greater than itself.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
movies
| id | title | year | genre | rating | votes | revenue_millions | runtime | director |
|---|---|---|---|---|---|---|---|---|
| 1 | Guardians of the Galaxy | 2014 | Action | 8.1 | 757074 | 333.13 | 121 | James Gunn |
| 2 | Prometheus | 2012 | Adventure | 7 | 485820 | 126.46 | 124 | Ridley Scott |
| 3 | Split | 2016 | Horror | 7.3 | 157606 | 138.12 | 117 | M. Night Shyamalan |
Expected output: 52 rows — each film that beat its own genre's average
Hint
SELECT, Subquery, Correlated Subquery, and open the hint there if you stall.Concepts
SELECT Subquery Correlated Subquery WHERE
Practise the topic: SQL practice questions · CTE practice
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test