Medium · Database

Customers Above Average Spend

Given Customer(id, name) and Orders(id, customerId, amount), total each customer’s orders, including zero for a customer with none. Return Customer and Total for customers whose total is strictly above the average customer total; row order does not matter.

Examples

Example 1

{
  "customers": [
    {"id": 1, "name": "Ada"},
    {"id": 2, "name": "Ben"},
    {"id": 3, "name": "Cy"}
  ],
  "orders": [
    {"id": 10, "customerId": 1, "amount": 70},
    {"id": 11, "customerId": 1, "amount": 30},
    {"id": 12, "customerId": 2, "amount": 20}
  ]
}

Output: [{"Customer":"Ada","Total":100}]

Example 2

{
  "customers": [
    {"id": 4, "name": "Dee"},
    {"id": 5, "name": "Eve"}
  ],
  "orders": []
}

Output: []

Rebuild it in the studio

20 free core interview problems with full runner & lessons; explore the complete 380+ library with Pro.

More Database problems