Subquery

imfarhan

Board Regular
Joined
Jan 29, 2010
Messages
125
Office Version
  1. 2016
Platform
  1. Windows
Hi Alls
Just the basic questions, I'm trying to get the result from the EMP table.
I can get the max(sal) from each deptno, but I would like to see the max(sal) from which department.
<TABLE cellSpacing=0 bgColor=#ffffff border=1><CAPTION>

Query21


</CAPTION><THEAD><TR><TH borderColor=#000000 bgColor=#c0c0c0>deptno</TH><TH borderColor=#000000 bgColor=#c0c0c0>MaxOfsal</TH></TR></THEAD><TBODY><TR vAlign=top><TD borderColor=#c0c0c0 align=right>10</TD><TD borderColor=#c0c0c0 align=right>2000</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0 align=right>20</TD><TD borderColor=#c0c0c0 align=right>4000</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0 align=right>30</TD><TD borderColor=#c0c0c0 align=right>1000</TD></TR></TBODY><TFOOT></TFOOT></TABLE>

I have used the following query but not working

Code:
SELECT emp.deptno, max(emp.sal)
FROM emp
GROUP BY emp.deptno
having max(emp.sal) >=  (Select max(emp.sal) from emp group by deptno)

So th Result should be ... 20 , 4000

Thanks in advance!

Regards
Farhan
 
Last edited:
surely you only need
SELECT emp.deptno, max(emp.sal)
FROM emp
GROUP BY emp.deptno

to get the maximum salary by department Code

Your HAVING clause appears to be redundant?

maybe you should illustrate what you'd expect to see?
 
Upvote 0
Thanks for reply,
In my SQL I'm getting all the department with their maximum salary.
I want to print only the maximum salary within those department Not other two departments.

so, within those department I need to whic department has maximum salary ... I you know what I mean ?

Many thanks
F
 
Upvote 0

Forum statistics

Threads
1,226,873
Messages
6,193,450
Members
453,800
Latest member
dmwass57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top