美文网首页
每日Leetcode—SQL(3)

每日Leetcode—SQL(3)

作者: Chuck_Wu | 来源:发表于2019-04-17 22:17 被阅读0次

180.连续出现的数字

表Logs
结果表

方法一:

select Num ConsecutiveNums from Logs l1,
left join Logs l2 on l1.Id = l2.Id-1
left join Logs l3 on l1.Id = l3.Id-2
where l1.Num = l2.Num and l2.Num = l3.Num

分析:该方法首先使用left join将表连接起来,再使用where查找满足条件的行。

方法二:

select Num ConsecutiveNums
from Logs l1,Logs l2,Logs l3
where l1.Num = l2.Num and l2.Num = l3.Num and l1.Id = l2.Id-1 and l2.Id = l3.Id-1 

分析:该方法直接使用了where条件查找

181.超过经历收入的员工

表Employee

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。


结果表

方法一:

select Name Employee
from Employee 
where e1.Salary>(select e2.Salary from Employee e2 where e1.ManagerId = e2.Id)

分析:取满足e1.ManagerId = e2.Id的行的Salary,并与e1表进行比较。

方二:

select e1.Name Employee
from Employee e1 join Employee e2
on e1.ManagerId = e2.Id and e1.Salary>e2.Salary

分析:利用join取交集的方式对满足条件的行进行查找。

方法三:

select e1.Name Employee
from Employee e1, Employee e2
where e1.ManagerId = e2.Id and e1.Salary>e2.Salary

分析:直接利用where判断出满足条件的行。

相关文章

  • 每日Leetcode—SQL(3)

    180.连续出现的数字 方法一: 分析:该方法首先使用left join将表连接起来,再使用where查找满足条件...

  • 每日Leetcode—SQL(1)

    175.组合两个表 编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 ...

  • 每日Leetcode—SQL(2)

    177.第N高的薪水 方法: 分析:此题可以参照(176.第二高的薪水)的思路来解题,176中使用了limit 1...

  • 每日Leetcode—SQL(4)

    182. 查找重复的电子邮箱 方法一: 分析:该方法使用聚合函数,利用having查找Email大于1的值。 方法...

  • 每日Leetcode—SQL(6)

    196.删除重复的电子邮箱 方法一: 197.上升的温度 给定一个 Weather 表,编写一个 SQL 查询,来...

  • 每日Leetcode—SQL(5)

    184. 部门工资最高的员工 方法一: 方法二: 分析:该方法的执行时间比方法一有所提升 185.部门工资前三高的...

  • 每日Leetcode—SQL(7)

    620. 有趣的电影 作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为非 boring (...

  • Letter Combinations of a Phone N

    标签: C++ 算法 LeetCode 字符串 每日算法——leetcode系列 问题 3Sum Closest ...

  • LeetCode | SQL | 3题

    凡是遥远的地方 对我们都有一种诱惑不是诱惑于美丽 就是诱惑于传说即便远方的风景 并不尽人意我们也无需在乎 因为这实...

  • LeetCode-SQL-eight

    Leetcode-sql-eight 本文中总结了LeetCode中关于SQL的游戏玩家分析的4个题目 玩家首次登...

网友评论

      本文标题:每日Leetcode—SQL(3)

      本文链接:https://www.haomeiwen.com/subject/rlbowqtx.html