Number of orders per customer
Mediumrow order matters
Show every customer's name and their number of orders (0 for none), most orders first, then by name.
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 | orders |
|---|---|
| Aarav Sharma | 5 |
| Grace Lee | 5 |
| Hiroshi Tanaka | 5 |
| Sita Gurung | 5 |
| Zoe Clarke | 5 |
| Ava Wilson | 4 |
| Isla Taylor | 4 |
| Jack Murphy | 4 |
| Chloe Davies | 3 |
| Ethan Nguyen | 2 |
| Liam Walker | 2 |
| Lucas Martin | 2 |
| Bikash Thapa | 1 |
| Leo Rossi | 1 |
| Mia Chen | 1 |
| Olivia Smith | 1 |
| Priya Patel | 1 |
| Anjali Karki | 0 |
| Kiran Rai | 0 |
| Noah Brown | 0 |
20 rows
Show a solution
SELECT c.name, COUNT(o.order_id) AS orders FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id GROUP BY c.customer_id ORDER BY orders DESC, c.name;
SQL used
New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.