UGC-NET Aug 2016 · SQL Queries

MCQ · 2 marks · Problem DBMS-SQL-0006

Consider a database table $R$ with attributes $A$ and $B$. Which of the following $SQL$ queries is illegal ?
  1. A
    SELECT A FROM R;
  2. B
    SELECT A, COUNT(*) FROM R;
  3. C
    SELECT A, COUNT(*) FROM R GROUP BY A;
  4. D
    SELECT A, B, COUNT(*) FROM R GROUP BY A, B;

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

Show the worked solutionWhen an aggregate such as COUNT(*) appears in the SELECT list, every other selected column must appear in GROUP BY. In (B), A is not grouped, so the query has no single value of A to show beside one total count. It is illegal.(A) A plain projection — legal.(C) and (D) Every non-aggregated column is in GROUP BY — legal.Answer: B

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