Why CASE expressions are needed
📖 Scenario: You work in a retail store database. You have a table of sales with product names and quantities sold. You want to label each sale as 'Low', 'Medium', or 'High' quantity based on the number sold.
🎯 Goal: Build a SQL query that uses a CASE expression to categorize sales quantities into 'Low', 'Medium', or 'High'.
📋 What You'll Learn
Create a table called
sales with columns product (text) and quantity (integer).Insert these exact rows into
sales: ('Apples', 5), ('Bananas', 15), ('Cherries', 25).Write a SQL query selecting
product, quantity, and a new column quantity_label using a CASE expression.The
CASE expression should label quantity as 'Low' if less than 10, 'Medium' if between 10 and 20 inclusive, and 'High' if greater than 20.💡 Why This Matters
🌍 Real World
Retail and sales databases often need to classify data dynamically, like labeling sales volumes or customer ratings.
💼 Career
Understanding CASE expressions is essential for data analysts and database developers to write flexible and readable SQL queries.
Progress0 / 4 steps