Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfbd06e7

Browse files
add 569
1 parentf53ed21 commitfbd06e7

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, Salaryfrom
39+
(
40+
selecte.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 temporder bye.Company,e.Salary,e.Id
42+
) Ranking
43+
INNER JOIN
44+
(
45+
selectcount(*)as totalcount, Companyas namefrom Employee e2group bye2.Company
46+
) companycount
47+
oncompanycount.name=Ranking.Company
48+
where Rank= floor((totalcount+1)/2)or Rank= floor((totalcount+2)/2)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp