This lesson on CASE WHEN is hands-on and example-driven. You will learn how to implement conditional logic (like if/else) directly within your SQL queries using the CASE statement. This allows you to categorize data, apply custom labels, or perform different calculations based on specific column values. This logic is executed row-by-row during the query execution.
What You'll Be Able To Do
- Write a CASE statement to assign descriptive labels based on numerical criteria.
- Apply multiple WHEN conditions within a single CASE block.
- Use BETWEEN within a WHEN clause to define an inclusive range.
- Calculate new column values using conditional arithmetic within the THEN clause.
- Alias the resulting column of a CASE statement using AS.
- Construct multiple independent CASE statements within one SELECT query.
Topics Covered in CASE WHEN
- Introduction to CASE (0:00 - 0:45) — The CASE statement adds if/else logic to a SELECT query.
- Basic CASE structure (0:45 - 1:45) — A simple CASE statement requires WHEN, THEN, and END keywords.
- Multiple WHEN conditions (1:45 - 3:15) — Multiple WHEN clauses can be chained to categorize data into different groups.
- Aliasing and BETWEEN (3:15 - 4:30) — The BETWEEN operator defines inclusive ranges, and AS aliases the final CASE column.
- Conditional Salary Raise (4:30 - 6:45) — A new scenario requires calculating different pay raises based on current salary thresholds.
- Calculation methods (6:45 - 8:15) — The raise calculation can be written using addition or simplified using multiplication.
- Handling edge cases (8:15 - 9:45) — Careful definition of conditions is necessary to avoid excluding boundary values like 50,000.
- Second CASE for Bonus (9:45 - 12:45) — A second, independent CASE statement is used to calculate a bonus based on Department ID.
- Summary and Use Cases (12:45 - 13:45) — CASE statements are powerful for labeling, categorization, and conditional calculations.
SQL Cheat Sheet
-
CASE WHEN <condition> THEN <result> END— Adds conditional logic (if/else) to a SELECT statementCASE WHEN age <= 30 THEN 'young' END -
WHEN <condition> THEN <result>— Defines a specific condition and the output if trueWHEN age BETWEEN 31 AND 50 THEN 'old' -
END AS <alias_name>— Terminates the CASE block and names the resulting columnCASE WHEN age > 50 THEN 'door' END AS age_bracket -
BETWEEN <val1> AND <val2>— Checks if a value falls inclusively within a rangeWHEN age BETWEEN 31 AND 50 THEN 'old' -
THEN salary * 1.05— Performs calculations based on the met conditionWHEN salary < 50000 THEN salary * 1.05 -
THEN salary + (salary * 0.05)— Alternative syntax for calculating percentage increaseWHEN salary < 50000 THEN salary + (salary * 0.05)
Comparison Table
| CASE Use | Syntax Goal | Example Result |
|---|---|---|
| Labeling/Categorization | Assigns a descriptive string. | THEN 'young' |
| Conditional Calculation | Applies arithmetic based on criteria. | THEN salary * 1.07 |
| Raise Calculation (Multiplication) | Shorter syntax for new total. | salary * 1.05 |
| Raise Calculation (Addition) | Explicitly shows added amount. | salary + (salary * 0.05) |
Common Pitfalls
- Mistake: Forgetting to close the CASE statement. Avoid: Always include the END keyword before aliasing the column.
- Mistake: Not aliasing the resulting CASE column. Avoid: Use AS column_name immediately after the END keyword.
- Mistake: Missing boundary conditions (e.g., exactly 50,000). Avoid: Ensure all possible values are covered by WHEN or an ELSE clause.
FAQs
- What happens if a value doesn't meet any WHEN condition? The resulting column value for that row will be NULL. To prevent this, use an ELSE clause to define a default result.
- Can I use an ELSE clause? Yes, ELSE defines the default result if all preceding WHEN conditions are false. This is useful for catching all remaining cases.
- Why did some employees not get a raise in the salary example? The defined conditions (< 50000 and > 50000) excluded those making exactly 50,000. Conditions must be carefully defined to cover all boundaries.
- Can I use CASE for calculations instead of just labeling? Yes, the THEN clause can contain arithmetic operations, allowing you to calculate new values like salary increases or bonuses.