leetcode上的数据库习题的练习,就是为了练练手,记笔记,仅供参考。
1,连续出现的数字
表:Logs
id 是这个表的主键。
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
返回的结果表中的数据可以按 任意顺序 排列。
select distinct Num AS ConsecutiveNums
from Logs
where (Id+1,Num) in (select * from logs)
and (Id+2,Num) in (select * from logs)
2,超过经理的收入的员工
编写一个SQL查询来查找收入比经理高的员工。
以 任意顺序 返回结果表。
获取两次表的信息。
select a.Name AS Employee
from Employee AS a,Employee AS b
where a.ManagerId=b.Id
and a.Salary>b.Salary
3,查找重复的电子邮箱
编写一个 SQL 查询来报告所有重复的电子邮件。 请注意,可以保证电子邮件字段不为 NULL。
以 任意顺序 返回结果表。
select email
from person
group by email having count(email)>1