175 Combine Two Tables

题目:左连接Person表和Address表。

select  FirstName,LastName,City,State from Person p left join Address a on p.PersonId=a.PersonId;

7个case耗时1001ms(注意耗时多次提交会不同,一般相差不大,偶尔也有相差大的)   --2015/1/11

176 Second Highest Salary

题目:写一条sql语句查询Employee 表中第二高工资值。

select max(Salary ) from Employee  where Salary  not in(select max(Salary ) from Employee)

7个case耗时1001ms   --2015/1/11

177 Nth Highest Salary

查询第n大的值。

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
#14 cases 925ms

178 Rank Scores

按得分排名并计算排名。

select Scores.score,count(Ranking.Score) as rank from
Scores,
(select distinct score from Scores) Ranking
where Scores.score <= Ranking.Score
group by Scores.id,Scores.score
order by Scores.score desc;
#955ms

180 Consecutive Numbers

找出至少连续出现3次的Num。这道题确实不知道怎么下手,看了答案理解了一下。

select DISTINCT num FROM
(select num,
case
when @record = num then @count:=@count+1
when @record <> num then @count:=1 end as n
from
Logs ,(select @count:=0,@record:=(SELECT num from Logs limit 0,1)) r
) a
where a.n>=3

这个居然也可以过?

select distinct(a.Num) from Logs a, Logs b,Logs c where a.Id=b.Id+1 and a.Num=b.Num and b.Id=c.Id+1 and b.Num=c.Num #21 cases 1577ms

181 Employees Earning More Than Their Managers

查询工资比上司高的员工。

select a.name as Employee from Employee a left join Employee b on a.ManagerId = b.Id where a.Salary>b.Salary #14 cases 1336ms
select a.Name from Employee a inner join Employee b on a.ManagerId=b.Id where a.Salary>b.Salary #用内联时间应该更短

182 Duplicate Emails

题目:查找表中有重复的emails。

这里提供四种方法:

select Email from Person group by Email having count(*) > 1;
#14 cases 1069ms
select distinct a.Email from Person a join Person b on (a.Email = b.Email) where a.Id <> b.Id;#14 cases 1048ms select distinct a.Email from Person a where exists(select 1 from Person b where a.Email = b.Email limit 1, 1); #14 cases 1138ms select distinct a.Email from Person a left join (select Id,Email from Person group by Email) b on (a.Email = b.Email) and (a.Id=b.Id) where b.Email is NULL;#14 cases 1060ms

183 Customers Who Never Order

题目:查出没有订单的客户。

select Name as Customers from Customers where Id not in(select distinct(CustomerId) from Orders); #693ms

184 Department Highest Salary

题目:查询每个部门最高的工资。

select D.Name as Department, E.Name as Employee, E.Salary as Salary from  Employee E ,Department D where  E.DepartmentId=D.Id AND (DepartmentId,Salary) in (SELECT DepartmentId,max(Salary) as max FROM Employee GROUP BY DepartmentId) ;#1736ms

[leetcode] database解题记录的更多相关文章

  1. LeetCode解题记录(贪心算法)(二)

    1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...

  2. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  3. pwnable.kr input解题记录

    pwnable input解题记录 给了源码如下: #include "stdio.h" #include "unistd.h" #include " ...

  4. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  5. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  6. leetcode网解题心得——61. 旋转链表

    目录 leetcode网解题心得--61. 旋转链表 1.题目描述 2.算法分析: 3.用自然语言描述该算法 4.java语言实现 5.C语言实现 leetcode网解题心得--61. 旋转链表 1. ...

  7. angr脚本——以angrctf解题记录为参考

    angr脚本--以angrctf解题记录为参考 ​ angr是用于逆向工程中进行二进制分析的一个python框架 ​ 符号执行 (Symbolic Execution)是一种程序分析技术.其可以通过分 ...

  8. ssrf解题记录

    ssrf解题记录 最近工作需要做一些Web的代码审计,而我Web方面还比较薄弱,决定通过一些ctf的题目打打审计基础,练练思维,在博客上准备开几个专题专门记录刷题的过程. pwn题最近做的也很少,也要 ...

  9. Leetcode解题记录

    尽量抽空刷LeetCode,持续更新 刷题记录在github上面,https://github.com/Zering/LeetCode 2016-09-05 300. Longest Increasi ...

随机推荐

  1. Python第三方库之openpyxl(8)

    Python第三方库之openpyxl(8) 饼图 饼图将数据绘制成一个圆片,每个片代表整体的百分比.切片是按顺时针方向绘制的,0在圆的顶部.饼图只能取一组数据.该图表的标题将默认为该系列的标题. 2 ...

  2. Matplotlib基本图形之直方图

    Matplotlib基本图形之直方图 直方图特点 由一系列高度不等的纵向条形组成,表示数据分布情况例如年级同学身高分布注意与条形图的区别 示例代码: import osimport numpy as ...

  3. x86保护模式 任务状态段和控制门

    x86保护模式    任务状态段和控制门 每个任务都有一个任务状态段TSS     用于保存任务的有关信息     在任务内权变和任务切换时  需要用到这些信息    任务内权变的转移和任务切换  一 ...

  4. xtu数据结构 C. Ultra-QuickSort

    C. Ultra-QuickSort Time Limit: 7000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java ...

  5. 九度oj 题目1091:棋盘游戏

    题目描述: 有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代价最小的路径:    1.只能沿上下左右四个方向移动    2.总代价是没走一步的 ...

  6. BZOJ 3473 字符串 ——广义后缀自动机

    这题就比较有趣了. 首先匹配一遍,然后统计子树叶子节点中包含大于等于k的节点个数(HH的项链) 然后就可以搞了. 关于合法的情况数,显然是l[i]-l[fa[i]],然后向下下传即可(YY一下). # ...

  7. Redis的数据类型及相关操作命令

    redis 基础内容 —— redis的数据类型及相关操作的Linux命令.所谓大厦千层基础承载,希望大家认真学习这一讲: 一.redis 的五大数据类型: 1.String(字符串): 2.List ...

  8. P1681 最大正方形II (动态规划)

    题目背景 忙完了学校的事,v神终于可以做他的"正事":陪女朋友散步.一天,他和女朋友走着走着,不知不觉就来到了一个千里无烟的地方.v神正要往回走,如发现了一块牌子,牌子上有有一行小 ...

  9. list或map 打印成json 方便调试

    private final Logger logger = Logger.getLogger(this.getClass()); logger.info(JSON.toJSONStringWithDa ...

  10. Eclipse、IDEA安装JavaFX

    简介 JavaFX插件 e(fx)eclipse是一组工具和必要的库,它们帮助您执行JavaFX编程,确保您已经作为插件将其安装在eclipse上了. JavaFX Scene Builder Jav ...