Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let's say there is a table Employee with columns Id (primary key), Name and Salary. How can I display 3 rows of employees with the highest salaries?
Posted

Try something like this:
SQL
SELECT TOP(3) [ID], [Name], MAX([Salary])
FROM YourTable
GROUP BY [ID], [Name]
ORDER BY [Salary] DESC
 
Share this answer
 
v2
Comments
shiny13 12-Feb-13 8:16am    
That's ok no need to join, the table Employee has these columns: Id, Name and Salary. You can just perform this operation there.
Maciej Los 12-Feb-13 8:19am    
You're right! Thank you.
shiny13 12-Feb-13 8:27am    
Best answer! :)
Maciej Los 12-Feb-13 9:19am    
Thank you ;)
Espen Harlinn 12-Feb-13 11:27am    
5'ed!
SQL
select top 3 Name,Salary from Employee order by Salary desc


Edit by shiny13: Oracle equivalent to Top is Rownum, for MySQL its limit.
 
Share this answer
 
v2
Comments
shiny13 12-Feb-13 8:07am    
Thanks! Is this only for SQL Server? Or will this sql also run in MySQL and Oracle? Is there any chance to solve this with aggregate functions like Max(Salary) ?
shiny13 12-Feb-13 8:27am    
I have found the solution to Oracle, the equivalent to Top is Rownum. For MySQL its limit... Thanks for suggesting Top though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900