The CASE statement is one of the most powerful and flexible tools in SQL. It allows conditional logic anywhere in your queryβ€”SELECT, WHERE, GROUP BY, ORDER BY, and especially within aggregations.


βœ… General Syntax of CASE

CASE 
  WHEN condition1 THEN result1
  WHEN condition2 THEN result2
  ...
  ELSE default_result
END

πŸ” Use Cases of CASE in Different Clauses

βœ… 1. In SELECT β€” Conditional column values

SELECT name,
       salary,
       CASE 
         WHEN salary > 100000 THEN 'High'
         WHEN salary > 50000 THEN 'Medium'
         ELSE 'Low'
       END AS salary_band
FROM employees;

πŸ“Œ Labeling or categorizing data based on conditions.


βœ… 2. In WHERE β€” Conditional filtering (less common)

SELECT * 
FROM orders
WHERE 
  CASE 
    WHEN status = 'pending' THEN amount > 100
    ELSE TRUE
  END;

πŸ“Œ Used sparingly, but can apply complex filters dynamically.


βœ… 3. In ORDER BY β€” Conditional sort logic

SELECT name, status
FROM tasks
ORDER BY 
  CASE 
    WHEN status = 'urgent' THEN 1
    WHEN status = 'high' THEN 2
    ELSE 3
  END;

πŸ“Œ Custom ordering of categorical values.


βœ… 4. In Aggregation β€” Conditional counting or summing

a. Count by condition:

SELECT 
  department,
  COUNT(*) AS total_employees,
  COUNT(CASE WHEN gender = 'Male' THEN 1 END) AS male_count,
  COUNT(CASE WHEN gender = 'Female' THEN 1 END) AS female_count
FROM employees
GROUP BY department;

b. Sum conditionally:

SELECT 
  department,
  SUM(CASE WHEN salary > 50000 THEN salary ELSE 0 END) AS high_earner_total
FROM employees
GROUP BY department;

c. Multiple category aggregation:

SELECT 
  product_type,
  SUM(CASE WHEN status = 'delivered' THEN 1 ELSE 0 END) AS delivered_count,
  SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled_count
FROM orders
GROUP BY product_type;

βœ… 5. In GROUP BY β€” Grouping based on a derived value

SELECT 
  CASE 
    WHEN age < 18 THEN 'Minor'
    WHEN age BETWEEN 18 AND 60 THEN 'Adult'
    ELSE 'Senior'
  END AS age_group,
  COUNT(*) 
FROM users
GROUP BY 
  CASE 
    WHEN age < 18 THEN 'Minor'
    WHEN age BETWEEN 18 AND 60 THEN 'Adult'
    ELSE 'Senior'
  END;

πŸ“Œ You must repeat the CASE in GROUP BY unless you use a subquery or CTE.


βœ… 6. Nested CASE

SELECT name,
  CASE 
    WHEN country = 'India' THEN
      CASE 
        WHEN city = 'Delhi' THEN 'North'
        WHEN city = 'Mumbai' THEN 'West'
        ELSE 'Other'
      END
    ELSE 'International'
  END AS region
FROM users;

πŸ“Œ Useful for multi-layered logic.


🧠 Summary Table

LocationExample Use Case
SELECTCategorize values, label based on logic
WHEREConditional filters
ORDER BYCustom sort order
GROUP BYGroup by derived buckets (like age, band)
AggregationConditional SUM, COUNT, AVG etc.

Pages: 1 2 3


Discover more from HintsToday

Subscribe to get the latest posts sent to your email.

Posted in

Leave a Reply

Discover more from HintsToday

Subscribe now to keep reading and get access to the full archive.

Continue reading

Subscribe