Practice

Problem 3 of 8

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 the total of 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

  1. Task 1, not checked yet. Return the right rows for the example data.

  2. 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.

All tasks

query.sql

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.