Which statement would count the number of orders per customer using a GROUP BY clause?

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

Multiple Choice

Which statement would count the number of orders per customer using a GROUP BY clause?

Explanation:
Grouping data by customer_id and counting rows in each group is how you get the number of orders per customer. In this approach, you select the customer_id and use an aggregate function COUNT(*) to count the rows in each group, then you apply GROUP BY customer_id to create a separate group for each customer. The alias order_count makes the result clear. This yields one row per customer with their total number of orders. If you omitted GROUP BY, you would get a single total for all orders, not per customer. The option that uses SUM(*) is invalid because SUM expects a numeric expression, not a set of rows. A version with HAVING COUNT(*) > 0 would work, but it's redundant since every group formed by GROUP BY has at least one row.

Grouping data by customer_id and counting rows in each group is how you get the number of orders per customer. In this approach, you select the customer_id and use an aggregate function COUNT() to count the rows in each group, then you apply GROUP BY customer_id to create a separate group for each customer. The alias order_count makes the result clear. This yields one row per customer with their total number of orders. If you omitted GROUP BY, you would get a single total for all orders, not per customer. The option that uses SUM() is invalid because SUM expects a numeric expression, not a set of rows. A version with HAVING COUNT(*) > 0 would work, but it's redundant since every group formed by GROUP BY has at least one row.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy