📖 Scenario: You are managing a small store's database. You want to filter products based on their category and price using a flexible condition.
🎯 Goal: Build an SQL query that uses a CASE expression inside the WHERE clause to filter products differently depending on their category.
📋 What You'll Learn
Create a table called products with columns id, name, category, and price.
Insert exactly 5 products with specified values.
Create a variable price_limit with value 100.
Write a SELECT query that uses CASE inside the WHERE clause to filter products:
If category is 'Electronics', select products with price less than price_limit.
Otherwise, select products with price less than 50.
💡 Why This Matters
🌍 Real World
Filtering products dynamically based on category and price is common in e-commerce and inventory management.
💼 Career
Understanding CASE in WHERE clauses helps write flexible SQL queries for reports and data analysis.
Progress0 / 4 steps
1
Create the products table and insert data
Create a table called products with columns id (integer), name (text), category (text), and price (integer). Then insert these 5 products exactly: (1, 'Laptop', 'Electronics', 120), (2, 'Mouse', 'Electronics', 25), (3, 'Shirt', 'Clothing', 40), (4, 'Book', 'Books', 30), (5, 'Headphones', 'Electronics', 80).
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add the products.
2
Set the price limit variable
Create a variable called price_limit and set it to 100. Use a SQL variable declaration or a common table expression (CTE) to hold this value.
SQL
Hint
Use a CTE with WITH price_limit AS (SELECT 100 AS limit_value) to create the variable.
3
Write the SELECT query with CASE in WHERE clause
Write a SELECT query to get all columns from products. Use the price_limit CTE. In the WHERE clause, use a CASE expression to filter rows: if category is 'Electronics', select products where price < price_limit.limit_value, else select products where price < 50.
SQL
Hint
Use CASE WHEN category = 'Electronics' THEN price_limit.limit_value ELSE 50 END inside the WHERE clause.
4
Complete the query with proper join syntax
Modify the query to use an explicit CROSS JOIN between products and price_limit instead of the comma join. Keep the WHERE clause with the CASE expression unchanged.
SQL
Hint
Use CROSS JOIN instead of comma to join products and price_limit.
Practice
(1/5)
1. What does using CASE in a WHERE clause allow you to do in SQL?
easy
A. Sort the results in multiple ways
B. Create new columns in the result set
C. Apply different filter conditions based on row values
D. Join tables conditionally
Solution
Step 1: Understand the role of CASE in WHERE
The CASE expression in WHERE lets you choose which condition to apply depending on data values in each row.
Step 2: Compare with other options
Creating new columns is done with SELECT, sorting with ORDER BY, and joining with JOIN, not CASE in WHERE.
Final Answer:
Apply different filter conditions based on row values -> Option C
Quick Check:
CASE in WHERE = conditional filtering [OK]
Hint: CASE in WHERE picks filters by row values [OK]
Common Mistakes:
Thinking CASE creates new columns
Confusing CASE with ORDER BY
Assuming CASE controls JOIN logic
2. Which of the following is the correct syntax to use CASE inside a WHERE clause?
easy
A. WHERE CASE age WHEN > 18 THEN 'adult' ELSE 'minor' END = 'adult'
B. WHERE CASE WHEN age > 18 THEN 'adult' ELSE 'minor' END = 'adult'
C. WHERE CASE WHEN age > 18 THEN adult ELSE minor END
D. WHERE CASE age > 18 THEN 'adult' ELSE 'minor' END = 'adult'
Solution
Step 1: Review correct CASE syntax
The correct CASE syntax uses WHEN condition THEN result ELSE result END. WHERE CASE WHEN age > 18 THEN 'adult' ELSE 'minor' END = 'adult' follows this exactly.
Step 2: Identify syntax errors in other options
Options B, C, and D misuse CASE syntax by missing WHEN or ELSE keywords or incorrect placement.
Final Answer:
WHERE CASE WHEN age > 18 THEN 'adult' ELSE 'minor' END = 'adult' -> Option B
Quick Check:
CASE WHEN ... THEN ... ELSE ... END [OK]
Hint: CASE needs WHEN and THEN keywords in WHERE [OK]
Common Mistakes:
Omitting WHEN keyword
Using CASE without END
Placing conditions incorrectly
3. Given the table Employees with column salary, what rows will this query return?
SELECT * FROM Employees WHERE CASE WHEN salary >= 50000 THEN 1 ELSE 0 END = 1;
medium
A. Rows where salary is less than 50000
B. Rows where salary is exactly 50000
C. All rows regardless of salary
D. Rows where salary is 50000 or more
Solution
Step 1: Understand the CASE condition
The CASE returns 1 if salary is 50000 or more, else 0.
Step 2: Apply the WHERE filter
The WHERE clause keeps rows where CASE result equals 1, so salary must be at least 50000.
Final Answer:
Rows where salary is 50000 or more -> Option D
Quick Check:
salary >= 50000 = filtered rows [OK]
Hint: CASE returns 1 for salary ≥ 50000, filter keeps those rows [OK]
Common Mistakes:
Thinking it returns rows below 50000
Confusing equality with greater than
Ignoring ELSE result
4. Identify the error in this query using CASE in WHERE clause:
SELECT * FROM Orders WHERE CASE order_status WHEN 'shipped' THEN TRUE ELSE FALSE;
medium
A. Missing END keyword for CASE expression
B. Incorrect use of TRUE/FALSE in CASE
C. CASE cannot be used in WHERE clause
D. Missing THEN keyword in CASE
Solution
Step 1: Check CASE syntax completeness
The CASE expression must end with END keyword, which is missing here.
Step 2: Verify other parts
TRUE/FALSE usage is valid, CASE can be used in WHERE, and THEN keyword is present.
Final Answer:
Missing END keyword for CASE expression -> Option A
Quick Check:
CASE ... END required [OK]
Hint: Always close CASE with END in WHERE [OK]
Common Mistakes:
Forgetting END keyword
Thinking CASE can't be in WHERE
Misplacing THEN keyword
5. You want to filter Products so that if category is 'Electronics', only products with stock > 0 are shown; otherwise, show products with price < 100. Which WHERE clause using CASE is correct?
hard
A. WHERE CASE WHEN category = 'Electronics' THEN stock > 0 ELSE price < 100 END = TRUE
B. WHERE CASE category WHEN 'Electronics' THEN stock > 0 ELSE price < 100 = TRUE
C. WHERE CASE WHEN category = 'Electronics' THEN stock > 0 ELSE price < 100
D. WHERE CASE category WHEN 'Electronics' THEN stock > 0 ELSE price < 100
Solution
Step 1: Understand CASE returns boolean condition
The CASE must return TRUE or FALSE to filter rows properly.
Step 2: Check syntax and comparison
WHERE CASE WHEN category = 'Electronics' THEN stock > 0 ELSE price < 100 END = TRUE correctly uses WHEN condition THEN boolean expression ELSE boolean expression END = TRUE to filter rows.
Step 3: Identify why others are wrong
Options A, B, and D miss the END keyword for the CASE expression, causing syntax errors.
Final Answer:
WHERE CASE WHEN category = 'Electronics' THEN stock > 0 ELSE price < 100 END = TRUE -> Option A
Quick Check:
CASE returns boolean, compare with TRUE in WHERE [OK]
Hint: Compare CASE result to TRUE in WHERE for boolean filters [OK]
Common Mistakes:
Missing END keyword
Using CASE with simple WHEN without condition
Returning non-boolean values in CASE
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.