Last active
September 23, 2021 05:13
-
-
Save will-fong/6306dc48b30c11c510856a4913b81b11 to your computer and use it in GitHub Desktop.
LeetCode SQL Exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Write your T-SQL query statement below */ | |
/* | |
=== Steps and Assumptions === | |
1. Join employees with their departments | |
1.1 Assume that employees must have a department and vice versa | |
2. Find the top 3 unique salaries | |
2.1 Assume that salaries can be equivalent and will both need to be output | |
3. Output the department, employee, and salary | |
*/ | |
WITH empRank AS ( | |
SELECT | |
name | |
, salary | |
, departmentid | |
, DENSE_RANK() OVER(PARTITION BY departmentid ORDER BY salary DESC) as rank | |
FROM employee | |
) | |
SELECT | |
dept.name AS Department | |
, empRank.name AS Employee | |
, empRank.salary AS Salary | |
FROM empRank | |
INNER JOIN department dept | |
ON empRank.departmentid = dept.id | |
WHERE rank <= 3 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment