链接: 力扣
个人做法:
# Write your MySQL query statement below
SELECT A.name Employee
FROM Employee A,Employee B
WHERE B.id=A.managerId
AND A.salary>B.salary
实际上是筛出比salary比managerId对应的元组中的salary大的元组
自连接查询,与自身做笛卡尔乘积,B.id=A.managerId进行限制
官方题解:
SELECT
a.Name AS 'Employee'
FROM
Employee AS a,
Employee AS b
WHERE
a.ManagerId = b.Id
AND a.Salary > b.Salary
个人做法:
链接: 力扣
个人做法:
# Write your MySQL query statement below
SELECT Email
FROM Person GROUP BY Email
HAVING COUNT(*) > 1
使用Group By子查询,将表按Email分组,Email相等的放到一组,然后使用HAVING条件判断,COUNT聚集函数计算结果的个数。
官方题解
select Email from
(
select Email, count(Email) as num
from Person
group by Email
) as statistic
where num > 1
链接: 力扣
个人做法:
# Write your MySQL query statement below
SELECT Name Customers
FROM Customers
WHERE Id NOT IN (SELECT CustomerId FROM Orders)
这里使用嵌套查询,用一个子查询筛选出订单中所有的客户ID构成一个集合,IN判断是否在其中。
官方题解,大差不离
select customers.name as 'Customers'
from customers
where customers.id not in
(
select customerid from orders
)
链接: 力扣
个人做法:
# Write your MySQL query statement below
SELECT * FROM cinema
WHERE description != 'boring'
AND MOD(id,2) = 1
ORDER BY rating DESC
这只是普通的单表查询,使用WHERE子句进行条件判断,description不为"boring",使用MOD函数进行取余,使用ODER BY进行排序,指定DESC降序排列。
链接: 力扣
个人做法:
# Write your MySQL query statement below
SELECT player_id,MIN(event_date) first_login
FROM Activity
GROUP BY player_id
将Activity中的记录按player_id进行分组,使用MIN函数选出每组最小
链接: 力扣
个人做法:
# Write your MySQL query statement below
SELECT name FROM SalesPerson
WHERE sales_id NOT IN(
SELECT sales_id FROM Orders
WHERE com_id = (SELECT com_id FROM Company WHERE name = 'RED')
)
先从Orders表中选出和RED相关的订单中销售员ID,再从SalesPerson查询不在这些ID集合中的销售员ID所对应的名字
链接:力扣
个人做法:
# Write your MySQL query statement below
SELECT class FROM Courses
GROUP BY class
HAVING COUNT(student) >= 5
使用group by分组,再统计学生个数即可
链接: 力扣
# Write your MySQL query statement below
SELECT event_day day,emp_id,
SUM(out_time-in_time) total_time
FROM Employees GROUP BY emp_id,event_day
按照ID和日期进行分组,使用SUM函数统计时间
手机扫一扫
移动阅读更方便
你可能感兴趣的文章