Combine Two Tables

 # Write your MySQL query statement below
Select p.FirstName, p.LastName, a.City, a.State from Person p left join Address a on p.PersonId = a.PersonId

Consecutive Numbers

 # Write your MySQL query statement below
select distinct num from (
select num, count(rank) as cnt from (
select num, @cur := @cur + if(@prev = num, 0, 1) as rank, @prev := num
from Logs s, (select @cur := 0) r, (select @prev := null) p
order by id asc
) t group by rank having cnt >= 3
) n;

个人觉得还是用下面这个简单的好

 # Write your MySQL query statement below
select distinct l1.Num 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;

Customers Who Never Order

 # Write your MySQL query statement below
select c.Name from Customers c left join Orders o on c.Id = o.CustomerId where o.Id is null
 # Write your MySQL query statement below
select c.Name from Customers c where c.Id not in (select CustomerId from Orders o)
 # Write your MySQL query statement below
select c.Name from Customers c where not exists (select c.Id from Orders o where o.CustomerId = c.Id)

in和exists的区别参考http://www.cnblogs.com/seasons1987/archive/2013/07/03/3169356.html

可以这么理解,我们偏向于:

select * from 大表 where cc in (select cc from 小表); 因为in相当于内表(这里是小表)与外表的hash连接,因此内表应选择小表

select * from 小表 where exists (select cc from 大表 where cc = 小表.cc); 因为exists是先对外表(这里是小表)做loop,再在后面exists里面的语句(内表)进行查询,因此外表要选择小表

Delete Duplicate Emails

 # Write your MySQL query statement below
delete p1 from Person p1 inner join Person p2 where p1.email = p2.email and p1.Id > p2.Id
 # Write your MySQL query statement below
delete from p1 using Person p1 inner join Person p2 where p1.email = p2.email and p1.Id > p2.Id
 # Write your MySQL query statement below
delete from Person where Id not in (select * from (select min(Id) from Person p group by Email) t)

注意下面这个语句会出现BUG

 # Write your MySQL query statement below
delete from Person where Id not in (select min(Id) from Person p group by Email)

错误提示为:You can't specify target table 'Person' for update in FROM clause

这个错误为在MySQL中,禁止在FROM子句中指定被更新的目标表。也就是说用Min或者max的时候要在外面再套一层select,这个错误只有在MYSQL中有,在MSSQL和Oracal中没有(好奇怪。。)

Department Highest Salary

 # Write your MySQL query statement below
select d.name as Department, t.Salary, e.Name as Employee from Employee e inner join (select DepartmentId, max(salary) as Salary from Employee group by DepartmentId) t using (DepartmentId, Salary) inner join Department d on d.Id = t.DepartmentId

SQL的顺序一般从from后面开始,再是select,最后是order by

下面这段代码更加容易理解

 # Write your MySQL query statement below
select d.Name Department, e.Name Employee, s.Salary from (
select MAX(e.Salary) Salary, e.DepartmentId from Employee e, Department d where e.DepartmentId = d.Id group by e.DepartmentId
) s, Employee e, Department d where s.Salary = e.Salary and e.DepartmentId = d.Id and e.DepartmentId = s.DepartmentId;

Departmet Top Three Salaries

 # Write your MySQL query statement below
select D.Name as Department, E.Name as Employee, E.Salary as Salary
from Employee E, Department D
where (select count(distinct(Salary)) from Employee where DepartmentId = E.DepartmentId and Salary > E.Salary) in (0, 1, 2)
and
E.DepartmentId = D.Id
order by E.DepartmentId, E.Salary DESC;

太难了。。

Duplicate Emails

 # Write your MySQL query statement below
select distinct(p.Email) from Person p, Person q where p.Id != q.Id and p.Email = q.Email
 # Write your MySQL query statement below
select Email from Person group by Email having count(*) > 1

Employees Earning More Than Their Managers

 # Write your MySQL query statement below
select e1.Name from Employee e1, Employee e2 where e1.ManagerId = e2.Id and e1.Salary > e2.Salary

Rank Scores

 # Write your MySQL query statement below
select Score, Rank from (
select Score, @cur := @cur + if(@prev = Score, 0, 1) as Rank, @prev := Score from
Scores s, (select @cur := 0) r, (select @prev := null) p order by Score desc) t

