Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

sql queries

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?

3 Replies
Gysbert_Wassenaar

Neither, unless you have an SQL DBMS that doesn't conform to standard sql.


talk is cheap, supply exceeds demand
chris_johnson
Creator III
Creator III

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!

sasiparupudi1
Master III
Master III

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