CASE & Conditional Logic
CASE & Conditional Logic
Section titled “CASE & Conditional Logic”SQL has several ways to handle conditional logic — think of them as “if/then/else” for your queries.
Real-World Analogy
Section titled “Real-World Analogy”Think of a school report card:
- CASE WHEN: “If score >= 90 → A, if score >= 80 → B, else → C”
- COALESCE: “If the student has a nickname, use it; otherwise use their full name”
- NULLIF: “If their old and new addresses are the same, don’t bother updating”
CASE WHEN — The SQL If/Else
Section titled “CASE WHEN — The SQL If/Else”SELECT name, salary, CASE WHEN salary >= 90000 THEN 'Senior' WHEN salary >= 70000 THEN 'Mid-level' WHEN salary >= 50000 THEN 'Junior' ELSE 'Entry' END AS levelFROM employees;┌────────┬────────┬───────────┐│ name │ salary │ level │├────────┼────────┼───────────┤│ Alice │ 95000 │ Senior ││ Bob │ 75000 │ Mid-level ││ Carol │ 55000 │ Junior ││ David │ 35000 │ Entry │└────────┴────────┴───────────┘CASE with GROUP BY
Section titled “CASE with GROUP BY”SELECT CASE WHEN salary >= 90000 THEN 'High' WHEN salary >= 50000 THEN 'Medium' ELSE 'Low' END AS salary_range, COUNT(*) AS employee_count, AVG(salary) AS avg_salaryFROM employeesGROUP BY salary_rangeORDER BY avg_salary DESC;CASE in WHERE, ORDER BY, and UPDATE
Section titled “CASE in WHERE, ORDER BY, and UPDATE”-- CASE in WHERESELECT name, salaryFROM employeesWHERE CASE WHEN department = 'Engineering' THEN salary > 80000 WHEN department = 'Marketing' THEN salary > 60000 ELSE salary > 50000 END;
-- CASE in ORDER BY (custom sort order)SELECT name, statusFROM tasksORDER BY CASE status WHEN 'critical' THEN 1 WHEN 'high' THEN 2 WHEN 'medium' THEN 3 WHEN 'low' THEN 4 ELSE 5 END;
-- CASE in UPDATEUPDATE employeesSET bonus = CASE WHEN salary > 100000 THEN salary * 0.20 WHEN salary > 70000 THEN salary * 0.15 ELSE salary * 0.10END;CASE with Multiple Conditions
Section titled “CASE with Multiple Conditions”SELECT name, department, salary, CASE WHEN department = 'Engineering' AND salary > 80000 THEN 'Star Engineer' WHEN department = 'Engineering' THEN 'Engineer' WHEN department = 'Marketing' AND salary > 70000 THEN 'Senior Marketer' WHEN department = 'Marketing' THEN 'Marketer' ELSE 'Other' END AS role_labelFROM employees;Simple CASE vs Searched CASE
Section titled “Simple CASE vs Searched CASE”-- Simple CASE (checks equality)CASE department WHEN 'Engineering' THEN 'Tech' WHEN 'Marketing' THEN 'Growth' ELSE 'Other'END
-- Searched CASE (checks any condition)CASE WHEN salary > 100000 THEN 'High' WHEN salary > 50000 THEN 'Medium' ELSE 'Low'ENDCOALESCE — First Non-NULL Value
Section titled “COALESCE — First Non-NULL Value”SELECT name, COALESCE(nickname, first_name, 'Unknown') AS display_nameFROM employees;-- If nickname exists → use it-- If not, try first_name-- If that's also NULL → use 'Unknown'NULLIF — Return NULL if Two Values Equal
Section titled “NULLIF — Return NULL if Two Values Equal”SELECT name, salary, NULLIF(salary, 0) AS non_zero_salaryFROM employees;-- If salary = 0, returns NULL (to avoid division by zero later)IFNULL — Simpler Version of COALESCE (MySQL)
Section titled “IFNULL — Simpler Version of COALESCE (MySQL)”SELECT name, IFNULL(nickname, first_name) AS display_nameFROM employees;-- Same as COALESCE with 2 argumentsPractical Examples
Section titled “Practical Examples”-- 1. Categorize by date rangeSELECT name, hire_date, CASE WHEN hire_date < '2020-01-01' THEN 'Veteran' WHEN hire_date < '2022-01-01' THEN 'Senior' WHEN hire_date < '2024-01-01' THEN 'Junior' ELSE 'New Hire' END AS tenureFROM employees;
-- 2. Pivot table with CASESELECT department, SUM(CASE WHEN gender = 'M' THEN 1 ELSE 0 END) AS male_count, SUM(CASE WHEN gender = 'F' THEN 1 ELSE 0 END) AS female_countFROM employeesGROUP BY department;
-- 3. Safe division (avoid divide by zero)SELECT name, revenue / NULLIF(employees, 0) AS revenue_per_employeeFROM departments;
-- 4. Conditional aggregationSELECT SUM(CASE WHEN status = 'completed' THEN amount ELSE 0 END) AS completed_revenue, SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS pending_revenueFROM orders;In Simple Words
Section titled “In Simple Words”- CASE WHEN is SQL’s if/then/else — use it to create conditional columns
- COALESCE returns the first non-NULL value — great for fallback defaults
- NULLIF returns NULL when two values are equal — useful for safe division
- CASE in ORDER BY lets you define a custom sort order (like priority: critical > high > medium)
- CASE in GROUP BY is powerful for creating custom buckets/ranges