📖 Scenario: You work in a retail store database. You have a table of products with their prices. You want to categorize products as 'Cheap', 'Moderate', or 'Expensive' based on their price.
🎯 Goal: Create a MySQL query that uses the CASE WHEN expression to add a new column called price_category which labels each product as 'Cheap' if the price is less than 50, 'Moderate' if the price is between 50 and 150, and 'Expensive' if the price is above 150.
📋 What You'll Learn
Create a table called products with columns id, name, and price.
Insert exactly three products with prices 30, 100, and 200.
Write a SELECT query that uses CASE WHEN to categorize prices.
The output must include id, name, price, and the new price_category column.
💡 Why This Matters
🌍 Real World
Categorizing products by price helps businesses analyze inventory and pricing strategies easily.
💼 Career
Using CASE WHEN expressions is a common skill for database querying and reporting in many data-related jobs.
Progress0 / 4 steps
1
Create the products table and insert data
Create a table called products with columns id (integer), name (varchar 50), and price (integer). Then insert these three rows exactly: (1, 'Pen', 30), (2, 'Notebook', 100), and (3, 'Backpack', 200).
MySQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Write the basic SELECT query
Write a SELECT query to get id, name, and price from the products table.
MySQL
Hint
Use SELECT id, name, price FROM products; to get the data.
3
Add CASE WHEN expression for price_category
Modify the SELECT query to add a new column called price_category using CASE WHEN. Label products as 'Cheap' if price < 50, 'Moderate' if price BETWEEN 50 AND 150, and 'Expensive' if price > 150.
MySQL
Hint
Use CASE WHEN ... THEN ... ELSE ... END AS price_category inside the SELECT.
4
Complete the query with proper formatting
Ensure the final SELECT query includes id, name, price, and the price_category column using the CASE WHEN expression exactly as specified.
MySQL
Hint
Make sure the SELECT query is complete and formatted as shown.
Practice
(1/5)
1. What does the CASE WHEN expression do in a MySQL query?
easy
A. It creates a new table in the database.
B. It sorts the results in ascending order.
C. It deletes rows that meet a condition.
D. It returns different values based on specified conditions.
Solution
Step 1: Understand the purpose of CASE WHEN
The CASE WHEN expression checks conditions and returns values accordingly.
Step 2: Compare with other options
Creating tables, deleting rows, or sorting are different SQL operations, not done by CASE WHEN.
Final Answer:
It returns different values based on specified conditions. -> Option D
Quick Check:
CASE WHEN = conditional value return [OK]
Hint: CASE WHEN returns values based on conditions, like IF-THEN-ELSE [OK]
Common Mistakes:
Thinking CASE WHEN creates or deletes tables
Confusing CASE WHEN with ORDER BY sorting
Assuming CASE WHEN changes data permanently
2. Which of the following is the correct syntax for a simple CASE WHEN expression in MySQL?
easy
A. SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END FROM students;
B. SELECT CASE score > 50 THEN 'Pass' ELSE 'Fail' END FROM students;
C. SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' FROM students;
D. SELECT CASE score > 50 THEN 'Pass' ELSE 'Fail' END students;
Solution
Step 1: Check the correct CASE WHEN syntax
The correct syntax includes CASE WHEN condition THEN result ELSE result END.
Step 2: Identify syntax errors in other options
The other options miss keywords or punctuation: some omit WHEN, others forget END, or misplace FROM.
Final Answer:
SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END FROM students; -> Option A
Quick Check:
Correct CASE WHEN syntax = SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END FROM students; [OK]
Hint: CASE WHEN needs WHEN, THEN, ELSE, and END keywords [OK]
Common Mistakes:
Omitting WHEN keyword
Forgetting END at the end
Misplacing FROM clause
3. Given the table employees with a column salary, what will this query return?
SELECT name, CASE WHEN salary >= 5000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees;
medium
A. An error because CASE WHEN cannot be used in SELECT.
B. Only employees with salary greater than 5000 will be shown.
C. A list of employee names with 'High' or 'Low' based on salary threshold 5000.
D. A list of salaries labeled as 'High' or 'Low' without employee names.
Solution
Step 1: Understand the CASE WHEN usage in SELECT
The query labels each employee's salary as 'High' if >= 5000, else 'Low'.
Step 2: Check what columns are selected
It selects employee names and the computed salary_level, so all employees appear.
Final Answer:
A list of employee names with 'High' or 'Low' based on salary threshold 5000. -> Option C
Quick Check:
CASE WHEN labels salaries, all employees listed [OK]
Hint: CASE WHEN in SELECT returns labels per row without filtering [OK]
Common Mistakes:
Thinking CASE WHEN filters rows instead of labeling
Assuming query returns error
Ignoring selected columns
4. Identify the error in this query:
SELECT name, CASE salary > 10000 THEN 'Rich' ELSE 'Average' END AS status FROM customers;
medium
A. Missing WHEN keyword after CASE.
B. Missing END keyword at the end.
C. Incorrect alias usage for CASE expression.
D. Using ELSE without THEN is invalid.
Solution
Step 1: Review CASE WHEN syntax
CASE must be followed by WHEN before condition; here WHEN is missing.
Step 2: Check other parts of the query
END keyword is present, alias is correct, ELSE is properly used after THEN.
Final Answer:
Missing WHEN keyword after CASE. -> Option A
Quick Check:
CASE requires WHEN after CASE [OK]
Hint: Always write CASE WHEN, not CASE condition directly [OK]
Common Mistakes:
Omitting WHEN keyword
Confusing ELSE and THEN order
Forgetting END keyword
5. You want to categorize products by price into 'Cheap' (price < 20), 'Moderate' (20 ≤ price ≤ 100), and 'Expensive' (price > 100). Which CASE WHEN expression correctly implements this?
hard
A. CASE WHEN price < 20 THEN 'Cheap' WHEN price < 100 THEN 'Moderate' ELSE 'Expensive' END
B. CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END
C. CASE WHEN price < 20 THEN 'Cheap' WHEN price > 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END
D. CASE WHEN price <= 20 THEN 'Cheap' WHEN price > 20 AND price < 100 THEN 'Moderate' ELSE 'Expensive' END
Solution
Step 1: Analyze the price ranges needed
'Cheap' is price less than 20, 'Moderate' is price between 20 and 100 inclusive, 'Expensive' is above 100.
Step 2: Check each option's conditions
CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END correctly uses price < 20 for 'Cheap', price >= 20 and <= 100 for 'Moderate', else 'Expensive'. Others have incorrect boundaries.
Final Answer:
CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END -> Option B
Quick Check:
Correct range checks = CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END [OK]
Hint: Use inclusive ranges carefully with AND and >=, <= operators [OK]
Common Mistakes:
Overlapping or missing ranges
Using incorrect comparison operators
Not covering all possible values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.