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 Queries→→UPDATE 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 Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Set Operations: Union, Intersection, and Complement → Relational Algebra → SQL: SELECT Statement and Basic Queries → CASE WHEN: Conditional Expressions in SQL

Longest path: 72 steps · 330 total prerequisite topics

Prerequisites (1)

Leads To (1)