Beginner LevelSQL & MySQL
How does CASE WHEN THEN ELSE END implement conditional logic in queries?
❓ Interview Prompt: Explain `CASE` expression syntax, Simple CASE vs Searched CASE, conditional logic in SELECT, WHERE, and ORDER BY.
The 'CASE' expression implements conditional 'IF-THEN-ELSE' branching logic inside SQL statements:
2 CASE Variants:
1
Searched 'CASE': Evaluates independent boolean expressions ('CASE WHEN age >= 18 THEN 'Adult' ELSE 'Minor' END').
2
Simple 'CASE': Compares an expression against target literal values ('CASE status WHEN 1 THEN 'Active' WHEN 2 THEN 'Pending' END').
Code Implementation Example
sql
SELECT
id,
total_amount,
CASE
WHEN total_amount >= 1000 THEN 'Platinum'
WHEN total_amount >= 500 THEN 'Gold'
ELSE 'Standard'
END AS customer_tier
FROM orders;💡Categorizing customer spend tiers using a CASE expression.
🎯 Interview Key Takeaway
Use CASE expressions to transform column outputs dynamically and embed conditional branching inside queries.
#SQL#CASE#Conditional Logic#Beginner
Back to all SQL & MySQL Questions→