Linux Foundation Certified System Administrator: SQL Query for Counting Occurrences | WebsiteName

Counting Occurrences of 'order_type' Field in 'orders' Table

Question

Which of the following SQL queries counts the number of occurrences for each value of the field order_type in the table orders?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

B

The correct answer is B. SELECT order_type,COUNT(*) FROM orders GROUP BY order_type;

Explanation:

A. SELECT order_type,COUNT(*) FROM orders WHERE order_type=order_type; This query does not count the occurrences for each value of the field order_type because it includes a WHERE clause that will only return rows where order_type is equal to itself (which is always true) and will count all rows, resulting in a single count of all rows in the table.

B. SELECT order_type,COUNT(*) FROM orders GROUP BY order_type; This query groups the orders by order_type and then counts the occurrences of each order_type. This will return a list of order_types and their corresponding count of occurrences.

C. COUNT(SELECT order_type FROM orders); This query will not work because it is not a valid SQL syntax. The COUNT function requires an argument within parentheses.

D. SELECT COUNT(*) FROM orders ORDER BY order_type; This query counts the total number of rows in the orders table and orders the result by the order_type field. It does not group the orders by order_type and does not count the occurrences of each order_type.

E. SELECT AUTO_COUNT FROM orders COUNT order_type; This query is not valid SQL syntax. It appears to be attempting to use an undefined variable AUTO_COUNT and does not properly specify the COUNT function.

Therefore, the correct answer is B. SELECT order_type,COUNT(*) FROM orders GROUP BY order_type;