Skip to content

Commit 334aaf8

Browse files
committed
feat: add solutions to lc problems: No.0178,0181
* No.0178.Rank Scores * No.0181.Employees Earning More Than Their Managers
1 parent 292ce90 commit 334aaf8

File tree

7 files changed

+85
-31
lines changed

7 files changed

+85
-31
lines changed

solution/0100-0199/0178.Rank Scores/README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,40 @@ DENSE_RANK() OVER (
9090

9191
```sql
9292
# Write your MySQL query statement below
93-
SELECT Score, DENSE_RANK() OVER (ORDER BY Score DESC) 'Rank'
94-
FROM Scores;
93+
SELECT
94+
Score,
95+
DENSE_RANK() OVER (
96+
ORDER BY
97+
Score DESC
98+
) 'Rank'
99+
FROM
100+
Scores;
95101
```
96102

97103
### **MySQL5**
98104

99105
MySQL 8 开始才提供了 `ROW_NUMBER()``RANK()``DENSE_RANK()`[窗口函数](https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html),在之前的版本,可以使用变量实现类似的功能:
100106

101107
```sql
102-
SELECT Score,
103-
CONVERT(rk, SIGNED) `Rank`
104-
FROM (SELECT Score,
105-
IF(@latest = Score, @rank, @rank := @rank + 1) rk,
106-
@latest := Score
107-
FROM Scores,
108-
(SELECT @rank := 0, @latest := NULL) tmp
109-
ORDER BY Score DESC) s;
108+
SELECT
109+
Score,
110+
CONVERT(rk, SIGNED) `Rank`
111+
FROM
112+
(
113+
SELECT
114+
Score,
115+
IF(@latest = Score, @rank, @rank := @rank + 1) rk,
116+
@latest := Score
117+
FROM
118+
Scores,
119+
(
120+
SELECT
121+
@rank := 0,
122+
@latest := NULL
123+
) tmp
124+
ORDER BY
125+
Score DESC
126+
) s;
110127
```
111128

112129
<!-- tabs:end -->

solution/0100-0199/0178.Rank Scores/README_EN.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,40 @@ Solution:
7979

8080
```sql
8181
# Write your MySQL query statement below
82-
SELECT Score, DENSE_RANK() OVER (ORDER BY Score DESC) 'Rank'
83-
FROM Scores;
82+
SELECT
83+
Score,
84+
DENSE_RANK() OVER (
85+
ORDER BY
86+
Score DESC
87+
) 'Rank'
88+
FROM
89+
Scores;
8490
```
8591

8692
### **MySQL5**
8793

8894
MySQL only provides [window function](https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html) after version 8. In previous versions, variables can be used to achieve similar functions:
8995

9096
```sql
91-
SELECT Score,
92-
CONVERT(rk, SIGNED) `Rank`
93-
FROM (SELECT Score,
94-
IF(@latest = Score, @rank, @rank := @rank + 1) rk,
95-
@latest := Score
96-
FROM Scores,
97-
(SELECT @rank := 0, @latest := NULL) tmp
98-
ORDER BY Score DESC) s;
97+
SELECT
98+
Score,
99+
CONVERT(rk, SIGNED) `Rank`
100+
FROM
101+
(
102+
SELECT
103+
Score,
104+
IF(@latest = Score, @rank, @rank := @rank + 1) rk,
105+
@latest := Score
106+
FROM
107+
Scores,
108+
(
109+
SELECT
110+
@rank := 0,
111+
@latest := NULL
112+
) tmp
113+
ORDER BY
114+
Score DESC
115+
) s;
99116
```
100117

101118
<!-- tabs:end -->

solution/0100-0199/0178.Rank Scores/Solution.MySQL5.sql

Lines changed: 0 additions & 8 deletions
This file was deleted.

solution/0100-0199/0178.Rank Scores/Solution.MySQL8.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
Score,
4+
DENSE_RANK() OVER (
5+
ORDER BY
6+
Score DESC
7+
) 'Rank'
8+
FROM
9+
Scores;

solution/0100-0199/0181.Employees Earning More Than Their Managers/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@ where Salary > (
7070
)
7171
```
7272

73+
```sql
74+
# Write your MySQL query statement below
75+
select
76+
e1.name Employee
77+
from
78+
Employee e1
79+
join Employee e2 on e1.managerId = e2.id
80+
where
81+
e1.salary > e2.salary
82+
```
83+
7384
<!-- tabs:end -->

solution/0100-0199/0181.Employees Earning More Than Their Managers/README_EN.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,15 @@ where Salary > (
6666
)
6767
```
6868

69+
```sql
70+
# Write your MySQL query statement below
71+
select
72+
e1.name Employee
73+
from
74+
Employee e1
75+
join Employee e2 on e1.managerId = e2.id
76+
where
77+
e1.salary > e2.salary
78+
```
79+
6980
<!-- tabs:end -->

0 commit comments

Comments
 (0)