Skip to content

Commit ae05303

Browse files
authored
Merge pull request #48 from iamAntimPal/Branch-1
176. Second Highest Salary
2 parents fd0928a + d3f5747 commit ae05303

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
176. Second Highest Salary
2+
Solved
3+
Medium
4+
Topics
5+
Companies
6+
SQL Schema
7+
Pandas Schema
8+
Table: Employee
9+
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| id | int |
14+
| salary | int |
15+
+-------------+------+
16+
id is the primary key (column with unique values) for this table.
17+
Each row of this table contains information about the salary of an employee.
18+
19+
20+
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).
21+
22+
The result format is in the following example.
23+
24+
25+
26+
Example 1:
27+
28+
Input:
29+
Employee table:
30+
+----+--------+
31+
| id | salary |
32+
+----+--------+
33+
| 1 | 100 |
34+
| 2 | 200 |
35+
| 3 | 300 |
36+
+----+--------+
37+
Output:
38+
+---------------------+
39+
| SecondHighestSalary |
40+
+---------------------+
41+
| 200 |
42+
+---------------------+
43+
Example 2:
44+
45+
Input:
46+
Employee table:
47+
+----+--------+
48+
| id | salary |
49+
+----+--------+
50+
| 1 | 100 |
51+
+----+--------+
52+
Output:
53+
+---------------------+
54+
| SecondHighestSalary |
55+
+---------------------+
56+
| null |
57+
+---------------------+
58+
59+
WITH
60+
RankedEmployees AS (
61+
SELECT *, DENSE_RANK() OVER(ORDER BY salary DESC) AS `rank`
62+
FROM Employee
63+
)
64+
SELECT MAX(salary) AS SecondHighestSalary
65+
FROM RankedEmployees
66+
WHERE `rank` = 2;

0 commit comments

Comments
 (0)