Skip to content

Instantly share code, notes, and snippets.

@will-fong
Last active September 23, 2021 05:13
Show Gist options
  • Save will-fong/6306dc48b30c11c510856a4913b81b11 to your computer and use it in GitHub Desktop.
Save will-fong/6306dc48b30c11c510856a4913b81b11 to your computer and use it in GitHub Desktop.
LeetCode SQL Exercises

SQL Exercises

Difficulty

Hard

Medium

/* 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