Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Dept wise max and min salaried employees

Author  Topic 

madhuvrk
Starting Member

2 Posts

Posted - 2014-09-14 : 04:00:41
Hi,

I have the employees and department tables(structure below).

Can anyone please help me write a sql to get the department name,employee earning maximum salary,employee earning minimum salary for each department

result set:

dname |max_salaried_employee|min_salaried_employee
------------------------------------------------------
Accounts | Blakes | Miller
HR | King | James


Structure :

create table dept(
deptno number(2,0),
dname varchar2(14),
loc varchar2(13),
constraint pk_dept primary key (deptno)
);

create table emp(
empno number(4,0),
ename varchar2(10),
job varchar2(9),
mgr number(4,0),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2,0),
constraint pk_emp primary key (empno),
constraint fk_deptno foreign key (deptno) references dept (deptno)
);


Any help would be appreciated!!

Thanks
Madhu

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-09-14 : 07:43:00
select *
from (
select *, row_number() over (partition by deptno order by sal) as rn, count(*) over (partition by deptno) AS c from dbo.emp
) AS d
where rn in (1, c);


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -