Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
i have 2 queries
select empid, ename, sal from emp group by empid;
select empid, ename, max(sal) from emp group by empid;
now which query executes correctly?
Neither, unless you have an SQL DBMS that doesn't conform to standard sql.
Hi,
The problem you have with both queries is if you are doing a "group by" statement in SQL you have to make sure that every field is either part of the "group by" or being aggregated (like "max" in your second statement).
So if you change the 2nd statement to be:
select empid, ename, max(sal) from emp group by empid, ename;
That should work!
Aggregation and group by goes together always.
1) select empid, ename, sal from emp group by empid;
This statement do not need a group by as there is no aggregation(sum/Max/Count etc) involved
2) select empid, ename, max(sal) from emp group by empid;
This statement is partially correct as group by is missing ename.Basically, group by clause should contain all the non aggregated fields
hth
Sasi