前两天重刷了《SQL必知必会》,昨天想到了 LeetCode,于是去刷了几道数据库的题目,开了不少脑洞。
今天把答案整理一下。喔,题库在这里。
175. Combine Two Tables
https://leetcode.com/problems/combine-two-tables/
样例中有些人的 PersonId
无法在 Address
表中找到,所以使用 LEFT JOIN
.
1 | SELECT FirstName, LastName, City, State |
176. Second Highest Salary
https://leetcode.com/problems/second-highest-salary/
UNION
查询,在结果的最后添加一个 NULL
, 若不存在第二高的薪水则会选择 NULL
.
1 | SELECT Salary FROM Employee |
177. Nth Highest Salary
https://leetcode.com/problems/nth-highest-salary/
这个不知道为什么不可以用 LIMIT 1, N-1
,所以用了 IF
函数。
1 | CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT |
178. Rank Scores
https://leetcode.com/problems/rank-scores/
1 | SELECT Score, ( |
180. Consecutive Numbers
https://leetcode.com/problems/consecutive-numbers/
暴力查询😂
1 | SELECT DISTINCT l1.Num AS ConsecutiveNums |
181. Employees Earning More Than Their Managers
https://leetcode.com/problems/employees-earning-more-than-their-managers/
选择雇员,根据 ManagerId
找到雇员上司的薪水,然后进行比较即可。
1 | SELECT Name |
182. Duplicate Emails
https://leetcode.com/problems/duplicate-emails/
按 Email
字段进行分类,使用 HAVING
筛选出相同 Email
数量大于 1 的项。
1 | SELECT Email FROM Person |
183. Customers Who Never Order
https://leetcode.com/problems/customers-who-never-order/
这个也是直接查询…
1 | SELECT c.Name AS Customers |
184. Department Highest Salary
https://leetcode.com/problems/department-highest-salary/
基本上就是直接查询,注意 WHERE
语句中判别条件的位置,否则有可能 TLE😂
1 | SELECT d.Name AS Department, |
185. Department Top Three Salaries
https://leetcode.com/problems/department-top-three-salaries/
输出每个部门薪资最高的三个人。这个题里有个坑,如果两个人薪资相同,那么这两个人并列,都要输出。并且如果四个人的薪资为 3 2 2 1, 薪资为 1 的那个人排第 3 😂
1 | SELECT d.Name AS Department, |
196. Delete Duplicate Emails
https://leetcode.com/problems/delete-duplicate-emails/
MySQL 不允许在删除时依据待删除的表进行筛选 (You can’t specify target table’Person’ for update in FROM clause), 所以要绕一下。
1 | # 错的!! |
197. Rising Temperature
https://leetcode.com/problems/rising-temperature/
主要考 MySQL
的日期操作函数。
1 | SELECT w1.Id AS Id |
262. Trips and Users
https://leetcode.com/problems/trips-and-users/
太乱了,没做😥
后记
昨天花了半天写完这些题,写到最后都不知道自己在写什么了😂不过还是掌握了不少的 SQL 查询技巧,比如 UNION SELECT NULL
等等。