SQL QuestSQL Interview Questions › Subqueries & CTEs

Better Than Its Own Genre

EasyFreeQuerying BasicsSubqueries & CTEs

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 WHEREWHERE 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

idtitleyeargenreratingvotesrevenue_millionsruntimedirector
1Guardians of the Galaxy2014Action8.1757074333.13121James Gunn
2Prometheus2012Adventure7485820126.46124Ridley Scott
3Split2016Horror7.3157606138.12117M. Night Shyamalan

Expected output: 52 rows — each film that beat its own genre's average

Hint

The hint for this one spells out most of the query, so it stays in the editor. Reach for 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

A Subquery That Returns One ValueEasy · FreeA Subquery That Returns a Set (IN)Easy · FreeGive a Query a Name (WITH)Easy · FreeMovies Rated Above the AverageEasy · FreeEvery Film by a Director Who Once Hit 8.5Easy · FreeFilter on an Average You Just ComputedEasy · Free

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