What if your database could instantly tell you the story behind your numbers without extra work?
Why CASE in SELECT for computed columns in SQL? - Purpose & Use Cases
Imagine you have a list of orders with their amounts, and you want to label each order as 'Small', 'Medium', or 'Large' based on its amount. Doing this by hand means checking each order one by one and writing down the label manually.
Manually labeling each order is slow and mistakes happen easily. If you have hundreds or thousands of orders, it becomes impossible to keep track without errors. Also, updating labels when rules change means redoing all the work.
The CASE statement in SELECT lets you create these labels automatically inside your query. It checks each order's amount and assigns the right label on the fly, saving time and avoiding mistakes.
For each order: if amount < 100 then 'Small' else if amount < 500 then 'Medium' else 'Large'
SELECT order_id, amount, CASE WHEN amount < 100 THEN 'Small' WHEN amount < 500 THEN 'Medium' ELSE 'Large' END AS size_label FROM orders;
You can instantly classify and transform data in your queries, making reports and decisions faster and more accurate.
A store manager can quickly see which sales are small, medium, or large without extra work, helping to focus on important orders.
Manual labeling is slow and error-prone.
CASE in SELECT automates conditional labeling inside queries.
This makes data classification fast, reliable, and easy to update.
Practice
CASE statement do in a SQL SELECT query?Solution
Step 1: Understand the purpose of CASE in SELECT
The CASE statement is used to create new columns by checking conditions and assigning values accordingly.Step 2: Compare with other SQL operations
Deleting rows is done with DELETE, joining tables with JOIN, and sorting with ORDER BY, not CASE.Final Answer:
Creates a new column with values based on conditions -> Option AQuick Check:
CASE = computed column based on conditions [OK]
- Thinking CASE deletes rows
- Confusing CASE with JOIN
- Assuming CASE sorts data
Solution
Step 1: Review correct CASE syntax
The correct syntax requires WHEN conditions, THEN results, ELSE for default, and END to close, with AS to name the column.Step 2: Identify errors in other options
A omits AS before result (END result); B misses WHEN after CASE; C misses END before AS result.Final Answer:
SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END AS result FROM students; -> Option DQuick Check:
CASE syntax = WHEN...THEN...ELSE...END AS alias [OK]
- Omitting WHEN keyword
- Forgetting END keyword
- Not using AS for alias
employees with column salary, what will this query return?SELECT name, CASE WHEN salary >= 5000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees;
Solution
Step 1: Understand the CASE condition
The CASE checks if salary is 5000 or more, then assigns 'High', otherwise 'Low'.Step 2: Analyze the SELECT output
The query returns all employees with their name and a new column salary_level showing 'High' or 'Low' accordingly.Final Answer:
A list of employee names with salary_level showing 'High' if salary ≥ 5000, else 'Low' -> Option CQuick Check:
CASE creates conditional labels per row [OK]
- Thinking CASE filters rows
- Expecting numeric salary changes
- Assuming syntax error
SELECT name, CASE salary > 10000 THEN 'Rich' ELSE 'Average' END AS status FROM users;
Solution
Step 1: Check CASE syntax
The CASE statement must have WHEN before the condition. Here, WHEN is missing.Step 2: Verify other parts
END and ELSE are present, AS is used correctly, so no errors there.Final Answer:
Missing WHEN keyword after CASE -> Option AQuick Check:
CASE requires WHEN before condition [OK]
- Omitting WHEN keyword
- Confusing ELSE with END
- Forgetting AS alias
Solution
Step 1: Check correct CASE syntax with multiple WHEN
SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products; correctly uses multiple WHEN conditions with THEN, ELSE, and END, and aliases the column.Step 2: Analyze logic correctness
SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products; classifies prices below 50 as 'Cheap', between 50 and 100 as 'Moderate', else 'Expensive', which matches the requirement.Final Answer:
SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products; -> Option BQuick Check:
Multiple WHEN conditions with ELSE classify ranges [OK]
- Omitting WHEN keywords
- Misplacing ELSE before WHEN
- Not using AS for alias
Ready to prove it?
Pass the test and this pattern moves unknown
