GATE / CS ARCHIVE

GATE CSE 2012 / QUESTION 15

HAVING without GROUP BY and unselected grouping attributes

Database Management Systems · SQL & Queries · Full GATE CSE 2012 paper

GATE 2012, Q15 1 mark · MCQ
Report issue

SQL & Queries

Question 15, GATE 2012: Which of the following statements are TRUE about an SQL query?

        P : An SQL query can contain a HAVING clause even if it does not have a GROUP BY clause
        Q : An SQL query can contain a HAVING clause only if it has a GROUP BY clause
        R : All attributes used in the GROUP BY clause must appear in the SELECT clause
        S : Not all attributes used in the GROUP BY clause need to appear in the SELECT clause
        (A) P and R             (B) P and S              (C) Q and R             (D) Q and S
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Our answer: B

P is true: SELECT COUNT(*) FROM T HAVING COUNT(*)>0 is a valid aggregate query without GROUP BY, treating the input as one group. S is true: SELECT COUNT(*) FROM T GROUP BY a groups by a without selecting a. Therefore Q and R are false, and the correct pair is P and S, option B. The appended official Code A key instead prints C (Q and R). Preserve official C for scoring provenance and derived B as a documented source/key discrepancy; do not silently change either.

Check: On a table with values a=1,1,2, grouping by a while selecting only COUNT(*) returns counts 2 and 1 without exposing a. On the same nonempty table, HAVING COUNT(*)>0 without GROUP BY retains its one aggregate group. These concrete cases refute both universal claims Q and R.

Original paper, page 4 · Answer key