How to write sql code with CASE
- User
How to write sql code with CASE
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
How to write sql code with CASE
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The CASE statement in SQL allows you to perform conditional logic directly within your queries. It works similarly to IF-THEN-ELSE statements in other programming languages. Below is a comprehensive guide on how to use CASE effectively:
There are two forms of CASE:
Simple CASE (compares a single value):
CASE expression
WHEN value1 THEN result1
WHEN value2 THEN result2
...
ELSE default_result
END
Searched CASE (evaluates multiple conditions):
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END
ELSE is optional (returns NULL if omitted and no conditions match).END to close the CASE block.AS for clarity.Label employees based on salary:
SELECT
name,
salary,
CASE
WHEN salary > 100000 THEN 'Executive'
WHEN salary > 75000 THEN 'Manager'
WHEN salary > 50000 THEN 'Associate'
ELSE 'Entry-Level'
END AS role_label
FROM employees;
Count active vs. inactive users:
SELECT
COUNT(CASE WHEN is_active = 1 THEN id END) AS active_users,
COUNT(CASE WHEN is_active = 0 THEN id END) AS inactive_users
FROM users;
Prioritize sorting by last_name for Admins, registration_date otherwise:
SELECT id, first_name, last_name, registration_date
FROM users
ORDER BY
CASE
WHEN user_type = 'Admin' THEN last_name
ELSE registration_date
END;
Convert NULL discounts to 0 for filtering:
SELECT order_id, discount
FROM orders
WHERE
CASE
WHEN discount IS NULL THEN 0
ELSE discount
END > 0.1;
Apply tiered discounts:
UPDATE products
SET price =
CASE
WHEN price > 100 THEN price * 0.8 -- 20% discount
WHEN price > 50 THEN price * 0.9 -- 10% discount
ELSE price * 0.95 -- 5% discount
END;
Set a default status if missing:
INSERT INTO orders (order_id, status)
VALUES (
123,
CASE
WHEN @input_status IS NOT NULL THEN @input_status
ELSE 'Pending'
END
);
Pivot Data: Combine CASE with aggregation to create cross-tabs:
SELECT
department,
COUNT(CASE WHEN status = 'Active' THEN 1 END) AS active_count,
COUNT(CASE WHEN status = 'Inactive' THEN 1 END) AS inactive_count
FROM employees
GROUP BY department;
Nested CASE:
CASE
WHEN condition1 THEN
CASE
WHEN sub_condition THEN result_a
ELSE result_b
END
ELSE result_c
END
THEN/ELSE results have compatible types.CASE in WHERE/JOIN clauses can slow queries. Test alternatives.Example output for Example 1:
| name | salary | role_label |
|---|---|---|
| Alice | 120000 | Executive |
| Bob | 80000 | Manager |
| Carol | 60000 | Associate |
| Dave | 45000 | Entry-Level |
By mastering CASE, you add powerful conditional logic to your SQL toolkit, enabling dynamic queries and data transformations. Practice with real datasets to explore its full potential!