QuestionQ164

VerifiedEssential Commands

Which of the following SQL statements returns, for each distinct value of the order_type field in the orders table, the number of times that value occurs?

Explanation

To count how many rows exist for each distinct value of a column, SQL requires the GROUP BY clause combined with an aggregate function such as COUNT(*). The query SELECT order_type, COUNT(*) FROM orders GROUP BY order_type; groups all rows by their order_type value and then counts the number of rows in each group, producing one row per distinct order_type along with its occurrence count. The other options either return a single aggregate count without grouping, use invalid syntax, or misuse ORDER BY (which only sorts results and does not group or aggregate them).

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!