595. Big Countries
https://leetcode.com/problems/big-countries/description/
Description
1 | +-----------------+------------+------------+--------------+---------------+ |
查找面积超过 3,000,000 或者人口数超过 25,000,000 的国家。
1 | +--------------+-------------+--------------+ |
SQL Schema
1 | DROP TABLE |
Solution
1 | SELECT name, |
627. Swap Salary
https://leetcode.com/problems/swap-salary/description/
Description
1 | | id | name | sex | salary | |
只用一个 SQL 查询,将 sex 字段反转。
1 | | id | name | sex | salary | |
SQL Schema
1 | DROP TABLE |
Solution
1 | UPDATE salary |
620. Not Boring Movies
https://leetcode.com/problems/not-boring-movies/description/
Description
1 | +---------+-----------+--------------+-----------+ |
查找 id 为奇数,并且 description 不是 boring 的电影,按 rating 降序。
1 | +---------+-----------+--------------+-----------+ |
SQL Schema
1 | DROP TABLE |
Solution
1 | SELECT |
596. Classes More Than 5 Students
https://leetcode.com/problems/classes-more-than-5-students/description/
Description
1 | +---------+------------+ |
查找有五名及以上 student 的 class。
1 | +---------+ |
SQL Schema
1 | DROP TABLE |
Solution
1 | SELECT |
182. Duplicate Emails
https://leetcode.com/problems/duplicate-emails/description/
Description
邮件地址表:
1 | +----+---------+ |
查找重复的邮件地址:
1 | +---------+ |
SQL Schema
1 | DROP TABLE |
Solution
1 | SELECT |
196. Delete Duplicate Emails
https://leetcode.com/problems/delete-duplicate-emails/description/
Description
邮件地址表:
1 | +----+---------+ |
删除重复的邮件地址:
1 | +----+------------------+ |
SQL Schema
与 182 相同。
Solution
连接:
1 | DELETE p1 |
子查询:
1 | DELETE |
应该注意的是上述解法额外嵌套了一个 SELECT 语句,如果不这么做,会出现错误:You can’t specify target table ‘Person’ for update in FROM clause。以下演示了这种错误解法。
1 | DELETE |
参考:pMySQL Error 1093 - Can’t specify target table for update in FROM clause
175. Combine Two Tables
https://leetcode.com/problems/combine-two-tables/description/
Description
Person 表:
1 | +-------------+---------+ |
Address 表:
1 | +-------------+---------+ |
查找 FirstName, LastName, City, State 数据,而不管一个用户有没有填地址信息。
SQL Schema
1 | DROP TABLE |
Solution
使用左外连接。
1 | SELECT |
181. Employees Earning More Than Their Managers
https://leetcode.com/problems/employees-earning-more-than-their-managers/description/
Description
Employee 表:
1 | +----+-------+--------+-----------+ |
查找所有员工,他们的薪资大于其经理薪资。
SQL Schema
1 | DROP TABLE |
Solution
1 | SELECT |
183. Customers Who Never Order
https://leetcode.com/problems/customers-who-never-order/description/
Description
Curstomers 表:
1 | +----+-------+ |
Orders 表:
1 | +----+------------+ |
查找没有订单的顾客信息:
1 | +-----------+ |
SQL Schema
1 | DROP TABLE |
Solution
左外链接
1 | SELECT |
子查询
1 | SELECT |
184. Department Highest Salary
https://leetcode.com/problems/department-highest-salary/description/
Description
Employee 表:
1 | +----+-------+--------+--------------+ |
Department 表:
1 | +----+----------+ |
查找一个 Department 中收入最高者的信息:
1 | +------------+----------+--------+ |
SQL Schema
1 | DROP TABLE IF EXISTS Employee; |
Solution
创建一个临时表,包含了部门员工的最大薪资。可以对部门进行分组,然后使用 MAX() 汇总函数取得最大薪资。
之后使用连接找到一个部门中薪资等于临时表中最大薪资的员工。
1 | SELECT |
176. Second Highest Salary
https://leetcode.com/problems/second-highest-salary/description/
Description
1 | +----+--------+ |
查找工资第二高的员工。
1 | +---------------------+ |
没有找到返回 null 而不是不返回数据。
SQL Schema
1 | DROP TABLE |
Solution
为了在没有查找到数据时返回 null,需要在查询结果外面再套一层 SELECT。
1 | SELECT |
177. Nth Highest Salary
Description
查找工资第 N 高的员工。
SQL Schema
同 176。
Solution
1 | CREATE FUNCTION getNthHighestSalary ( N INT ) RETURNS INT BEGIN |
178. Rank Scores
https://leetcode.com/problems/rank-scores/description/
Description
得分表:
1 | +----+-------+ |
将得分排序,并统计排名。
1 | +-------+------+ |
SQL Schema
1 | DROP TABLE |
Solution
1 | SELECT |
180. Consecutive Numbers
https://leetcode.com/problems/consecutive-numbers/description/
Description
数字表:
1 | +----+-----+ |
查找连续出现三次的数字。
1 | +-----------------+ |
SQL Schema
1 | DROP TABLE |
Solution
1 | SELECT |
626. Exchange Seats
https://leetcode.com/problems/exchange-seats/description/
Description
seat 表存储着座位对应的学生。
1 | +---------+---------+ |
要求交换相邻座位的两个学生,如果最后一个座位是奇数,那么不交换这个座位上的学生。
1 | +---------+---------+ |
SQL Schema
1 | DROP TABLE |
Solution
使用多个 union。
1 | SELECT |