Problems
https://leetcode.com/problemset/database/
175. Combine Two Tables
# Write your MySQL query statement below
select FirstName,LastName,City,State
from Person left join Address
on Person.PersonId = Address.PersonId;
176. Second Highest Salary
# Write your MySQL query statement below
select max(Salary) as SecondHighestSalary
from Employee
where Salary < (select max(Salary) from Employee);
177. Nth Highest Salary
select * from tablename limit 0,1
即取出第一条记录。
select * from tablename limit 1,1
第二条记录
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
declare M int;
set M=N-1;
RETURN (
# Write your MySQL query statement below.
select distinct Salary from Employee order by Salary desc limit M , 1
);
END
178. Rank Scores
# Write your MySQL query statement below
select Score,(select count(1)+1
from (select distinct Score ss
from Scores
) tmp
where ss > Score
) Rank
from Scores
order by Score desc
180. Consecutive Numbers
# Write your MySQL query statement below
select distinct l1.Num as ConsecutiveNums
from Logs l1,Logs l2,Logs l3
where l1.Id = l2.Id - 1 and l1.Id = l3.Id - 2 and l1.Num = l2.Num and l2.Num = l3.Num;
181. Employees Earning More Than Their Managers
# Write your MySQL query statement below
select a.Name as Employee
from Employee a,Employee b
where a.ManagerId = b.Id and a.Salary > b.Salary;
182. Duplicate Emails
# Write your MySQL query statement below
select Email
from Person
group by Email
having count(1) > 1;
183. Customers Who Never Order
# Write your MySQL query statement below
select Name as Customers
from Customers
where Id not in
(
select distinct CustomerId
from Orders
);
184. Department Highest Salary
# Write your MySQL query statement below
select Department.Name Department,Employee.name Employee,Salary
from
(
select Department.Name,Max(Salary) maxs
from Department,Employee
where Employee.DepartmentId = Department.Id
group by Department.Name
) tmp,Department,Employee
where tmp.Name = Department.Name and Employee.DepartmentId = Department.Id and Salary = maxs;
185. Department Top Three Salaries
# Write your MySQL query statement below
select d.Name Department,e.name Employee,Salary
from Employee e,Department d
where DepartmentId = d.Id and (select count(distinct(e2.Salary))
from Employee e2
where e.DepartmentId = e2.DepartmentId and e.Salary < e2.Salary) < 3
order by d.Id,Salary desc;
196. Delete Duplicate Emails
# Write your MySQL query statement below
delete a
from Person a,Person b
where a.Email = b.Email and a.Id > b.Id;
197. Rising Temperature
# Write your MySQL query statement below
select a.Id
from Weather a,Weather b
where TO_DAYS(a.Date)=TO_DAYS(b.Date)+1 and a.Temperature > b.Temperature;
262. Trips and Users
# Write your MySQL query statement below
select Request_at Day,round(sum(case when Status = 'completed' then 0 else 1 end) / count(1) ,2) 'Cancellation Rate'
from Trips
where Client_Id in
(
select Users_Id
from Users
where Banned = 'No' and Role = 'client'
) and Request_at >= '2013-10-01' and Request_at <= '2013-10-03'
group by Request_at;
网友评论