GATE CS 2012 · SQL Queries

MCQ · 1 mark · Problem DBMS-SQL-0105

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
  1. A
    P and R
  2. B
    P and S
  3. C
    Q and R
  4. D
    Q and S

Solve it on GATE DA practice →More Relational Model & SQL problems

Show the worked solutionP: True. Without GROUP BY, HAVING treats the whole result as one group, e.g. SELECT COUNT() FROM t HAVING COUNT() > 1 is valid standard SQL.Q: False, by P.R: False. SELECT COUNT(*) FROM emp GROUP BY dept is valid although the grouping column dept is not selected.S: True: the grouping columns need not appear in the SELECT clause. (The reverse rule holds: non-aggregated SELECT columns must appear in GROUP BY.)So P and S. The official GATE 2012 key was (C), which is wrong under the SQL standard.Answer: B

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com