diff --git a/LeetCode SQL 50 Solution/176. Second Highest Salary.SQL b/LeetCode SQL 50 Solution/176. Second Highest Salary.SQL new file mode 100644 index 0000000..e8f8028 --- /dev/null +++ b/LeetCode SQL 50 Solution/176. Second Highest Salary.SQL @@ -0,0 +1,66 @@ +176. Second Highest Salary +Solved +Medium +Topics +Companies +SQL Schema +Pandas Schema +Table: Employee + ++-------------+------+ +| Column Name | Type | ++-------------+------+ +| id | int | +| salary | int | ++-------------+------+ +id is the primary key (column with unique values) for this table. +Each row of this table contains information about the salary of an employee. + + +Write a solution to find the second highest distinct salary from the Employee table. If there is no second highest salary, return null (return None in Pandas). + +The result format is in the following example. + + + +Example 1: + +Input: +Employee table: ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | +| 2 | 200 | +| 3 | 300 | ++----+--------+ +Output: ++---------------------+ +| SecondHighestSalary | ++---------------------+ +| 200 | ++---------------------+ +Example 2: + +Input: +Employee table: ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | ++----+--------+ +Output: ++---------------------+ +| SecondHighestSalary | ++---------------------+ +| null | ++---------------------+ + +WITH + RankedEmployees AS ( + SELECT *, DENSE_RANK() OVER(ORDER BY salary DESC) AS `rank` + FROM Employee + ) +SELECT MAX(salary) AS SecondHighestSalary +FROM RankedEmployees +WHERE `rank` = 2; \ No newline at end of file