Best-selling product
Medium
Show the name of the product with the most units sold and its units.
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 | units |
|---|---|
| SQL Pocket Guide | 25 |
1 row
Show a solution
SELECT p.name, SUM(i.quantity) AS units FROM order_items i JOIN products p ON p.product_id = i.product_id GROUP BY p.product_id ORDER BY units DESC LIMIT 1;
SQL used
New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.