A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

CASE WHEN: Conditional Expressions in SQL

College Depth 71 in the knowledge graph I know this Set as goal
1topic build on this
330prerequisites beneath it
See this on the map →
SQL: SELECT Statement and Basic QueriesUPDATE with JOINs: Conditional Updates
sql conditional-logic data-transformation

Core Idea

CASE WHEN allows conditional branching in SELECT, UPDATE, and other SQL statements, returning different values based on evaluated conditions. It provides SQL the ability to perform if-then-else logic.

How It's Best Learned

Begin with simple two-branch CASE expressions, then progress to multi-condition CASE with ELSE clauses and nested CASE statements.

Common Misconceptions

CASE evaluates conditions sequentially and stops at the first match—later conditions are not evaluated. The ELSE clause is optional and defaults to NULL if no condition matches.

Explainer

You already know how to select and filter data with SELECT and WHERE. But sometimes you need to transform values conditionally — not just retrieve them, but reclassify, bucket, or label them based on rules. CASE WHEN gives SQL the equivalent of if-then-else logic, letting you produce new computed values inline within a query.

The basic structure reads almost like English: `CASE WHEN condition THEN result WHEN condition THEN result ELSE default END`. For example, if you have a table of exam scores and want to assign letter grades, you would write `CASE WHEN score >= 90 THEN 'A' WHEN score >= 80 THEN 'B' WHEN score >= 70 THEN 'C' ELSE 'F' END AS grade`. The database evaluates conditions top to bottom and returns the result for the first match. This sequential evaluation matters — if you accidentally put `score >= 70` before `score >= 90`, every score above 70 would get a 'C' because the engine stops at the first true condition.

CASE expressions are not limited to SELECT lists. You can use them inside ORDER BY to create custom sort orders (sort active users before inactive ones), inside GROUP BY to bucket rows into categories before aggregating, and inside UPDATE statements to conditionally change values. A particularly powerful pattern is combining CASE with aggregate functions: `SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END)` gives you a conditional sum, effectively pivoting rows into columns without restructuring your query.

One subtlety to watch: if no condition matches and you omit the ELSE clause, the result is NULL — not an error, not zero, just NULL. This silent default catches people off guard when they use CASE inside arithmetic expressions, since any operation involving NULL produces NULL. Adding an explicit ELSE clause, even when you think every case is covered, is a defensive habit that prevents unexpected NULLs from propagating through your results.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesSet Operations: Union, Intersection, and ComplementRelational AlgebraSQL: SELECT Statement and Basic QueriesCASE WHEN: Conditional Expressions in SQL

Longest path: 72 steps · 330 total prerequisite topics

Prerequisites (1)

Leads To (1)