题目来源:LeetCode。
175. 组合两个表
题目
SQL架构 表1: `Person` <pre style="box-sizing: border-box; font-size: 13px; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; margin-top: 0px; margin-bottom: 1em; overflow: auto; background: rgb(247, 249, 250); padding: 10px 15px; color: rgb(38, 50, 56); line-height: 1.6; border-radius: 3px; white-space: pre-wrap;">+-------------+---------+ | 列名 | 类型 | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId 是上表主键 </pre> 表2: `Address` <pre style="box-sizing: border-box; font-size: 13px; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; margin-top: 0px; margin-bottom: 1em; overflow: auto; background: rgb(247, 249, 250); padding: 10px 15px; color: rgb(38, 50, 56); line-height: 1.6; border-radius: 3px; white-space: pre-wrap;">+-------------+---------+ | 列名 | 类型 | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+ AddressId 是上表主键 </pre> 编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息: <pre style="box-sizing: border-box; font-size: 13px; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; margin-top: 0px; margin-bottom: 1em; overflow: auto; background: rgb(247, 249, 250); padding: 10px 15px; color: rgb(38, 50, 56); line-height: 1.6; border-radius: 3px; white-space: pre-wrap;">FirstName, LastName, City, State</pre>
题解
本题考察的是表连接, 题目中要求是否有地址信息都要查询出来,那么应该使用外连接。
使用左外连接求解。
代码
SELECT A.FirstName, A.LastName, B.City, B.State FROM Person AS A LEFT JOIN Address AS B ON A.PersonId=B.PersonId
176.第二高的薪水
题目
SQL架构 编写一个 SQL 查询,获取 `Employee` 表中第二高的薪水(Salary) 。 <pre style="box-sizing: border-box; font-size: 13px; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; margin-top: 0px; margin-bottom: 1em; overflow: auto; background: rgb(247, 249, 250); padding: 10px 15px; color: rgb(38, 50, 56); line-height: 1.6; border-radius: 3px; white-space: pre-wrap;">+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ </pre> 例如上述 `Employee` 表,SQL查询应该返回 `200` 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 `null`。 <pre style="box-sizing: border-box; font-size: 13px; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; margin-top: 0px; margin-bottom: 1em; overflow: auto; background: rgb(247, 249, 250); padding: 10px 15px; color: rgb(38, 50, 56); line-height: 1.6; border-radius: 3px; white-space: pre-wrap;">+---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+</pre>
题解
- 求第二高的薪水,说明需要排序,通过分页思想来取第二条数据,这里注意的一点是需要过滤相同高的薪水,否则的话在某些情况下得到的不是正确答案。
- 但是可能不存在第二高的薪水,需要返回
null
, 那么可以使用IFNULL函数。
代码
SELECT IFNULL((SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1), null) AS SecondHighestSalary
177.第N高的薪水
题目
编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。 +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。 +------------------------+ | getNthHighestSalary(2) | +------------------------+ | 200 | +------------------------+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/nth-highest-salary 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
该题跟求第二高的薪水思想是一样的,只不过多了动态传参数而已。
代码
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN SET N = N - 1; RETURN ( # Write your MySQL query statement below. SELECT ( select distinct salary from Employee order by salary desc limit N, 1 ) AS getNthHighestSalary ); END
178.分数排名
题目
SQL架构 编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。 <pre style="box-sizing: border-box; font-size: 13px; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; margin-top: 0px; margin-bottom: 1em; overflow: auto; background: rgb(247, 249, 250); padding: 10px 15px; color: rgb(38, 50, 56); line-height: 1.6; border-radius: 3px; white-space: pre-wrap;">+----+-------+ | Id | Score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+ </pre> 例如,根据上述给定的 `Scores` 表,你的查询应该返回(按分数从高到低排列): <pre style="box-sizing: border-box; font-size: 13px; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; margin-top: 0px; margin-bottom: 1em; overflow: auto; background: rgb(247, 249, 250); padding: 10px 15px; color: rgb(38, 50, 56); line-height: 1.6; border-radius: 3px; white-space: pre-wrap;">+-------+------+ | Score | Rank | +-------+------+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2 | | 3.65 | 3 | | 3.65 | 3 | | 3.50 | 4 | +-------+------+</pre>
题解
该排名是可以重复排名的且不会因为有并列排名而缺少排名序号。
使用自连接将分数表分为a,b两表, a表的分数排名,统计b表中有多少分数大于当前分数的。
最终结果进行一个降序排序即可。
代码
SELECT a.score AS Score, COUNT(DISTINCT b.score) AS Rank FROM scores a, scores b WHERE b.score >= a.score GROUP BY a.id ORDER BY a.score DESC
180.连续出现的数字
题目
编写一个 SQL 查询,查找所有至少连续出现三次的数字。 +----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ 例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。 +-----------------+ | ConsecutiveNums | +-----------------+ | 1 | +-----------------+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/consecutive-numbers 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
连续出现三次,说明id是连续递增的。
相同的数字,三个值是相等的。
使用自连接,判断值是否相同,id是否连续均可。
代码
select distinct a.Num as ConsecutiveNums from Logs as a,Logs as b,Logs as c where a.Num=b.Num and b.Num=c.Num and a.id=b.id-1 and b.id=c.id-1;
184.部门工资最高的员工
题目
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。 +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | +----+-------+--------+--------------+ Department 表包含公司所有部门的信息。 +----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+ 编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。 +------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | Sales | Henry | 80000 | +------------+----------+--------+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/department-highest-salary 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
使用max函数和分组先求出各个部门的最高薪水。
使用表连接和IN查询来求最终结果。
代码
SELECT Department.name AS 'Department', Employee.name AS 'Employee', Salary FROM Employee JOIN Department ON Employee.DepartmentId = Department.Id WHERE (Employee.DepartmentId , Salary) IN ( SELECT DepartmentId, MAX(Salary) FROM Employee GROUP BY DepartmentId ) ;
181.收入超过经理的员工
题目
Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。 +----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+ 给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。 +----------+ | Employee | +----------+ | Joe | +----------+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
使用子查询判断当前员工的收入是否大于其经理即可。
代码
SELECT Name Employee FROM Employee AS a WHERE Salary > (SELECT Salary FROM Employee WHERE Id = a.Managerid)
182.查找重复的邮箱
题目
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。 示例: +----+---------+ | Id | Email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+ 根据以上输入,你的查询应返回以下结果: +---------+ | Email | +---------+ | a@b.com | +---------+ 说明:所有电子邮箱都是小写字母。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/duplicate-emails 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
使用自联连判断id不同,Email相同,最终结果进行去除即可。
代码
SELECT DISTINCT A.Email FROM Person A, Person B WHERE A.id != B.id AND A.Email=B.Email;
183. 从不订购的客户
题目
某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。 Customers 表: +----+-------+ | Id | Name | +----+-------+ | 1 | Joe | | 2 | Henry | | 3 | Sam | | 4 | Max | +----+-------+ Orders 表: +----+------------+ | Id | CustomerId | +----+------------+ | 1 | 3 | | 2 | 1 | +----+------------+ 例如给定上述表格,你的查询应返回: +-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/customers-who-never-order 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
只需要判断顾客ID是否出现在订单表中即可。
代码
SELECT Name AS Customers FROM Customers WHERE Id NOT IN (SELECT CustomerId AS Id FROM Orders);
185.部门工资前三高的员工
题目
Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。 +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 85000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | | 7 | Will | 70000 | 1 | +----+-------+--------+--------------+ Department 表包含公司所有部门的信息。 +----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+ 编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回: +------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Randy | 85000 | | IT | Joe | 85000 | | IT | Will | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/department-top-three-salaries 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
使用子查询来筛选结果,再用表连接来获取数据。
代码
SELECT d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary FROM Employee e1 JOIN Department d ON e1.DepartmentId = d.Id WHERE 3 > (SELECT COUNT(DISTINCT e2.Salary) FROM Employee e2 WHERE e2.Salary > e1.Salary AND e1.DepartmentId = e2.DepartmentId ) ;
196.删除重复的邮箱
题目
编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。 +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+ Id 是这个表的主键。 例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行: +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | +----+------------------+ 提示: 执行 SQL 之后,输出是整个 Person 表。 使用 delete 语句。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/delete-duplicate-emails 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
使用自连接表a, b,只要判断a和b的Email相等,且b.id>a.id即可。
代码
delete a from Person a,Person b where a.Email = b.Email and a.Id > b.Id
197.上升的温度
题目
给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。 +---------+------------------+------------------+ | Id(INT) | RecordDate(DATE) | Temperature(INT) | +---------+------------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------------+------------------+ 例如,根据上述给定的 Weather 表格,返回如下 Id: +----+ | Id | +----+ | 2 | | 4 | +----+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/rising-temperature 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
使用DATEDIFF函数计算日期的差值,差值为1天且温度较高则是结果。
代码
select w1.Id from Weather as w1 left join Weather as w2 on DATEDIFF(w1.RecordDate,w2.RecordDate)=1 where w1.Temperature>w2.Temperature
595.超过5名学生的课
题目
有一个courses 表 ,有: student (学生) 和 class (课程)。 请列出所有超过或等于5名学生的课。 例如,表: +---------+------------+ | student | class | +---------+------------+ | A | Math | | B | English | | C | Math | | D | Biology | | E | Math | | F | Computer | | G | Math | | H | Math | | I | Math | +---------+------------+ 应该输出: +---------+ | class | +---------+ | Math | +---------+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/classes-more-than-5-students 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
简单的分组查询过滤即可。
代码
select class from courses group by class having count(distinct(student)) >= 5
付费题刷不起,没有价值的题略过了。