Problem · Medium · 20 min
Orders per customer
- Medium
- Joins
- Grouping and totals
A shop keeps its customers in the table customers and its orders in the table orders.
| Table | Column | Meaning |
|---|---|---|
customers |
id |
The number of the customer. It is unique. |
customers |
name |
The name of the customer. No two customers have the same name. |
orders |
id |
The number of the order. It is unique. |
orders |
customer_id |
The customer who made the order. |
orders |
total |
The value of the order, as a whole number. |
Write one query that returns one row for each customer, with these columns:
name: the name of the customer.order_count: how many orders the customer made.total_spent: the sum of thetotalof those orders.
A customer with no orders must be in the result, with 0 in both numeric columns. Put the customers with more orders first. If two customers have the same number of orders, sort them by name.
Tasks
2 to do
Task 1, not checked yet. Return the right rows for the example data.
Task 2, not checked yet. Return the right rows for other data that you cannot see.
Task 1 of 2
Return the right rows for the example data.