Using CASE WHEN Expression in MySQL
📖 Scenario: You work in a retail store database. You have a table of products with their prices. You want to categorize products as 'Cheap', 'Moderate', or 'Expensive' based on their price.
🎯 Goal: Create a MySQL query that uses the CASE WHEN expression to add a new column called price_category which labels each product as 'Cheap' if the price is less than 50, 'Moderate' if the price is between 50 and 150, and 'Expensive' if the price is above 150.
📋 What You'll Learn
Create a table called
products with columns id, name, and price.Insert exactly three products with prices 30, 100, and 200.
Write a SELECT query that uses
CASE WHEN to categorize prices.The output must include
id, name, price, and the new price_category column.💡 Why This Matters
🌍 Real World
Categorizing products by price helps businesses analyze inventory and pricing strategies easily.
💼 Career
Using CASE WHEN expressions is a common skill for database querying and reporting in many data-related jobs.
Progress0 / 4 steps