SQL QuestSQL Interview Questions › Subqueries & CTEs

Every Film by a Director Who Once Hit 8.5

EasyFreeQuerying BasicsSubqueries & CTEs

A retrospective is being programmed around directors with at least one great film. The brief: if a director ever reached 8.5, screen everything of theirs in the table.

Show exactly these 4 columns, in this order: title, year, rating, director. No aliases, no rounding. Include every movie whose director appears at least once in the table with a rating of 8.5 or higher. Order by director ascending, then year ascending, then title ascending.

The new idea: the subquery can read the same table you are already selecting from. (SELECT director FROM movies WHERE rating >= 8.5) returns a column of names — repeats and all, which IN does not mind — and the outer query keeps every row whose director is on that list.

Look at what comes back: films rated well below 8.5 are in the result, because the test is on the director, not on the row. That is exactly why a plain WHERE rating >= 8.5 cannot answer this question.

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: 36 rows — including their weaker films

Hint

The hint for this one spells out most of the query, so it stays in the editor. Reach for SELECT, Subquery, IN, and open the hint there if you stall.

Concepts

SELECT Subquery IN 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 · FreeFilter on an Average You Just ComputedEasy · FreeBetter Than Its Own GenreEasy · 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