Consider the following Relation 'Sales':
Sales(Attribute-CarID, CustID, SaleDate, PaymentMode, EmpID, SalePrice)
Arrange the following clauses to form a SQL query to display the Customer Id and number of cars purchased if the customer purchased more than 1 car from 'Sales' table.
(A) FROM SALE
(B) HAVING Count(*)>1
(C) SELECT CustID, COUNT(*)
(D) GROUP BY CustID
Consider the following Relation 'Sales':
Sales(Attribute-CarID, CustID, SaleDate, PaymentMode, EmpID, SalePrice)
Arrange the following clauses to form a SQL query to display the Customer Id and number of cars purchased if the customer purchased more than 1 car from 'Sales' table.
(A) FROM SALE
(B) HAVING Count(*)>1
(C) SELECT CustID, COUNT(*)
(D) GROUP BY CustID
Solution
SQL clauses must appear in the order SELECT, FROM, GROUP BY, HAVING. HAVING filters the groups formed by GROUP BY, so it has to come after it.
(C) SELECT CustID, COUNT(*)
(A) FROM SALE
(D) GROUP BY CustID
(B) HAVING Count(*)>1
Options 1 and 4: put HAVING before GROUP BY (and, in option 4, FROM last), which is invalid syntax.
Option 2: puts GROUP BY before FROM, which is invalid because the table must be named first.
Remember the order SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. WHERE filters rows before grouping, while HAVING filters groups after it.
Related questions:
2025: 13 May Shift 2
2025: 21 May Shift 1
2026: 26 May Shift 1