SQL Beginner Challenge 11: Conditional Logic with CASE

Difficulty: Beginner
Estimated time: 15–20 minutes
SQL concepts: CASE, conditional logic, derived columns
Goal: Add human-readable logic to query results.

The Scenario

A product manager looks at your product list and says:

“Can we label products as Cheap, Standard, or Expensive based on price?”

Instead of doing this logic in a spreadsheet or app code, you’ll do it directly in SQL using a CASE expression.

Database Table

products

column_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
categoryTEXTProduct category
priceDECIMALProduct price

Sample Data

product_idnamecategoryprice
101Wireless MouseAccessories24.99
102Mechanical KeyboardAccessories89.00
10327-inch MonitorDisplays229.99
104USB-C HubAccessories34.50
105Laptop StandWorkspace39.99

Your Task

Write a SQL query that returns:

  • Product name
  • price
  • A new column called price_category

Pricing rules

  • Price below 50Cheap
  • Price 50 to 150Standard
  • Price above 150Expensive

Expected Output

namepriceprice_category
Wireless Mouse24.99Cheap
Mechanical Keyboard89.00Standard
27-inch Monitor229.99Expensive
USB-C Hub34.50Cheap
Laptop Stand39.99Cheap

Constraints

  • Use a CASE expression
  • Name the derived column price_category
  • Do not use SELECT *

Hint (Optional)

A CASE expression works like an IF / ELSE statement:

CASE
WHEN condition THEN result
WHEN condition THEN result
ELSE result
END

SELECT
name,
price,
CASE
WHEN price < 50 THEN ‘Cheap’
WHEN price BETWEEN 50 AND 150 THEN ‘Standard’
ELSE ‘Expensive’
END AS price_category
FROM products;

Explanation

  • CASE evaluates conditions top to bottom.
  • The first matching condition is used.
  • ELSE acts as a fallback when no condition matches.
  • This logic runs inside the database, not in application code.

Common Mistakes

  1. Forgetting the END keyword
  2. Overlapping conditions
  3. Missing an ELSE
    • This can result in NULL values.

Optional Extension (Mini Bonus)

Try:

  1. Add a label like Very Cheap for prices below 25.
  2. Combine CASE with GROUP BY to count products per price category.
  3. Use CASE to flag products as In Budget / Over Budget.

Next Challenge

Beginner Challenge #12: Handling NULL Values with COALESCE

🔗 View reference solution on GitHub
(After you’ve tried the challenge)

Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.