HAVING: Categories With More Than 2 Orders

Basics
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_idcategory
1Tech
2Tech
3Tech
4Books
5Books
6Home

Expected Output

categoryorder_count
Tech3

Explanation: Groups rows by category first, then keeps only categories whose COUNT(*) > 2.

Requirements

  • Exact output columns: category, 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 `category, order_count` ordered by `order_count DESC, category ASC`.

Hints

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