BasicsGROUP BYHAVINGGoogleAmazonMeta
Table
orders(order_id INT, category VARCHAR)
Task
From orders(order_id, category), return category and order_count for categories having strictly more than 2 orders, ordered by order_count DESC, category ASC.
Why It Matters: Knowing WHERE filters rows before grouping while HAVING filters groups after aggregation is a classic SQL interview checkpoint.
Output columns: category, order_count
Example
Input — orders
| order_id | category |
|---|---|
| 1 | Tech |
| 2 | Tech |
| 3 | Tech |
| 4 | Books |
| 5 | Books |
| 6 | Home |
Expected Output
| category | order_count |
|---|---|
| Tech | 3 |
Explanation: Groups rows by category first, then keeps only categories whose COUNT(*) > 2.
Requirements
- Exact output columns:
category,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 `category, order_count` ordered by `order_count DESC, category ASC`.
Hints
SQL⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
orders (6 rows)
Expected Output / Criteria
1 row(s): (Tech, 3)