CASE Statement
Add if-else logic inside your SQL query
✅ What You Will Learn
The CASE statement adds conditional logic to SQL — similar to IF/ELSE in other languages. It evaluates conditions in order and returns a value for the first condition that is true.
CASE is widely used in real analytics for creating category labels, bucketing numbers into ranges, replacing codes with readable text, and building conditional aggregations.
📋 The orders table — CASE adds a computed label column
| order_id | customer_name | product | amount |
|---|---|---|---|
| 1001 | Rahul Sharma | Laptop | 45000 |
| 1002 | Priya Verma | Mobile Phone | 18000 |
| 1003 | Amit Kumar | Headphones | 3500 |
| 1004 | Sneha Kapoor | Laptop | 52000 |
| 1005 | Vikram Singh | Tablet | 28000 |
Syntax
Examples
📌 Key Points to Remember
- ✓CASE is evaluated top to bottom — first matching WHEN wins
- ✓ELSE is optional but recommended to handle unexpected values
- ✓CASE can be used in SELECT, WHERE, ORDER BY, and inside aggregates
- ✓CASE inside COUNT or SUM creates conditional aggregations
- ✓Always end CASE with END and give it an alias
🏢 Real-World Application
CASE is how you add business logic directly into SQL. Salary bands, customer segments, discount tiers, order priorities — all of these are implemented with CASE. A finance analyst uses CASE to label transactions as "Revenue", "Refund", or "Adjustment" based on transaction type codes. A marketing analyst uses CASE to segment customers into "High Value", "Medium Value", "Low Value" based on total spend. CASE inside SUM() — called conditional aggregation — lets you pivot data without needing a PIVOT operator: SUM(CASE WHEN category = 'Electronics' THEN amount ELSE 0 END) AS electronics_revenue.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What is the CASE statement in SQL?
CASE is a conditional expression in SQL that returns different values based on conditions. It works like IF-ELSE logic. CASE WHEN condition THEN result WHEN condition2 THEN result2 ELSE default END.
What is the difference between simple CASE and searched CASE?
Simple CASE compares one expression to multiple values: CASE status WHEN 1 THEN 'Active' WHEN 0 THEN 'Inactive' END. Searched CASE evaluates boolean conditions: CASE WHEN amount > 10000 THEN 'High' ELSE 'Low' END. Searched CASE is more flexible.
Can CASE be used in a WHERE clause?
Yes. CASE can be used anywhere an expression is valid, including WHERE: WHERE (CASE WHEN type = 'A' THEN amount ELSE 0 END) > 1000. However, this is often clearer rewritten as a normal condition.
What is conditional aggregation using CASE?
Conditional aggregation uses CASE inside SUM or COUNT to aggregate only specific rows. Example: SUM(CASE WHEN month = 'January' THEN amount ELSE 0 END) AS jan_revenue. This lets you pivot months into columns without a PIVOT operator.
Can I nest CASE inside another CASE in SQL?
Yes, CASE can be nested inside THEN or ELSE. However, deeply nested CASE becomes hard to read. Consider using a CTE or subquery to break complex logic into steps.
✏️ Practice Exercise
Write a query that adds a "delivery_priority" column: orders above ₹40,000 get "Express", orders between ₹10,000 and ₹40,000 get "Standard", and below ₹10,000 get "Economy".
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.