Sorting Products by Category Priority Using CASE in ORDER BY
📖 Scenario: You manage a product database for an online store. You want to display products sorted by category priority: Electronics first, then Clothing, then Home, and finally all other categories.
🎯 Goal: Create a SQL query that sorts products by category priority using CASE inside the ORDER BY clause.
📋 What You'll Learn
Create a table called
products with columns id, name, and category.Insert exactly these products:
(1, 'Laptop', 'Electronics'), (2, 'T-Shirt', 'Clothing'), (3, 'Vacuum', 'Home'), (4, 'Smartphone', 'Electronics'), (5, 'Jeans', 'Clothing').Write a SELECT query to get
name and category from products.Use
CASE inside ORDER BY to sort categories in this order: Electronics, Clothing, Home, then others alphabetically.💡 Why This Matters
🌍 Real World
Sorting products by priority helps online stores show important categories first, improving user experience.
💼 Career
Knowing how to use CASE in ORDER BY is useful for database querying and reporting tasks in many jobs.
Progress0 / 4 steps