To display the 'Number of Bikes' purchased by each customer from the 'SALE' table, what will be the correct order of the following statements?
(A) SELECT CustID, COUNT(*) "Number of Bikes"
(B) HAVING COUNT(*)>3
(C) GROUP BY CustID
(D) FROM SALE
To display the 'Number of Bikes' purchased by each customer from the 'SALE' table, what will be the correct order of the following statements?
(A) SELECT CustID, COUNT(*) "Number of Bikes"
(B) HAVING COUNT(*)>3
(C) GROUP BY CustID
(D) FROM SALE
Solution
SQL clauses must follow a fixed order: SELECT, FROM, GROUP BY, HAVING. HAVING filters the groups made by GROUP BY, so it comes last.
(A) SELECT CustID, COUNT(*) "Number of Bikes"
(D) FROM SALE
(C) GROUP BY CustID
(B) HAVING COUNT(*)>3
Options 1, 2 and 4: each puts a clause out of order (HAVING before FROM or GROUP BY, or GROUP BY before FROM), which is a syntax error.
Memory trick: the order is SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.
Related questions:
2025: 15 May Shift 2
2025: 15 May Shift 2
2025: 27 May Shift 2
2025: 30 May Shift 2