Guided Notes

CASE, NULL Functions & Operators

Conditional values with CASE and IF, handling NULLs with IFNULL and COALESCE, comments, and MySQL's operators.

CASE

Slide 1 / 5

CASE returns a value based on conditions — SQL's if/else:

SELECT name, price, CASE WHEN price < 10 THEN 'budget' WHEN price < 40 THEN 'standard' ELSE 'premium' END AS price_band FROM products;

The first true WHEN wins. Without ELSE, unmatched rows get NULL. CASE works in SELECT, WHERE, ORDER BY, GROUP BY, and inside aggregates: SUM(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END).

← Subqueries, EXISTS, ANY & ALL