Home / SQL / Practice problems / Hard

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
Show the expected result
namepricequartile
A5 notebook8.51
Gel pens, pack of 1011.01
Green tea, 100 bags12.01
SQL Pocket Guide25.01
Wireless mouse29.02
Coffee beans 1 kg32.02
Python Crash Course (book)45.02
USB-C hub49.02
Desk lamp59.03
Deep Learning (book)89.03
Mechanical keyboard119.03
Noise-cancelling headphones249.03
27-inch monitor329.04
Ergonomic chair399.04
Standing desk549.04

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.