Skip to main contentSkip to solution

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

✅ Correct Option: 3

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question