leetcode刷题笔记一百八十一题 && 一百八十二题 && 一百八十三题
leetcode刷题笔记一百八十一题 && 一百八十二题 && 一百八十三题
源地址:
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 |
+----------+
# Write your MySQL query statement below
SELECT a.Name AS Employee FROM Employee a JOIN Employee b ON a.ManagerId = b.Id WHERE a.Salary > b.Salary;
182问题描述:
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
示例:
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
根据以上输入,你的查询应返回以下结果:+---------+
| Email |
+---------+
| a@b.com |
+---------+
说明:所有电子邮箱都是小写字母。
# Write your MySQL query statement below
SELECT Email FROM (
SELECT Email, COUNT(Email) AS counte FROM Person GROUP BY Email
) AS TempTable WHERE counte > 1 ;
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 |
+-----------+
# Write your MySQL query statement below
SELECT a.Name AS 'Customers' FROM Customers a WHERE a.Id NOT IN (SELECT CustomerId FROM Orders);