Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fbd06e7

Browse files
add 569
1 parent f53ed21 commit fbd06e7

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ Your ideas/fixes/algorithms are more than welcome!
642642
|577|[Employee Bonus](https://leetcode.com/problems/employee-bonus/)|[Solution](../master/database/_577.sql) | || Easy |
643643
|574|[Winning Candidate](https://leetcode.com/problems/winning-candidate/)|[Solution](../master/database/_574.sql) | || Medium |
644644
|570|[Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports/)|[Solution](../master/database/_570.sql) | || Medium |
645+
|569|[Median Employee Salary](https://leetcode.com/problems/median-employee-salary/)|[Solution](../master/database/_569.sql) | || Hard |
645646
|262|[Trips and Users](https://leetcode.com/problems/trips-and-users/)|[Solution](../master/database/_262.sql)||| Hard| Inner Join
646647
|197|[Rising Temperature](https://leetcode.com/problems/rising-temperature/)|[Solution](../master/database/_197.sql)| O(n^2)|O(n) | Easy|
647648
|196|[Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/)|[Solution](../master/database/_196.sql)| O(n^2)|O(n) | Easy|

database/_569.sql

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--569. Median Employee Salary
2+
-- The Employee table holds all employees. The employee table has three columns: Employee Id, Company Name, and Salary.
3+
--
4+
--+-----+------------+--------+
5+
--|Id | Company | Salary |
6+
--+-----+------------+--------+
7+
--|1 | A | 2341 |
8+
--|2 | A | 341 |
9+
--|3 | A | 15 |
10+
--|4 | A | 15314 |
11+
--|5 | A | 451 |
12+
--|6 | A | 513 |
13+
--|7 | B | 15 |
14+
--|8 | B | 13 |
15+
--|9 | B | 1154 |
16+
--|10 | B | 1345 |
17+
--|11 | B | 1221 |
18+
--|12 | B | 234 |
19+
--|13 | C | 2345 |
20+
--|14 | C | 2645 |
21+
--|15 | C | 2645 |
22+
--|16 | C | 2652 |
23+
--|17 | C | 65 |
24+
--+-----+------------+--------+
25+
--
26+
--Write a SQL query to find the median salary of each company. Bonus points if you can solve it without using any built-in SQL functions.
27+
--
28+
--+-----+------------+--------+
29+
--|Id | Company | Salary |
30+
--+-----+------------+--------+
31+
--|5 | A | 451 |
32+
--|6 | A | 513 |
33+
--|12 | B | 234 |
34+
--|9 | B | 1154 |
35+
--|14 | C | 2645 |
36+
--+-----+------------+--------+
37+
38+
select Id, Company, Salary from
39+
(
40+
select e.Id, e.Salary, e.Company, if( @prev = e.Company , @Rank := @Rank + 1, @Rank := 1) as rank, @prev := e.Company
41+
from Employee e , (select @Rank := 0, @prev := 0) as temp order by e.Company, e.Salary, e.Id
42+
) Ranking
43+
INNER JOIN
44+
(
45+
select count(*) as totalcount, Company as name from Employee e2 group by e2.Company
46+
) companycount
47+
on companycount.name = Ranking.Company
48+
where Rank = floor((totalcount+1)/2) or Rank = floor((totalcount+2)/2)

0 commit comments

Comments
 (0)