Orders with 3 or more products
Medium
Show the order_id and the number of different products of orders containing at least 3 different products.
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
| order_id | products |
|---|---|
| 2 | 4 |
| 3 | 3 |
| 4 | 3 |
| 6 | 4 |
| 8 | 4 |
| 9 | 4 |
| 10 | 3 |
| 12 | 4 |
| 15 | 4 |
| 16 | 4 |
| 23 | 3 |
| 27 | 3 |
| 29 | 4 |
| 33 | 3 |
| 36 | 3 |
| 41 | 4 |
| 42 | 3 |
| 45 | 3 |
| 49 | 4 |
| 51 | 3 |
20 rows
Show a solution
SELECT order_id, COUNT(*) AS products FROM order_items GROUP BY order_id HAVING COUNT(*) >= 3;
SQL used
New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.