LeetCode Database题解
使用外连接即可。
# Write your MySQL query statement below
select FirstName, LastName, City, State from Person left outer join Address on Person.PersonId = Address.PersonId;
# Write your MySQL query statement below
select p.FirstName, p.LastName, a.City, a.State from Person as p left outer join Address as a on p.PersonId = a.PersonId;
查询第二高工资值,可能无此值。使用聚集函数与子查询即可,注意MAX可能返回NULL。
# Write your MySQL query statement below
select max(Salary) as SecondHighestSalary from Employee where Salary < (select max(Salary) from Employee);
# Write your MySQL query statement below
select max(Salary) as SecondHighestSalary from Employee where Salary not in(select max(Salary) from Employee);
查询第N高工资值,可能无此值。使用IFNULL,然后再对结果进行限制即可。
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE M INT;
SET M = N - 1;
RETURN (
# Write your MySQL query statement below.
SELECT IFNULL((SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1), NULL)
);
END
按得分排名并计算排名。使用子查询、聚集函数,然后再对结果进行排序即可。
# Write your MySQL query statement below
select Scores.Score, (select count(*) from (select distinct Score from Scores) as DistinctScores where DistinctScores.Score >= Scores.Score) as Rank from Scores order by Score desc;
# Write your MySQL query statement below
select Scores.Score, count(DistinctScores.Score) as Rank from Scores, (select distinct Score from Scores) as DistinctScores where Scores.Score <= DistinctScores.Score group by Scores.Id, Scores.Score order by Scores.Score desc;
找出至少连续出现3次的Num。先排序,再查询即可。也有人使用变量来解答。
# Write your MySQL query statement below
select distinct(l1.Num) as ConsecutiveNums from Logs l1, Logs l2, Logs l3 where l1.Id + 1 = l2.Id and l1.Id + 2 = l3.Id and l1.Num = l2.Num and l1.Num = l3.Num;
181. Employees Earning More Than Their Managers
查询工资比上司高的员工。使用自连接即可。
# Write your MySQL query statement below
select e1.Name as Employee from Employee as e1, Employee as e2 where e1.ManagerId = e2.Id and e1.Salary > e2.Salary;
查找表中有重复的emails。使用数据分组或自连接即可。
# Write your MySQL query statement below
select Email from Person group by Email having count(*) > 1;
# Write your MySQL query statement below
select distinct(p1.Email) from Person as p1, Person as p2 where p1.Id != p2.Id and p1.Email = p2.Email;
183. Customers Who Never Order
查出没有订单的客户。使用数据过滤即可。
# Write your MySQL query statement below
select Name as Customers from Customers where Customers.Id not in (select distinct(CustomerId) from Orders);
184. Department Highest Salary
查询每个部内最高的工资,最高工资可能有多个员工。这题相对来说稍复杂,使用子查询与数据分组来解决。
# Write your MySQL query statement below
select Department.Name as Department, Employee.Name as Employee, max(Salary) as Salary from Employee, Department where Employee.DepartmentId = Department.Id;
# 错误解法,结果会出现[null, null, null]这种情况
# Write your MySQL query statement below
select Department.Name as Department, Employee.Name as Employee, Salary from Employee, Department where Employee.DepartmentId = Department.Id order by Salary limit 1;
# 错误解法,结果只会有一行
# Write your MySQL query statement below
select d.Name as Department, e.Name as Employee, e.Salary as Salary
from Department as d, (select Name, max(Salary) as Salary, DepartmentId from Employee group by DepartmentId) as e
where d.Id = e.DepartmentId;
# 错误解法,当同一部门有两个最高的Salary时候,结果只有一行,故必须使用下面解法,使用2个Employee表
# Write your MySQL query statement below
select d.Name as Department, e.Name as Employee, e.Salary as Salary
from Department as d, Employee as e, (select max(Salary) as Salary, DepartmentId from Employee group by DepartmentId) as h
where d.Id = h.DepartmentId and e.Salary = h.Salary and e.DepartmentId = h.DepartmentId;
LeetCode Database题解的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LeetCode一句话题解
深度优先搜索 人生经验 1. 需要输出所有解.并由于元素集有重复元素,要求返回的结果需要去重的情况,可考虑使用值对应数量的map,然后分别考虑依次取不同数量该值的可能. LeetCode39 题目:给 ...
- [leetcode] 位操作题解
子集 题目[78]:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [ ...
- LeetCode 中等题解(3)
34 在排序数组中查找元素的第一个和最后一个位置 Question 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂 ...
- LeetCode 中等题解(1)
16 最接近的三数之和 Question 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和. ...
- leetcode database题目
LeetCode有10道SQL的题目,最近学习SQL语言,顺便刷题强化一下, 说实话刷完SQL学习指南这本书,不是很难,上面的例子 跟语法规则我都能理解透, 实际中来做一些比较难的业务逻辑题,却一下子 ...
随机推荐
- MySQL中文乱码修改
一.从服务端进行修改 show variables like "%char%"; 然后可能显示如下信息,注意红色部分,不同的用户可能实际情况不同,但是需要保证除了 filesyst ...
- 基于Verilog HDL 各种实验
菜鸟做的的小实验链接汇总: 1.基于Verilog HDL 的数字时钟设计 2.乘法器 3.触发器(基本的SR触发器.同步触发器.D触发器) 4.基于Verilog HDL的ADC ...
- Struts2--DomainModel接收参数---使用广泛!!!
1. JSP文件调用格式: <a href="user/user!add?user.name=a&user.age=8">添加用户</a> 2. s ...
- 改变MyEclipse创建JSP时默认的pageEncoding编码
如何改变MyEclipse创建JSP时默认的pageEncoding编码 有时我们需要改变MyEclipse创建JSP时默认的pageEncoding编码,因为也许它默认的编码不是我们想要的,比如我们 ...
- php的命名空间层级与目录层级是一致的吗?
php的命名空间和目录的层级之间并不是说一定 要一致,两者之间没有必然的联系.并没有直接的关联,当然了,推荐关联起来,不然管理会非常混乱,但你确实可以自己实现一个Autoload来管理“混乱”的nam ...
- Cocos2dx 3.1.1 学习笔记整理(3):逐帧动画
以下代码是在cocos中使用,plist+png 还有SpriteBatchNode播放动画的代码,备份一下,以防git被墙: bool GameMain::init() { if( !Layer:: ...
- ice grid 完整部署过程
待补充 一 理论准备 一个IceGrid集群有一个registry(注册表,用于定位)和多个node组成. IceGrid配置包括集群配置和应用配置: config.grid是集群配置,配置Regis ...
- Sematic库系列一
最近在做项目时采用了sematic css 库,由于这个库的资料太少,在做项目中遇到很多问题,在这里做一些记录 1. 下拉框demo HTML 代码 <div class="field ...
- list集合怎么转化成一个javaBean对象,及常见的使用方法(全)
一.List集合的用法 1.list集合添加实体并输出 for (int i = 0; i < list.size(); i++) { javabean obj= (javabean)list. ...
- VSC#2010打开视图编辑器假死/卡死
最近写项目代码的时候写C#,VSC#2010刚配置好,打开之前同学写的项目的设计界面结果...我擦,卡死了,重复几次都是这样. 于是上网搜发现是VS一个bug,打一个VSSP1补丁就好了~ http: ...