-
Notifications
You must be signed in to change notification settings - Fork 43
/
Nth_Highest_Salary.sql
109 lines (91 loc) · 2.91 KB
/
Nth_Highest_Salary.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
-- Nth Highest Salary
-- https://leetcode.com/problems/nth-highest-salary/description/
/*
Write a SQL query to get the nth highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
*/
-- V0
-- IDEA : MYSQL PROCEDURE
-- DEMO
-- mysql> delimiter //
-- mysql> CREATE PROCEDURE getNthHighestSalary( N INT)
-- -> BEGIN
-- ->
-- -> # Write your MySQL query statement below.
-- -> SELECT MAX(Salary) /*This is the outer query part */
-- -> FROM Employee Emp1
-- -> WHERE (N-1) = ( /* Subquery starts here */
-- -> SELECT COUNT(DISTINCT(Emp2.Salary))
-- -> FROM Employee Emp2
-- -> WHERE Emp2.Salary > Emp1.Salary);
-- -> END//
-- Query OK, 0 rows affected (0.00 sec)
-- mysql> use local_dev;
-- Reading table information for completion of table and column names
-- You can turn off this feature to get a quicker startup with -A
-- Database changed
-- mysql> call getNthHighestSalary(2);
-- +-------------+
-- | MAX(Salary) |
-- +-------------+
-- | 200 |
-- +-------------+
-- 1 row in set (0.00 sec)
-- Query OK, 0 rows affected (0.00 sec)
-- mysql> call getNthHighestSalary(1);
-- +-------------+
-- | MAX(Salary) |
-- +-------------+
-- | 300 |
-- +-------------+
-- 1 row in set (0.00 sec)
-- Query OK, 0 rows affected (0.00 sec)
-- mysql> call getNthHighestSalary(3);
-- +-------------+
-- | MAX(Salary) |
-- +-------------+
-- | 100 |
-- +-------------+
-- 1 row in set (0.00 sec)
-- Query OK, 0 rows affected (0.00 sec)
-- mysql>
delimiter //
CREATE PROCEDURE getNthHighestSalary( N INT)
BEGIN
-- Write your MySQL query statement below.
SELECT MAX(Salary) /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary);
END//
-- V1
-- https://github.com/kamyu104/LeetCode/blob/master/MySQL/nth-highest-salary.sql
-- Time: O(n^2)
-- Space: O(n)
delimiter //
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
-- Write your MySQL query statement below.
SELECT MAX(Salary) /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
);
END//