Ad space (banner)
🗄️SQL Lessons
Lesson 20 / 50

Conditional Values with CASE

This lesson covers the CASE expression, so a query can branch on a condition and produce a different value. It's for anyone searching "SQL CASE expression tutorial."

CASE WHEN condition THEN value ... END lets a query swap in different output values based on a condition — SQL's equivalent of an if-statement, usable right inside a query.

The example uses CASE WHEN score >= 80 THEN 'A' WHEN score >= 70 THEN 'B' ELSE 'C' END to convert scores into letter grades. Conditions are checked top-to-bottom, and the first match wins — very similar to if/elif in most programming languages.

A common early mistake is forgetting the closing END — every CASE must be closed with one, or it's a syntax error. Skip ELSE and any row that matches nothing becomes NULL instead. This is an extremely common pattern for turning scores into letter grades or numeric status codes into human-readable labels in a summary report, and once you know it, it opens up a lot of expressive power.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)