Price Tiers
Mediumsql
Write a query returning id, name, and a computed tier column: 'Budget' if price < 300, 'Mid' if price is between 300 and 1000 inclusive, otherwise 'Premium'. Order by id.
sqlCREATE TABLE products ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, category TEXT NOT NULL, price INTEGER NOT NULL, stock INTEGER );
Sample data:
sqlINSERT INTO products (id, name, category, price, stock) VALUES (1, 'ProLaptop', 'Electronics', 85000, 12), (2, 'Notebook', 'Stationery', 40, NULL), (3, 'Python Basics', 'Books', 350, 5), (4, 'ProPhone', 'Electronics', 45000, 0), (5, 'Novel', 'Books', 220, NULL), (6, 'Desk Lamp', 'Home', 900, 8);
Example 1
Input
(none)
Output
id name tier 1 ProLaptop Premium 2 Notebook Budget 3 Python Basics Mid 4 ProPhone Premium 5 Novel Budget 6 Desk Lamp Mid
CASE evaluates conditions top-to-bottom; order the branches from most specific to least. Reference: SELECT id, name, CASE WHEN price < 300 THEN 'Budget' WHEN price <= 1000 THEN 'Mid' ELSE 'Premium' END AS tier FROM products ORDER BY id;