w3resource

AdventureWorks Database: Return the top ten employees ranked by their salary

SQL Query - AdventureWorks: Exercise-117 with Solution

117. From the following table write a query in SQL to return the top ten employees ranked by their salary.

Sample table: HumanResources.EmployeePayHistory


Click to view Full table

Sample Solution:

SELECT BusinessEntityID, Rate,   
       DENSE_RANK() OVER (ORDER BY Rate DESC) AS RankBySalary  
FROM HumanResources.EmployeePayHistory
FETCH FIRST 10 ROWS ONLY;

Sample Output:

businessentityid|rate   |rankbysalary|
----------------+-------+------------+
               1|  125.5|           1|
              25|84.1346|           2|
             273|72.1154|           3|
               2|63.4615|           4|
             234|60.0962|           5|
             263|50.4808|           6|
               7|50.4808|           6|
             234|48.5577|           7|
             287| 48.101|           8|
             274| 48.101|           8|

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Rank the products by the specified inventory locations.
Next: Divide rows into defined groups based on SalesYTD.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.