175. Combine Two Tables
FirstName, LastName, City, State
select a.FirstName, a.LastName, b.City, b.State
from Person a
left join Address b
on a.PersonId = b.PersonId
176. Second Highest Salary
select
(select distinct Salary
from Employee
order by Salary DESC
limit 1,1) as SecondHighestSalary;
177. Nth Highest Salary
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N = N-1; # 先令N=N-1,limit那里不能做运算
RETURN (
select distinct Salary
from Employee
order by Salary desc
limit N,1
);
END
178. Rank Scores
select Score,
(select count(*) from (select distinct Score s from Scores) tmp
where s >= Score) as Rank
from Scores
order by Score desc
180. Consecutive Numbers
select distinct l1.Num as ConsecutiveNums
from
Logs l1,
Logs l2,
Logs l3
where
l1.id = l2.id - 1
and l2.id = l3.id - 1
and l1.Num = l2.Num
and l2.Num = l3.Num
;