Customer Order Aggregation
BMW ☯️ Medium SQLJoins
Beginner Mode

Objective

Write a SQL query that retrieves the names and email addresses of customers who have placed more than two orders. Additionally, for each customer, calculate the total number of orders they have placed and the total amount they have spent. The result should be sorted in descending order based on the total amount spent.

Additional Information

  • The database consists of two tables: customers and orders.
  • If a customer has not placed any orders, the total amount spent should be considered as 0.
  • The output should include the following columns: customer_name, email, total_orders, and total_spent.
  • The results should be sorted in descending order by total_spent.

The customers table:

Column Type
id integer
customer_name string
email string

The orders table:

Column Type
id integer
customer_id integer
amount decimal

Examples

Example 1:

Input:

customers
customer_nameemailid
John Smith[email protected]1
Sarah Brown[email protected]2
Mike Johnson[email protected]3
Lisa Davis[email protected]4
orders
amountcustomer_idid
10011
15012
20013
30024
25025
17536
40027
12048

Output:
customer_nameemailtotal_orderstotal_spent
Sarah Brown[email protected]3950
John Smith[email protected]3450
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →