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
 Find highest salary and top 3 salaries

Author  Topic 

laddu
Constraint Violating Yak Guru

332 Posts

Posted - 2008-07-12 : 13:13:07
I would like to know the query to find the highest salary and top 3 salaries in employee table

Thanks in advance

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-12 : 14:09:17
1. SELECT MAX(Salary) AS HighestSalary FROM Employee
2. SELECT TOP 3 Salary FROM Employee ORDER BY Salary DESC

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

laddu
Constraint Violating Yak Guru

332 Posts

Posted - 2008-07-12 : 14:33:46
Thank you Tkizer,
How do we find only top 3 salary?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-12 : 14:41:41
quote:
Originally posted by laddu

Thank you Tkizer,
How do we find only top 3 salary?


Use 2nd query provided by Tara.

If you're using SQL 2005 you can even use ROW_NUMBER() function to get this

SELECT Salary
FROM
(
SELECT ROW_NUMBER() OVER(ORDER NY Salary DESC) AS RowNo,*
FROM Employee)t
WHERE t.RowNo=1


will give you highest salary
and

SELECT Salary
FROM
(
SELECT ROW_NUMBER() OVER(ORDER NY Salary DESC) AS RowNo,*
FROM Employee)t
WHERE t.RowNo<=3


Will give you top 3 salaries.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-12 : 14:45:11
What's the point of using the ROW_NUMBER() function though in a simple problem like this?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-12 : 14:48:11
quote:
Originally posted by tkizer

What's the point of using the ROW_NUMBER() function though in a simple problem like this?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



I was just showing another way of doing it.
Go to Top of Page
   

- Advertisement -