The CASE expression returns a value based on conditions, like if/else:
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE result END
It can be used in SELECT, ORDER BY, UPDATE and inside aggregate functions.
If-then-else logic in queries.
✍️ 3 exercises + code challenge in this lessonThe CASE expression returns a value based on conditions, like if/else:
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE result END
It can be used in SELECT, ORDER BY, UPDATE and inside aggregate functions.
Mastering SQL CASE helps you write clean and reliable SQL code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
SELECT ProductName, Price,
CASE
WHEN Price >= 50000 THEN 'Premium'
WHEN Price >= 5000 THEN 'Standard'
ELSE 'Budget'
END AS PriceBand,
CASE WHEN Stock = 0 THEN 'Out of stock' ELSE 'Available' END AS Availability
FROM Products
ORDER BY Price DESC;
Check what you learned in “SQL CASE”.
Finish the CASE expression.
CASE WHEN Stock = 0 THEN 'Out' ELSE 'In stock'
What does CASE return when no WHEN matches and there is no ELSE?
Select ProductName and a column Size that is 'Big' when Price >= 50000, otherwise 'Small', for ProductID 1 to 3, sorted by ProductID.
Free sign-in required · Google login available
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services