SQL QuestSQL Interview Questions › Conditional Logic

Conditional Counting with CASE

MediumFreeQuerying BasicsConditional LogicAggregation & Grouping

For each movie genre, count how many movies are hits (revenue > $100M), moderate ($10M-$100M), and flops (< $10M). Show genre, hits, moderate, flops, and total (excluding NULL revenue). Sort by hits descending. This CASE-inside-SUM pattern is the foundation of pivot tables in SQL.

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: Action | 5 | 8 | 2 | 15

Hint

SUM(CASE WHEN revenue_millions > 100 THEN 1 ELSE 0 END) AS hits. Each CASE acts as a conditional counter inside the aggregate.

Concepts

SELECT CASE Aggregation GROUP BY BETWEEN Conditional Aggregation

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

In these company practice sets

Amazon · Databricks · Ramp · Snowflake · Spotify · Microsoft

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