Combine Two Tables
1 # Write your MySQL query statement below 2 Select p.FirstName, p.LastName, a.City, a.State from Person p left join Address a on p.PersonId = a.PersonId
Consecutive Numbers
1 # Write your MySQL query statement below 2 select distinct num from ( 3 select num, count(rank) as cnt from ( 4 select num, @cur := @cur + if(@prev = num, 0, 1) as rank, @prev := num 5 from Logs s, (select @cur := 0) r, (select @prev := null) p 6 order by id asc 7 ) t group by rank having cnt >= 3 8 ) n; 9
个人觉得还是用下面这个简单的好
1 # Write your MySQL query statement below 2 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
1 # Write your MySQL query statement below 2 select c.Name from Customers c left join Orders o on c.Id = o.CustomerId where o.Id is null
1 # Write your MySQL query statement below 2 select c.Name from Customers c where c.Id not in (select CustomerId from Orders o)
1 # Write your MySQL query statement below 2 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
1 # Write your MySQL query statement below 2 delete p1 from Person p1 inner join Person p2 where p1.email = p2.email and p1.Id > p2.Id
1 # Write your MySQL query statement below 2 delete from p1 using Person p1 inner join Person p2 where p1.email = p2.email and p1.Id > p2.Id
1 # Write your MySQL query statement below 2 delete from Person where Id not in (select * from (select min(Id) from Person p group by Email) t)
注意下面这个语句会出现BUG
1 # Write your MySQL query statement below 2 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
1 # Write your MySQL query statement below 2 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
下面这段代码更加容易理解
1 # Write your MySQL query statement below 2 select d.Name Department, e.Name Employee, s.Salary from ( 3 select MAX(e.Salary) Salary, e.DepartmentId from Employee e, Department d where e.DepartmentId = d.Id group by e.DepartmentId 4 ) s, Employee e, Department d where s.Salary = e.Salary and e.DepartmentId = d.Id and e.DepartmentId = s.DepartmentId;
Departmet Top Three Salaries
1 # Write your MySQL query statement below 2 select D.Name as Department, E.Name as Employee, E.Salary as Salary 3 from Employee E, Department D 4 where (select count(distinct(Salary)) from Employee where DepartmentId = E.DepartmentId and Salary > E.Salary) in (0, 1, 2) 5 and 6 E.DepartmentId = D.Id 7 order by E.DepartmentId, E.Salary DESC;
太难了。。
Duplicate Emails
1 # Write your MySQL query statement below 2 select distinct(p.Email) from Person p, Person q where p.Id != q.Id and p.Email = q.Email
1 # Write your MySQL query statement below 2 select Email from Person group by Email having count(*) > 1
Employees Earning More Than Their Managers
1 # Write your MySQL query statement below 2 select e1.Name from Employee e1, Employee e2 where e1.ManagerId = e2.Id and e1.Salary > e2.Salary
Rank Scores
1 # Write your MySQL query statement below 2 select Score, Rank from ( 3 select Score, @cur := @cur + if(@prev = Score, 0, 1) as Rank, @prev := Score from 4 Scores s, (select @cur := 0) r, (select @prev := null) p order by Score desc) t
Rising Temperature
1 # Write your MySQL query statement below 2 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
1 # Write your MySQL query statement below 2 select max(Salary) from Employee where Salary < (select max(Salary) from Employee)