Rising Temperature

 # Write your MySQL query statement below
select w1.Id from Weather w1 join Weather w2 where to_days(w1.Date) = to_days(w2.Date) + 1 and w1.Temperature > w2.Temperature

Second Highest Salary

 # Write your MySQL query statement below
select max(Salary) from Employee where Salary < (select max(Salary) from Employee)

DataBase: LeetCode的更多相关文章

  1. leetcode

    Coding on LeetCode Online Judge leetcode(leetcode website) Problems algorithms 13. Roman to Integer ...

  2. leetcode database题目

    LeetCode有10道SQL的题目,最近学习SQL语言,顺便刷题强化一下, 说实话刷完SQL学习指南这本书,不是很难,上面的例子 跟语法规则我都能理解透, 实际中来做一些比较难的业务逻辑题,却一下子 ...

  3. leetcode - database - 177. Nth Highest Salary (Oracle)

    题目链接:https://leetcode.com/problems/nth-highest-salary/description/ 题意:查询出表中工资第N高的值 思路: 1.先按照工资从高到低排序 ...

  4. LeetCode Database: Delete Duplicate Emails

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  5. LeetCode Database: Rank Scores

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  6. LeetCode Database: Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  7. LeetCode Database题解

    175. Combine Two Tables 使用外连接即可. # Write your MySQL query statement below select FirstName, LastName ...

  8. leetcode中Database题(一)

    Combine Two Tables Table: Person +-------------+---------+ | Column Name | Type | +-------------+--- ...

  9. [leetcode] database解题记录

    175 Combine Two Tables 题目:左连接Person表和Address表. select FirstName,LastName,City,State from Person p le ...

随机推荐

  1. Nginx 禁用IP IP段

    最近公司网站被竞争对手用爬虫频繁访问,所以我们这边要禁止这些爬虫访问,我们通过nginx 指令就可以实现了 方法一:直接在LB机器上封IP 1.在 blocksip.conf 文件中加入要屏蔽的ip或 ...

  2. Python 虚拟环境Virtualenv

    本人也是Python爱好者,众所周知,Python扩展多,每次为了测试,安装各种各样的扩展,这样导致本地的Python环境非常混乱,就有人想到搞个隔离环境  和 本地环境没有关系,随时可以删除这个隔离 ...

  3. 蓝牙—服务发现协议(SDP)

    服务搜索协议(SDP)提供了应用发现可用服务以及确定可用服务特点的方法.SDP发现协议提供下面的能力 <1>为客户提供搜索所需要服务的能力. <2>允许基于服务类型搜索服务 & ...

  4. CC2540的使用入门

    目录 1. 介绍 2. 开发环境 3. SDCC 1. 介绍 CC2540是一款2.4GHz Bluetooth® low energy SOC,基于8051 MCU 首先,你需要硬件设备 笔者的开发 ...

  5. CDH(Cloudera)与hadoop(apache)对比

    本文出自:CDH(Cloudera)与hadoop(apache)对比http://www.aboutyun.com/thread-9225-1-1.html(出处: about云开发)   问题导读 ...

  6. 大数据下的java client连接JDBC

    1.前提 启动hiveserver2服务 url,username,password 2.程序 3.结果 emp的第一列与第二列

  7. zepto源码--qsa--学习笔记

    zepto内部选择器qsa方法的实现. 简述实现原理: 通过判断传入的参数类型: 如果是'#id',则使用getElementById(id)来获取元素,并且将结果包装成数组形式: 如果是'.clas ...

  8. C#程序中从数据库取数据时需注意数据类型之间的对应,int16\int32\int64

    private void btn2_Click(object sender, RoutedEventArgs e)         {             using (SqlConnection ...

  9. LightOj1285 - Drawing Simple Polygon(连接多边形各点)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1285 题意:给你一些点,然后把它们用一条线把它们连起来,构成一个多边形,不能有相交,必 ...

  10. Interview How to Count Squares

    火柴拼出多少个正方形 http://matchstickpuzzles.blogspot.com/2011/06/55-4x4-square-how-many-squares.html 输入是两个二维 ...