Easy · Database

Customer Order Counts

Given Customer(id, name) and Orders(id, customerId), return Customer and OrderCount for every customer, including customers with zero orders. Count orders, not joined rows with a missing order; 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},
    {"id": 11, "customerId": 1},
    {"id": 12, "customerId": 2}
  ]
}

Output: [{"Customer":"Ada","OrderCount":2},{"Customer":"Ben","OrderCount":1},{"Customer":"Cy","OrderCount":0}]

Example 2

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

Output: [{"Customer":"Dee","OrderCount":0},{"Customer":"Eve","OrderCount":0}]

Rebuild it in the studio

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

More Database problems