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
 query to retrieve max bal

Author  Topic 

add7
Starting Member

2 Posts

Posted - 2012-11-25 : 10:35:48
I have to write a query that retrieves All the names of the employees who have the maximum salary in a company. say i have two tables with employee{id, name, dept} and empsalary{id, salary}. plz help

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-11-25 : 10:40:00
Please show us what you have tried. What Salary do you consider as maximum?
Go to Top of Page

add7
Starting Member

2 Posts

Posted - 2012-11-25 : 10:49:02
quote:
Originally posted by sodeep

Please show us what you have tried. What Salary do you consider as maximum?



I tried by using MAX and joining the two tables. but that gives only one name and not all the names having the salary=max salary.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-25 : 11:05:11
quote:
Originally posted by add7

quote:
Originally posted by sodeep

Please show us what you have tried. What Salary do you consider as maximum?



I tried by using MAX and joining the two tables. but that gives only one name and not all the names having the salary=max salary.



SELECT TOP 1 WITH TIES *
FROM Table
ORDER BY Salary DESC



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-25 : 11:10:04
or

SELECT *
FROM
(
SELECT DENSE_RANK() OVER (ORDER BY Salary DESC) AS Rnk,*
FROM Table
)t
WHERE Rnk=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -