How do you filter groups to those with more than 5 orders?

Study for the SQL Basics Test. Improve your knowledge with multiple choice questions and detailed explanations. Prepare effectively to master SQL concepts!

Multiple Choice

How do you filter groups to those with more than 5 orders?

Explanation:
Grouping data and filtering by aggregates means you first aggregate rows into groups, then apply a condition to those groups. To keep only customers with more than five orders, count how many orders belong to each customer and then restrict the results to groups where that count exceeds five. You group by customer_id, compute the count of orders for each group, and use a HAVING clause to require COUNT(*) > 5. HAVING runs after the grouping and filtering on aggregates, while WHERE runs before grouping and can’t reference aggregate results. So the correct pattern is: select customer_id, COUNT(*) as order_count from orders group by customer_id having COUNT(*) > 5. The other options fail because they either try to filter with WHERE on an aggregate, misuse SUM(*), or omit proper grouping and filtering altogether.

Grouping data and filtering by aggregates means you first aggregate rows into groups, then apply a condition to those groups. To keep only customers with more than five orders, count how many orders belong to each customer and then restrict the results to groups where that count exceeds five. You group by customer_id, compute the count of orders for each group, and use a HAVING clause to require COUNT() > 5. HAVING runs after the grouping and filtering on aggregates, while WHERE runs before grouping and can’t reference aggregate results. So the correct pattern is: select customer_id, COUNT() as order_count from orders group by customer_id having COUNT() > 5. The other options fail because they either try to filter with WHERE on an aggregate, misuse SUM(), or omit proper grouping and filtering altogether.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy