Using CASE in SELECT for Computed Columns
📖 Scenario: You work in a retail store database. You have a table of products with their prices. You want to add a new column that shows if the product is 'Expensive' or 'Affordable' based on its price.
🎯 Goal: Create a SQL query that uses CASE in the SELECT statement to add a computed column called PriceCategory. This column should show 'Expensive' if the price is more than 100, and 'Affordable' otherwise.
📋 What You'll Learn
Create a table called
Products with columns ProductID (integer), ProductName (text), and Price (numeric).Insert exactly three products with these values: (1, 'Laptop', 1200), (2, 'Mouse', 25), (3, 'Keyboard', 75).
Write a
SELECT query that uses CASE in the SELECT clause to create a computed column PriceCategory.The
PriceCategory should be 'Expensive' if Price > 100, else 'Affordable'.💡 Why This Matters
🌍 Real World
Retail stores often need to classify products by price ranges to help with marketing and inventory decisions.
💼 Career
Knowing how to use CASE in SELECT queries is essential for data analysts and database developers to create meaningful reports and computed columns.
Progress0 / 4 steps