Which of the following queries are legal?

Options
- SELECT deptno, count(deptno) FROM emp GROUP BY ename;
- SELECT deptno, count(deptno), job FROM emp GROUP BY deptno;
- SELECT deptno, avg(sal) FROM emp;
- SELECT deptno, avg(sal) FROM emp GROUP BY deptno;


CORRECT ANSWER : SELECT deptno, avg(sal) FROM emp GROUP BY deptno;

Discussion Board
Logic Behind Anser

SELECT deptno, count(deptno) FROM emp GROUP BY ename ;:
Is not correct because the column declared in group by is not in select list.
SELECT deptno, count(deptno), job FROM emp GROUP BY deptno;
Is not correct because the column used in aggregator function can not be used in group by clause.
SELECT deptno, avg(sal) FROM emp;
is not correct because a aggregator function can not be used with other column without grouping it.


Shikha Singh 05-20-2020 02:50 PM

Legal query is: SELECT deptno, avg(sal) FROM emp GROUP BY deptno;

Correct answer is: SELECT deptno, avg(sal) FROM emp GROUP BY deptno;
For aggregate functions (COUNT, MAX, MIN, SUM, AVG), GROUP BY clause is mandatory.

Prajakta Pandit 02-2-2017 02:11 AM

Logic behind answe

In Select List we can not write individual result column unless, they are not declared on the GROUP BY clause.

rinkey 12-11-2014 11:22 PM

Logic behind answer

Can you please explain why the other statements are wrong

Shilpa P 11-30-2014 08:53 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement