Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Get Started Developing for Android Apps with Eclipse Articles
Serial Number in SSRS Articles
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
How to Print a Crystal Report direct to printer Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
Html5 interview questions and answers Interview Questions
SharePoint 2010 interview Questions Answers Interview Questions
Infosys Interview Questions Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
Populate or bind Dropdownlist in Asp.net using Jquery and Json Articles
ASP.NET MVC Overview Interview Questions
Submit Articles | More Articles..

List the highest salary from each department?

Posted By: syedshakeer On:7/23/2010 5:00:19 AM in:Source Codes Category:Sql Server Hits:2574
List the highest salary from each department?
create table empname
(
name varchar(20),
salary int,
deptno int
)


name salary deptno

inthiyaaz 2000 10
khaja      8000 10
Afroz      2000 20
Heera    1000  30
Naseeb  1000 10
King      1000   20
Imroz    3000  30
Naseer   2000 10
Pankaj   1500  30
--------------------------------------

Q1) List the highest salaries from each department?


Ans
)with CTE as(select row_number() over(partition by deptno order by salary desc) row,salary,deptno from empname)
select * from CTE where row=1

OutPut
:-

row salary deptno

1 8000 10
1 2000 20
1 2000 30


Question) List the Top 2 highest salaries from each department?

with CTE as(select row_number() over(partition by deptno order by salary desc) row,salary,deptno from empname)

select * from CTE where row<3

OutPut :-

row salary deptno

1 8000 10
2 3000 10
1 2000 20
2 1000 20
1 2000 30
2 1500 30
--------------------------------------

Q3)List the name of all Employees who are taking 3rd highest salaries?

Ans)Method 1

select Name,salary from empname
where salary=(select min(salary) from empname
where salary in (select top 3 salary from empname order by salary desc))


OutPut)

name salary

inthiyaaz 2000
Afroz 2000
Imroz 2000

Method 2

with CTE as(select dense_rank() over( order by salary desc) row,salary,deptno from empname)
select * from CTE where row=3
OutPut)

name salary

inthiyaaz 2000
Afroz 2000
Imroz 2000

Thank you
Syed Shakeer Hussain
comments powered by Disqus
User Profile
P.S.SH
Working as Windows Mobile Developer
Hyderabad , India
Email :You must Log In to access the contact details.
Latest Post from :syedshakeer
List the highest salary from each department?
View: 2574 | Submitted on: 7/23/2010 5:00:19 AM
How to get the second last record from a Table.
View: 2540 | Submitted on: 7/23/2010 4:53:01 AM
How to get DATE without DATE TIME using SQL SERVER?
View: 1274 | Submitted on: 7/23/2010 4:50:59 AM
Add Select in DropDownlist
View: 1308 | Submitted on: 7/23/2010 4:47:45 AM
Submit Articles | All Post of This User..

All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher