GROUP BY & COUNT: Number of Orders per Customer

Basics
BasicsGROUP BYCOUNTGoogleAmazonMeta

Table

  • orders(order_id INT, user_id INT)

Task

From orders(order_id, user_id), return each user_id and their total order_count, ordered by user_id ASC.

Why It Matters: GROUP BY with COUNT(*) computes per-customer order frequency—the first step in RFM (Recency, Frequency, Monetary) segmentation.

Output columns: user_id, order_count

Example

Input — orders

order_iduser_id
12
21
32
43
52

Expected Output

user_idorder_count
11
23
31

Explanation: Groups orders by customer_id and tallies the number of rows belonging to each customer.

Requirements

  • Exact output columns: user_id, order_count
  • Row order: Exact ORDER BY order is checked by the grader.
  • Query type: Write a single read-only Standard SQL SELECT (or WITH ... SELECT) query.

Constraints & Evaluation Criteria

  • Return `user_id, order_count` ordered by `user_id ASC`.

Hints

SQL
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
orders (5 rows)
Expected Output / Criteria
3 row(s): (1, 1); (2, 3); (3, 1)