Price quartiles
Hard
Show each product's name, price and quartile (1 = cheapest quarter, using NTILE(4) by price).
Tables: customers(customer_id, name, city, country, joined) · products(product_id, name, category, price) · orders(order_id, customer_id, order_date, status) · order_items(order_id, product_id, quantity, unit_price)
Your query✔ Solved
Show the expected result
| name | price | quartile |
|---|---|---|
| A5 notebook | 8.5 | 1 |
| Gel pens, pack of 10 | 11.0 | 1 |
| Green tea, 100 bags | 12.0 | 1 |
| SQL Pocket Guide | 25.0 | 1 |
| Wireless mouse | 29.0 | 2 |
| Coffee beans 1 kg | 32.0 | 2 |
| Python Crash Course (book) | 45.0 | 2 |
| USB-C hub | 49.0 | 2 |
| Desk lamp | 59.0 | 3 |
| Deep Learning (book) | 89.0 | 3 |
| Mechanical keyboard | 119.0 | 3 |
| Noise-cancelling headphones | 249.0 | 3 |
| 27-inch monitor | 329.0 | 4 |
| Ergonomic chair | 399.0 | 4 |
| Standing desk | 549.0 | 4 |
15 rows
Show a solution
SELECT name, price, NTILE(4) OVER (ORDER BY price, name) AS quartile FROM products;
SQL used
New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.