Fill in the words from below to complete the query in the correct order to display the PaymentMode and the number of payments made using that mode more than once from the relation 'SALE'.
_______ PaymentMode, COUNT(PaymentMode) _____ SALE ________ PaymentMode _________ COUNT(*)>1 ORDER BY PaymentMode;
(A) HAVING
(B) FROM
(C) SELECT
(D) GROUP BY
Fill in the words from below to complete the query in the correct order to display the PaymentMode and the number of payments made using that mode more than once from the relation 'SALE'.
_______ PaymentMode, COUNT(PaymentMode) _____ SALE ________ PaymentMode _________ COUNT(*)>1 ORDER BY PaymentMode;
(A) HAVING
(B) FROM
(C) SELECT
(D) GROUP BY
Solution
The blanks fill in SQL's clause order: SELECT, FROM, GROUP BY, HAVING.
SELECT PaymentMode, COUNT(PaymentMode) FROM SALE GROUP BY PaymentMode HAVING COUNT(*)>1 ORDER BY PaymentMode;
GROUP BY puts the rows into one group per payment mode. HAVING then keeps only the groups with more than one payment, because it filters groups after aggregation.
Option 2: HAVING cannot come before GROUP BY, since it filters the groups that GROUP BY creates.
Remember that WHERE filters rows before grouping, while HAVING filters groups after grouping. An aggregate such as COUNT(*) can only be used in HAVING.
Related questions:
2025: 13 May Shift 2
2026: 29 May Shift 1
2025: 30 May Shift 2
2026: 20 May Shift 1