SQL QuestSQL Interview Questions › Conditional Logic

Introduction to CASE WHEN

MediumFreeQuerying BasicsConditional LogicAggregation & Grouping

Categorize movies into eras: 'Classic' (year < 2000), 'Modern' (2000-2009), 'Recent' (2010+). For each era, show era, movie_count, avg_rating (2 dec), and avg_revenue (2 dec). Sort by era. CASE WHEN is SQL's if/else — it creates new categories from existing data.

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: 3 rows, one per era with aggregated stats

Hint

CASE WHEN year < 2000 THEN 'Classic' WHEN year < 2010 THEN 'Modern' ELSE 'Recent' END AS era. Put this in both SELECT and GROUP BY.

Concepts

SELECT CASE GROUP BY Aggregation

Practise the topic: SQL practice questions · CASE WHEN practice · GROUP BY exercises

In these company practice sets

Snowflake

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

Related questions

Movie Rating Tier BreakdownMedium · FreeGenre Financial ReportMedium · FreeDepartment Compensation ReportMedium · FreePivot: Order Status by CountryMedium · FreeGenre Box Office ReportMedium · FreeFare Imputation AnalysisMedium · 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