Click here to Skip to main content
16,018,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm a newbie to SQL. My Question is

Is it possible to use Rank function and Where together? If yes, can you give an example.

I'm trying to retrieve the 3rd highest paid employee from a sample database.

Thanks in advance
Posted

it is possible as given here[^].

Here is the example used -
USE AdventureWorks2012
SELECT TOP(10) BusinessEntityID, Rate, 
       RANK() OVER (ORDER BY Rate DESC) AS RankBySalary
FROM HumanResources.EmployeePayHistory AS eph1
WHERE RateChangeDate = (SELECT MAX(RateChangeDate) 
                        FROM HumanResources.EmployeePayHistory AS eph2
                        WHERE eph1.BusinessEntityID = eph2.BusinessEntityID)
ORDER BY BusinessEntityID;
 
Share this answer
 
In T-sql you can easily use Rank function with WHERE clause as given here[^] in MSDN.
 
Share this answer
 

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