Using CASE in WHERE Clause in SQL
📖 Scenario: You are managing a small store's database. You want to filter products based on their category and price using a flexible condition.
🎯 Goal: Build an SQL query that uses a CASE expression inside the WHERE clause to filter products differently depending on their category.
📋 What You'll Learn
Create a table called
products with columns id, name, category, and price.Insert exactly 5 products with specified values.
Create a variable
price_limit with value 100.Write a SELECT query that uses
CASE inside the WHERE clause to filter products:If category is 'Electronics', select products with price less than
price_limit.Otherwise, select products with price less than 50.
💡 Why This Matters
🌍 Real World
Filtering products dynamically based on category and price is common in e-commerce and inventory management.
💼 Career
Understanding CASE in WHERE clauses helps write flexible SQL queries for reports and data analysis.
Progress0 / 4 steps