Using Searched CASE Syntax in SQL
📖 Scenario: You work at a small bookstore. You have a table of books with their prices. You want to add a new column that shows a price category for each book: 'Cheap', 'Moderate', or 'Expensive'. This helps customers quickly see which books fit their budget.
🎯 Goal: Create a SQL query that uses the searched CASE syntax to assign a price category to each book based on its price.
📋 What You'll Learn
Create a table called
books with columns id (integer), title (text), and price (numeric).Insert exactly these three books with prices: 'Book A' at 10, 'Book B' at 25, and 'Book C' at 50.
Write a SQL query that selects
id, title, price, and a new column price_category.Use
searched CASE syntax to assign 'Cheap' if price is less than 15, 'Moderate' if price is between 15 and 40 inclusive, and 'Expensive' if price is greater than 40.💡 Why This Matters
🌍 Real World
Classifying products or items into categories based on numeric or text conditions is common in business reports and dashboards.
💼 Career
Understanding CASE syntax helps you write flexible SQL queries for data analysis, reporting, and decision-making tasks in many data-related jobs.
Progress0 / 4 steps