[leetcode] database解题记录
题目:左连接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
题目:写一条sql语句查询Employee
表中第二高工资值。
select max(Salary ) from Employee where Salary not in(select max(Salary ) from Employee)
7个case耗时1001ms --2015/1/11
查询第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
找出至少连续出现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
题目:查出没有订单的客户。
select Name as Customers from Customers where Id not in(select distinct(CustomerId) from Orders); #693ms
题目:查询每个部门最高的工资。
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解题记录的更多相关文章
- LeetCode解题记录(贪心算法)(二)
1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- pwnable.kr input解题记录
pwnable input解题记录 给了源码如下: #include "stdio.h" #include "unistd.h" #include " ...
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- leetcode网解题心得——61. 旋转链表
目录 leetcode网解题心得--61. 旋转链表 1.题目描述 2.算法分析: 3.用自然语言描述该算法 4.java语言实现 5.C语言实现 leetcode网解题心得--61. 旋转链表 1. ...
- angr脚本——以angrctf解题记录为参考
angr脚本--以angrctf解题记录为参考 angr是用于逆向工程中进行二进制分析的一个python框架 符号执行 (Symbolic Execution)是一种程序分析技术.其可以通过分 ...
- ssrf解题记录
ssrf解题记录 最近工作需要做一些Web的代码审计,而我Web方面还比较薄弱,决定通过一些ctf的题目打打审计基础,练练思维,在博客上准备开几个专题专门记录刷题的过程. pwn题最近做的也很少,也要 ...
- Leetcode解题记录
尽量抽空刷LeetCode,持续更新 刷题记录在github上面,https://github.com/Zering/LeetCode 2016-09-05 300. Longest Increasi ...
随机推荐
- TOJ 3134: 渊子赛马修改版
3134: 渊子赛马修改版 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 458 ...
- ASP.NET(一):Reques对象和Response对象的区别,以及IsPostBack属性的用法
导读:在ASP.NET的学习中,初步认识了其6大对象(严格说来只能算是属性):Request,Response,Application,Session,Server,OjectContext.这些对象 ...
- nginx,lvs,haproxy负载均衡对比
Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术,具体的应用需求还得具体分析. 如果是中小型的Web应用,比 ...
- [UOJ#129][BZOJ4197][Noi2015]寿司晚宴
[UOJ#129][BZOJ4197][Noi2015]寿司晚宴 试题描述 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司 ...
- 【2018.10.15】WZJ笔记(数论)
1. 证明:对于任意质数$p\gt 3$,$p^2-1$能被$24$整除. 证:平方差公式,$p^2-1 = (p-1)(p+1)$. 再把$24$分解质因数$2^3*3$. 三个相邻的自然数中至少有 ...
- 点击不同按钮,加载不同的页面(不使用iframe的情况下)
<button id="button1">Load Html1</button> <button id="button2"> ...
- 安装sqlServer2012失败补救
今天拿着新电脑win10,装数据库2012,装了第一次,没装上,有一半的工具都失败了,慌了.. 连management studio都没装上,我用navicat也连不上. 卸了,第二次安装,装一半卡住 ...
- Requests模拟登陆
requests模拟登陆知乎网站 实例 # -*- coding: utf-8 -*- __author__ = 'CQ' import requests try: import cookielib ...
- android自定义Activity窗体大小
先给大家看图吧: 看,是不是很酷呢,呵呵. 这里我说关键的地方,就是自定义Activity的窗体大小. 这个登录框它不是一个Dialog,而是一个Activity. 如何定义,即把Activity的主 ...
- MySql的架构和历史
1.1.mysql的逻辑架构 架构为如下: 存储引擎:负责数据的储存和提取,供了几十个API供服务层进行调用.各个存储引擎之间不会进行交互,只是供服务层进行调用.事务控制和锁的管理也是在存储引擎里面进 ...