BasicsLEFT JOINGROUP BYGoogleAmazonMeta
Tables
users(user_id INT, name VARCHAR)orders(order_id INT, user_id INT)
Task
From users(user_id, name) and orders(order_id, user_id), return every user along with their order_count (0 if they placed no orders), ordered by user_id ASC.
Why It Matters: LEFT JOIN + COUNT(o.order_id) (never COUNT(*)) preserves zero-activity users—crucial for unbiased activation and funnel metrics.
Output columns: user_id, name, order_count
Example
Input — users
| user_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Carol |
Input — orders
| order_id | user_id |
|---|---|
| 10 | 1 |
| 11 | 1 |
| 12 | 3 |
Expected Output
| user_id | name | order_count |
|---|---|---|
| 1 | Alice | 2 |
| 2 | Bob | 0 |
| 3 | Carol | 1 |
Explanation: Every user is kept in the result; users with no matching orders appear with 0 instead of being dropped.
Requirements
- Exact output columns:
user_id,name,order_count - Row order: Exact
ORDER BYorder is checked by the grader. - Query type: Write a single read-only Standard SQL
SELECT(orWITH ... SELECT) query.
Constraints & Evaluation Criteria
- Return `user_id, name, order_count` ordered by `user_id ASC`.
Hints
SQL⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
users (3 rows), orders (3 rows)
Expected Output / Criteria
3 row(s): (1, Alice, 2); (2, Bob, 0); (3, Carol, 1)