SQL Quest › SQL Interview Questions › Aggregation & Grouping

Genre-Spanning Directors (HAVING)

MediumFreeQuerying BasicsAggregation & Grouping

A film studies report wants directors who have worked in more than one genre — a signal of stylistic range. Show director and genre_count (COUNT(DISTINCT genre)). Filter with HAVING COUNT(DISTINCT genre) > 1. Order by genre_count descending, then director ascending.

The DISTINCT inside the HAVING aggregate is the whole trick: a director with five Action movies has COUNT(*) = 5 but COUNT(DISTINCT genre) = 1, and we only want the second number.

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". Free accounts get 10 free challenge solves, and this can be one of them.

Schema

movies

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

Expected output: Only Christopher Nolan appears

Hint

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

Concepts

SELECT GROUP BY HAVING COUNT DISTINCT

Practise the topic: SQL practice questions · GROUP BY exercises

Read the concept: SQL GROUP BY tutorial

In these company practice sets

LinkedIn

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Month-over-Month Customer GrowthMedium · FreeDay-of-Week Order Pattern (strftime)Medium · FreeActive Spender Cohort (HAVING)Medium · FreeFailure Clustering — State + YearMedium · FreeHow Long Has Each Card Been Active?Medium · FreeMonthly Active UsersMedium · 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