auto commit

This commit is contained in:
CyC2018 2019-12-11 00:19:49 +08:00
parent 8f466d2c6f
commit 3b3fac2878
2 changed files with 430 additions and 416 deletions

View File

@ -46,9 +46,22 @@ https://leetcode.com/problems/big-countries/description/
+--------------+-------------+--------------+
```
## Solution
```sql
SELECT name,
population,
area
FROM
World
WHERE
area > 3000000
OR population > 25000000;
```
## SQL Schema
SQL Schema 用于在本地环境下创建表结构并导入数据从而方便在本地环境解答
SQL Schema 用于在本地环境下创建表结构并导入数据从而方便在本地环境调试
```sql
DROP TABLE
@ -64,19 +77,6 @@ VALUES
( 'Angola', 'Africa', '1246700', '20609294', '1009900000' );
```
## Solution
```sql
SELECT name,
population,
area
FROM
World
WHERE
area > 3000000
OR population > 25000000;
```
# 627. Swap Salary
https://leetcode.com/problems/swap-salary/description/
@ -103,6 +103,24 @@ https://leetcode.com/problems/swap-salary/description/
| 4 | D | m | 500 |
```
## Solution
两个相等的数异或的结果为 0 0 与任何一个数异或的结果为这个数
sex 字段只有两个取值'f' 'm'并且有以下规律
```
'f' ^ ('m' ^ 'f') = 'm' ^ ('f' ^ 'f') = 'm'
'm' ^ ('m' ^ 'f') = 'f' ^ ('m' ^ 'm') = 'f'
```
因此将 sex 字段和 'm' ^ 'f' 进行异或操作最后就能反转 sex 字段
```sql
UPDATE salary
SET sex = CHAR ( ASCII(sex) ^ ASCII( 'm' ) ^ ASCII( 'f' ) );
```
## SQL Schema
```sql
@ -118,22 +136,6 @@ VALUES
( '4', 'D', 'f', '500' );
```
## Solution
使用异或操作两个相等的数异或的结果为 0 0 与任何一个数异或的结果为这个数
```
'f' ^ 'm' ^ 'f' = 'm'
'm' ^ 'm' ^ 'f' = 'f'
```
```sql
UPDATE salary
SET sex = CHAR ( ASCII(sex) ^ ASCII( 'm' ) ^ ASCII( 'f' ) );
```
# 620. Not Boring Movies
https://leetcode.com/problems/not-boring-movies/description/
@ -164,6 +166,20 @@ https://leetcode.com/problems/not-boring-movies/description/
+---------+-----------+--------------+-----------+
```
## Solution
```sql
SELECT
*
FROM
cinema
WHERE
id % 2 = 1
AND description != 'boring'
ORDER BY
rating DESC;
```
## SQL Schema
```sql
@ -180,20 +196,6 @@ VALUES
( 5, 'House card', 'Interesting', 9.1 );
```
## Solution
```sql
SELECT
*
FROM
cinema
WHERE
id % 2 = 1
AND description != 'boring'
ORDER BY
rating DESC;
```
# 596. Classes More Than 5 Students
https://leetcode.com/problems/classes-more-than-5-students/description/
@ -226,6 +228,21 @@ https://leetcode.com/problems/classes-more-than-5-students/description/
+---------+
```
## Solution
class 列进行分组之后再使用 count 汇总函数统计数量统计之后使用 having 进行过滤
```sql
SELECT
class
FROM
courses
GROUP BY
class
HAVING
count( DISTINCT student ) >= 5;
```
## SQL Schema
```sql
@ -246,21 +263,6 @@ VALUES
( 'I', 'Math' );
```
## Solution
class 列进行分组之后再使用 count 汇总函数统计数量统计之后使用 having 进行过滤
```sql
SELECT
class
FROM
courses
GROUP BY
class
HAVING
count( DISTINCT student ) >= 5;
```
# 182. Duplicate Emails
https://leetcode.com/problems/duplicate-emails/description/
@ -289,20 +291,6 @@ https://leetcode.com/problems/duplicate-emails/description/
+---------+
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Person;
CREATE TABLE Person ( Id INT, Email VARCHAR ( 255 ) );
INSERT INTO Person ( Id, Email )
VALUES
( 1, 'a@b.com' ),
( 2, 'c@d.com' ),
( 3, 'a@b.com' );
```
## Solution
Email 进行分组如果相同 Email 的数量大于等于 2则表示该 Email 重复
@ -318,6 +306,21 @@ HAVING
COUNT( * ) >= 2;
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Person;
CREATE TABLE Person ( Id INT, Email VARCHAR ( 255 ) );
INSERT INTO Person ( Id, Email )
VALUES
( 1, 'a@b.com' ),
( 2, 'c@d.com' ),
( 3, 'a@b.com' );
```
# 196. Delete Duplicate Emails
https://leetcode.com/problems/delete-duplicate-emails/description/
@ -347,10 +350,6 @@ https://leetcode.com/problems/delete-duplicate-emails/description/
+----+------------------+
```
## SQL Schema
182 相同
## Solution
只保留相同 Email Id 最小的那一个然后删除其它的
@ -389,6 +388,12 @@ WHERE
参考[pMySQL Error 1093 - Can't specify target table for update in FROM clause](https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause)
## SQL Schema
182 相同
# 175. Combine Two Tables
https://leetcode.com/problems/combine-two-tables/description/
@ -424,6 +429,22 @@ AddressId is the primary key column for this table.
查找 FirstName, LastName, City, State 数据而不管一个用户有没有填地址信息
## Solution
涉及到 Person Address 两个表在对这两个表执行连接操作时因为要保留 Person 表中的信息即使在 Address 表中没有关联的信息也要保留此时可以用左外连接 Person 表放在 LEFT JOIN 的左边
```sql
SELECT
FirstName,
LastName,
City,
State
FROM
Person P
LEFT JOIN Address A
ON P.PersonId = A.PersonId;
```
## SQL Schema
```sql
@ -443,22 +464,6 @@ VALUES
( 1, 2, 'New York City', 'New York' );
```
## Solution
涉及到 Person Address 两个表在对这两个表执行连接操作时因为要保留 Person 表中的信息即使在 Address 表中没有关联的信息也要保留此时可以用左外连接 Person 表放在 LEFT JOIN 的左边
```sql
SELECT
FirstName,
LastName,
City,
State
FROM
Person P
LEFT JOIN Address A
ON P.PersonId = A.PersonId;
```
# 181. Employees Earning More Than Their Managers
https://leetcode.com/problems/employees-earning-more-than-their-managers/description/
@ -480,6 +485,18 @@ Employee 表:
查找薪资大于其经理薪资的员工信息
## Solution
```sql
SELECT
E1.NAME AS Employee
FROM
Employee E1
INNER JOIN Employee E2
ON E1.ManagerId = E2.Id
AND E1.Salary > E2.Salary;
```
## SQL Schema
```sql
@ -495,18 +512,6 @@ VALUES
( 4, 'Max', 90000, NULL );
```
## Solution
```sql
SELECT
E1.NAME AS Employee
FROM
Employee E1
INNER JOIN Employee E2
ON E1.ManagerId = E2.Id
AND E1.Salary > E2.Salary;
```
# 183. Customers Who Never Order
https://leetcode.com/problems/customers-who-never-order/description/
@ -548,29 +553,6 @@ Orders 表:
+-----------+
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Customers;
CREATE TABLE Customers ( Id INT, NAME VARCHAR ( 255 ) );
DROP TABLE
IF
EXISTS Orders;
CREATE TABLE Orders ( Id INT, CustomerId INT );
INSERT INTO Customers ( Id, NAME )
VALUES
( 1, 'Joe' ),
( 2, 'Henry' ),
( 3, 'Sam' ),
( 4, 'Max' );
INSERT INTO Orders ( Id, CustomerId )
VALUES
( 1, 3 ),
( 2, 1 );
```
## Solution
左外链接
@ -597,6 +579,29 @@ WHERE
Id NOT IN ( SELECT CustomerId FROM Orders );
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Customers;
CREATE TABLE Customers ( Id INT, NAME VARCHAR ( 255 ) );
DROP TABLE
IF
EXISTS Orders;
CREATE TABLE Orders ( Id INT, CustomerId INT );
INSERT INTO Customers ( Id, NAME )
VALUES
( 1, 'Joe' ),
( 2, 'Henry' ),
( 3, 'Sam' ),
( 4, 'Max' );
INSERT INTO Orders ( Id, CustomerId )
VALUES
( 1, 3 ),
( 2, 1 );
```
# 184. Department Highest Salary
https://leetcode.com/problems/department-highest-salary/description/
@ -638,25 +643,6 @@ Department 表:
+------------+----------+--------+
```
## SQL Schema
```sql
DROP TABLE IF EXISTS Employee;
CREATE TABLE Employee ( Id INT, NAME VARCHAR ( 255 ), Salary INT, DepartmentId INT );
DROP TABLE IF EXISTS Department;
CREATE TABLE Department ( Id INT, NAME VARCHAR ( 255 ) );
INSERT INTO Employee ( Id, NAME, Salary, DepartmentId )
VALUES
( 1, 'Joe', 70000, 1 ),
( 2, 'Henry', 80000, 2 ),
( 3, 'Sam', 60000, 2 ),
( 4, 'Max', 90000, 1 );
INSERT INTO Department ( Id, NAME )
VALUES
( 1, 'IT' ),
( 2, 'Sales' );
```
## Solution
创建一个临时表包含了部门员工的最大薪资可以对部门进行分组然后使用 MAX() 汇总函数取得最大薪资
@ -678,6 +664,26 @@ WHERE
AND E.Salary = M.Salary;
```
## SQL Schema
```sql
DROP TABLE IF EXISTS Employee;
CREATE TABLE Employee ( Id INT, NAME VARCHAR ( 255 ), Salary INT, DepartmentId INT );
DROP TABLE IF EXISTS Department;
CREATE TABLE Department ( Id INT, NAME VARCHAR ( 255 ) );
INSERT INTO Employee ( Id, NAME, Salary, DepartmentId )
VALUES
( 1, 'Joe', 70000, 1 ),
( 2, 'Henry', 80000, 2 ),
( 3, 'Sam', 60000, 2 ),
( 4, 'Max', 90000, 1 );
INSERT INTO Department ( Id, NAME )
VALUES
( 1, 'IT' ),
( 2, 'Sales' );
```
# 176. Second Highest Salary
https://leetcode.com/problems/second-highest-salary/description/
@ -706,6 +712,15 @@ https://leetcode.com/problems/second-highest-salary/description/
没有找到返回 null 而不是不返回数据
## Solution
为了在没有查找到数据时返回 null需要在查询结果外面再套一层 SELECT
```sql
SELECT
( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1, 1 ) SecondHighestSalary;
```
## SQL Schema
```sql
@ -720,25 +735,12 @@ VALUES
( 3, 300 );
```
## Solution
为了在没有查找到数据时返回 null需要在查询结果外面再套一层 SELECT
```sql
SELECT
( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1, 1 ) SecondHighestSalary;
```
# 177. Nth Highest Salary
## Description
查找工资第 N 高的员工
## SQL Schema
176
## Solution
```sql
@ -750,6 +752,11 @@ RETURN ( SELECT ( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMI
END
```
## SQL Schema
176
# 178. Rank Scores
https://leetcode.com/problems/rank-scores/description/
@ -786,23 +793,6 @@ https://leetcode.com/problems/rank-scores/description/
+-------+------+
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Scores;
CREATE TABLE Scores ( Id INT, Score DECIMAL ( 3, 2 ) );
INSERT INTO Scores ( Id, Score )
VALUES
( 1, 3.5 ),
( 2, 3.65 ),
( 3, 4.0 ),
( 4, 3.85 ),
( 5, 4.0 ),
( 6, 3.65 );
```
## Solution
要统计某个 score 的排名只要统计大于该 score score 数量然后加 1
@ -852,6 +842,23 @@ ORDER BY
S1.score DESC;
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Scores;
CREATE TABLE Scores ( Id INT, Score DECIMAL ( 3, 2 ) );
INSERT INTO Scores ( Id, Score )
VALUES
( 1, 3.5 ),
( 2, 3.65 ),
( 3, 4.0 ),
( 4, 3.85 ),
( 5, 4.0 ),
( 6, 3.65 );
```
# 180. Consecutive Numbers
https://leetcode.com/problems/consecutive-numbers/description/
@ -884,6 +891,21 @@ https://leetcode.com/problems/consecutive-numbers/description/
+-----------------+
```
## Solution
```sql
SELECT
DISTINCT L1.num ConsecutiveNums
FROM
Logs L1,
Logs L2,
Logs L3
WHERE L1.id = l2.id - 1
AND L2.id = L3.id - 1
AND L1.num = L2.num
AND l2.num = l3.num;
```
## SQL Schema
```sql
@ -902,21 +924,6 @@ VALUES
( 7, 2 );
```
## Solution
```sql
SELECT
DISTINCT L1.num ConsecutiveNums
FROM
Logs L1,
Logs L2,
Logs L3
WHERE L1.id = l2.id - 1
AND L2.id = L3.id - 1
AND L1.num = L2.num
AND l2.num = l3.num;
```
# 626. Exchange Seats
https://leetcode.com/problems/exchange-seats/description/
@ -951,22 +958,6 @@ seat 表存储着座位对应的学生。
+---------+---------+
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS seat;
CREATE TABLE seat ( id INT, student VARCHAR ( 255 ) );
INSERT INTO seat ( id, student )
VALUES
( '1', 'Abbot' ),
( '2', 'Doris' ),
( '3', 'Emerson' ),
( '4', 'Green' ),
( '5', 'Jeames' );
```
## Solution
使用多个 union
@ -1004,6 +995,22 @@ ORDER BY
id;
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS seat;
CREATE TABLE seat ( id INT, student VARCHAR ( 255 ) );
INSERT INTO seat ( id, student )
VALUES
( '1', 'Abbot' ),
( '2', 'Doris' ),
( '3', 'Emerson' ),
( '4', 'Green' ),
( '5', 'Jeames' );
```

View File

@ -46,9 +46,22 @@ https://leetcode.com/problems/big-countries/description/
+--------------+-------------+--------------+
```
## Solution
```sql
SELECT name,
population,
area
FROM
World
WHERE
area > 3000000
OR population > 25000000;
```
## SQL Schema
SQL Schema 用于在本地环境下创建表结构并导入数据从而方便在本地环境解答
SQL Schema 用于在本地环境下创建表结构并导入数据从而方便在本地环境调试
```sql
DROP TABLE
@ -64,19 +77,6 @@ VALUES
( 'Angola', 'Africa', '1246700', '20609294', '1009900000' );
```
## Solution
```sql
SELECT name,
population,
area
FROM
World
WHERE
area > 3000000
OR population > 25000000;
```
# 627. Swap Salary
https://leetcode.com/problems/swap-salary/description/
@ -103,6 +103,24 @@ https://leetcode.com/problems/swap-salary/description/
| 4 | D | m | 500 |
```
## Solution
两个相等的数异或的结果为 0 0 与任何一个数异或的结果为这个数
sex 字段只有两个取值'f' 'm'并且有以下规律
```
'f' ^ ('m' ^ 'f') = 'm' ^ ('f' ^ 'f') = 'm'
'm' ^ ('m' ^ 'f') = 'f' ^ ('m' ^ 'm') = 'f'
```
因此将 sex 字段和 'm' ^ 'f' 进行异或操作最后就能反转 sex 字段
```sql
UPDATE salary
SET sex = CHAR ( ASCII(sex) ^ ASCII( 'm' ) ^ ASCII( 'f' ) );
```
## SQL Schema
```sql
@ -118,22 +136,6 @@ VALUES
( '4', 'D', 'f', '500' );
```
## Solution
使用异或操作两个相等的数异或的结果为 0 0 与任何一个数异或的结果为这个数
```
'f' ^ 'm' ^ 'f' = 'm'
'm' ^ 'm' ^ 'f' = 'f'
```
```sql
UPDATE salary
SET sex = CHAR ( ASCII(sex) ^ ASCII( 'm' ) ^ ASCII( 'f' ) );
```
# 620. Not Boring Movies
https://leetcode.com/problems/not-boring-movies/description/
@ -164,6 +166,20 @@ https://leetcode.com/problems/not-boring-movies/description/
+---------+-----------+--------------+-----------+
```
## Solution
```sql
SELECT
*
FROM
cinema
WHERE
id % 2 = 1
AND description != 'boring'
ORDER BY
rating DESC;
```
## SQL Schema
```sql
@ -180,20 +196,6 @@ VALUES
( 5, 'House card', 'Interesting', 9.1 );
```
## Solution
```sql
SELECT
*
FROM
cinema
WHERE
id % 2 = 1
AND description != 'boring'
ORDER BY
rating DESC;
```
# 596. Classes More Than 5 Students
https://leetcode.com/problems/classes-more-than-5-students/description/
@ -226,6 +228,21 @@ https://leetcode.com/problems/classes-more-than-5-students/description/
+---------+
```
## Solution
class 列进行分组之后再使用 count 汇总函数统计数量统计之后使用 having 进行过滤
```sql
SELECT
class
FROM
courses
GROUP BY
class
HAVING
count( DISTINCT student ) >= 5;
```
## SQL Schema
```sql
@ -246,21 +263,6 @@ VALUES
( 'I', 'Math' );
```
## Solution
class 列进行分组之后再使用 count 汇总函数统计数量统计之后使用 having 进行过滤
```sql
SELECT
class
FROM
courses
GROUP BY
class
HAVING
count( DISTINCT student ) >= 5;
```
# 182. Duplicate Emails
https://leetcode.com/problems/duplicate-emails/description/
@ -289,20 +291,6 @@ https://leetcode.com/problems/duplicate-emails/description/
+---------+
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Person;
CREATE TABLE Person ( Id INT, Email VARCHAR ( 255 ) );
INSERT INTO Person ( Id, Email )
VALUES
( 1, 'a@b.com' ),
( 2, 'c@d.com' ),
( 3, 'a@b.com' );
```
## Solution
Email 进行分组如果相同 Email 的数量大于等于 2则表示该 Email 重复
@ -318,6 +306,21 @@ HAVING
COUNT( * ) >= 2;
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Person;
CREATE TABLE Person ( Id INT, Email VARCHAR ( 255 ) );
INSERT INTO Person ( Id, Email )
VALUES
( 1, 'a@b.com' ),
( 2, 'c@d.com' ),
( 3, 'a@b.com' );
```
# 196. Delete Duplicate Emails
https://leetcode.com/problems/delete-duplicate-emails/description/
@ -347,10 +350,6 @@ https://leetcode.com/problems/delete-duplicate-emails/description/
+----+------------------+
```
## SQL Schema
182 相同
## Solution
只保留相同 Email Id 最小的那一个然后删除其它的
@ -389,6 +388,12 @@ WHERE
参考[pMySQL Error 1093 - Can't specify target table for update in FROM clause](https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause)
## SQL Schema
182 相同
# 175. Combine Two Tables
https://leetcode.com/problems/combine-two-tables/description/
@ -424,6 +429,22 @@ AddressId is the primary key column for this table.
查找 FirstName, LastName, City, State 数据而不管一个用户有没有填地址信息
## Solution
涉及到 Person Address 两个表在对这两个表执行连接操作时因为要保留 Person 表中的信息即使在 Address 表中没有关联的信息也要保留此时可以用左外连接 Person 表放在 LEFT JOIN 的左边
```sql
SELECT
FirstName,
LastName,
City,
State
FROM
Person P
LEFT JOIN Address A
ON P.PersonId = A.PersonId;
```
## SQL Schema
```sql
@ -443,22 +464,6 @@ VALUES
( 1, 2, 'New York City', 'New York' );
```
## Solution
涉及到 Person Address 两个表在对这两个表执行连接操作时因为要保留 Person 表中的信息即使在 Address 表中没有关联的信息也要保留此时可以用左外连接 Person 表放在 LEFT JOIN 的左边
```sql
SELECT
FirstName,
LastName,
City,
State
FROM
Person P
LEFT JOIN Address A
ON P.PersonId = A.PersonId;
```
# 181. Employees Earning More Than Their Managers
https://leetcode.com/problems/employees-earning-more-than-their-managers/description/
@ -480,6 +485,18 @@ Employee 表:
查找薪资大于其经理薪资的员工信息
## Solution
```sql
SELECT
E1.NAME AS Employee
FROM
Employee E1
INNER JOIN Employee E2
ON E1.ManagerId = E2.Id
AND E1.Salary > E2.Salary;
```
## SQL Schema
```sql
@ -495,18 +512,6 @@ VALUES
( 4, 'Max', 90000, NULL );
```
## Solution
```sql
SELECT
E1.NAME AS Employee
FROM
Employee E1
INNER JOIN Employee E2
ON E1.ManagerId = E2.Id
AND E1.Salary > E2.Salary;
```
# 183. Customers Who Never Order
https://leetcode.com/problems/customers-who-never-order/description/
@ -548,29 +553,6 @@ Orders 表:
+-----------+
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Customers;
CREATE TABLE Customers ( Id INT, NAME VARCHAR ( 255 ) );
DROP TABLE
IF
EXISTS Orders;
CREATE TABLE Orders ( Id INT, CustomerId INT );
INSERT INTO Customers ( Id, NAME )
VALUES
( 1, 'Joe' ),
( 2, 'Henry' ),
( 3, 'Sam' ),
( 4, 'Max' );
INSERT INTO Orders ( Id, CustomerId )
VALUES
( 1, 3 ),
( 2, 1 );
```
## Solution
左外链接
@ -597,6 +579,29 @@ WHERE
Id NOT IN ( SELECT CustomerId FROM Orders );
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Customers;
CREATE TABLE Customers ( Id INT, NAME VARCHAR ( 255 ) );
DROP TABLE
IF
EXISTS Orders;
CREATE TABLE Orders ( Id INT, CustomerId INT );
INSERT INTO Customers ( Id, NAME )
VALUES
( 1, 'Joe' ),
( 2, 'Henry' ),
( 3, 'Sam' ),
( 4, 'Max' );
INSERT INTO Orders ( Id, CustomerId )
VALUES
( 1, 3 ),
( 2, 1 );
```
# 184. Department Highest Salary
https://leetcode.com/problems/department-highest-salary/description/
@ -638,25 +643,6 @@ Department 表:
+------------+----------+--------+
```
## SQL Schema
```sql
DROP TABLE IF EXISTS Employee;
CREATE TABLE Employee ( Id INT, NAME VARCHAR ( 255 ), Salary INT, DepartmentId INT );
DROP TABLE IF EXISTS Department;
CREATE TABLE Department ( Id INT, NAME VARCHAR ( 255 ) );
INSERT INTO Employee ( Id, NAME, Salary, DepartmentId )
VALUES
( 1, 'Joe', 70000, 1 ),
( 2, 'Henry', 80000, 2 ),
( 3, 'Sam', 60000, 2 ),
( 4, 'Max', 90000, 1 );
INSERT INTO Department ( Id, NAME )
VALUES
( 1, 'IT' ),
( 2, 'Sales' );
```
## Solution
创建一个临时表包含了部门员工的最大薪资可以对部门进行分组然后使用 MAX() 汇总函数取得最大薪资
@ -678,6 +664,26 @@ WHERE
AND E.Salary = M.Salary;
```
## SQL Schema
```sql
DROP TABLE IF EXISTS Employee;
CREATE TABLE Employee ( Id INT, NAME VARCHAR ( 255 ), Salary INT, DepartmentId INT );
DROP TABLE IF EXISTS Department;
CREATE TABLE Department ( Id INT, NAME VARCHAR ( 255 ) );
INSERT INTO Employee ( Id, NAME, Salary, DepartmentId )
VALUES
( 1, 'Joe', 70000, 1 ),
( 2, 'Henry', 80000, 2 ),
( 3, 'Sam', 60000, 2 ),
( 4, 'Max', 90000, 1 );
INSERT INTO Department ( Id, NAME )
VALUES
( 1, 'IT' ),
( 2, 'Sales' );
```
# 176. Second Highest Salary
https://leetcode.com/problems/second-highest-salary/description/
@ -706,6 +712,15 @@ https://leetcode.com/problems/second-highest-salary/description/
没有找到返回 null 而不是不返回数据
## Solution
为了在没有查找到数据时返回 null需要在查询结果外面再套一层 SELECT
```sql
SELECT
( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1, 1 ) SecondHighestSalary;
```
## SQL Schema
```sql
@ -720,25 +735,12 @@ VALUES
( 3, 300 );
```
## Solution
为了在没有查找到数据时返回 null需要在查询结果外面再套一层 SELECT
```sql
SELECT
( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1, 1 ) SecondHighestSalary;
```
# 177. Nth Highest Salary
## Description
查找工资第 N 高的员工
## SQL Schema
176
## Solution
```sql
@ -750,6 +752,11 @@ RETURN ( SELECT ( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMI
END
```
## SQL Schema
176
# 178. Rank Scores
https://leetcode.com/problems/rank-scores/description/
@ -786,23 +793,6 @@ https://leetcode.com/problems/rank-scores/description/
+-------+------+
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Scores;
CREATE TABLE Scores ( Id INT, Score DECIMAL ( 3, 2 ) );
INSERT INTO Scores ( Id, Score )
VALUES
( 1, 3.5 ),
( 2, 3.65 ),
( 3, 4.0 ),
( 4, 3.85 ),
( 5, 4.0 ),
( 6, 3.65 );
```
## Solution
要统计某个 score 的排名只要统计大于该 score score 数量然后加 1
@ -852,6 +842,23 @@ ORDER BY
S1.score DESC;
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Scores;
CREATE TABLE Scores ( Id INT, Score DECIMAL ( 3, 2 ) );
INSERT INTO Scores ( Id, Score )
VALUES
( 1, 3.5 ),
( 2, 3.65 ),
( 3, 4.0 ),
( 4, 3.85 ),
( 5, 4.0 ),
( 6, 3.65 );
```
# 180. Consecutive Numbers
https://leetcode.com/problems/consecutive-numbers/description/
@ -884,6 +891,21 @@ https://leetcode.com/problems/consecutive-numbers/description/
+-----------------+
```
## Solution
```sql
SELECT
DISTINCT L1.num ConsecutiveNums
FROM
Logs L1,
Logs L2,
Logs L3
WHERE L1.id = l2.id - 1
AND L2.id = L3.id - 1
AND L1.num = L2.num
AND l2.num = l3.num;
```
## SQL Schema
```sql
@ -902,21 +924,6 @@ VALUES
( 7, 2 );
```
## Solution
```sql
SELECT
DISTINCT L1.num ConsecutiveNums
FROM
Logs L1,
Logs L2,
Logs L3
WHERE L1.id = l2.id - 1
AND L2.id = L3.id - 1
AND L1.num = L2.num
AND l2.num = l3.num;
```
# 626. Exchange Seats
https://leetcode.com/problems/exchange-seats/description/
@ -951,22 +958,6 @@ seat 表存储着座位对应的学生。
+---------+---------+
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS seat;
CREATE TABLE seat ( id INT, student VARCHAR ( 255 ) );
INSERT INTO seat ( id, student )
VALUES
( '1', 'Abbot' ),
( '2', 'Doris' ),
( '3', 'Emerson' ),
( '4', 'Green' ),
( '5', 'Jeames' );
```
## Solution
使用多个 union
@ -1004,6 +995,22 @@ ORDER BY
id;
```
## SQL Schema
```sql
DROP TABLE
IF
EXISTS seat;
CREATE TABLE seat ( id INT, student VARCHAR ( 255 ) );
INSERT INTO seat ( id, student )
VALUES
( '1', 'Abbot' ),
( '2', 'Doris' ),
( '3', 'Emerson' ),
( '4', 'Green' ),
( '5', 'Jeames' );
```