# Write your MySQL query statement below select ifnull( (select Salary from (select@n :=@n+1as ranking, Salary from (select@n :=0) as a, (selectdistinct Salary from Employee) as b orderby Salary desc) as c where c.ranking =2), null) as SecondHighestSalary
解2:
1 2
# Write your MySQL query statement below select ifnull((selectdistinct Salary from Employee orderby Salary desc limit 1,1),null) as SecondHighestSalary
错误解法:
1 2 3 4 5 6 7 8 9
# Write your MySQL query statement below select IF(Salary isnotnull, Salary, null) as SecondHighestSalary from (select@n :=@n+1as ranking, Id, Salary from (select@n :=0) a, Employee b orderby Salary desc) as c where c.ranking =2; #返回的是{"headers": ["SecondHighestSalary"], "values": []} #而预期是{"headers": ["SecondHighestSalary"], "values": [[null]]}
知识点
为查询结果增加一个自增列
1 2 3 4 5 6
#两句 set@rownum=0; select (@rownum:=@rownum+1),colname from [tablename or (subquery) a];
#合成一句 select@rownum:=@rownum+1,colnum from (select@rownum:=0) a,[tablename or (subquery) b];
CREATEFUNCTION getNthHighestSalary(N INT) RETURNSINT BEGIN RETURN ( # Write your MySQL query statement below. select ifnull( (select Salary from (select@n :=@n+1as ranking, Salary from (select@n :=0) as a, (selectdistinct Salary from Employee) as b orderby Salary desc) as c where c.ranking = N), null) as getNthHighestSalary ); END
解2(但是如果只能在return里作答的话,set那句加在了非答题区域):
1 2 3 4 5 6 7 8 9
CREATEFUNCTION getNthHighestSalary(N INT) RETURNSINT BEGIN SET N = N -1; RETURN ( # Write your MySQL query statement below. select ifnull((selectdistinct Salary from Employee orderby Salary desc limit N,1), null) as getNthHighestSalary ); END
# Write your MySQL query statement below select Score,dense_rank() over(orderby Score desc) as `Rank` from Scores
解2(没有窗口函数时):
1 2 3 4 5
# Write your MySQL query statement below select Score, (selectcount(distinct Score) from Scores as a where a.Score >= b.Score) as `Rank` from Scores as b orderby Score desc
# Write your MySQL query statement below selectdistinct a.Num as ConsecutiveNums from `Logs` a, `Logs` b, `Logs` c where (a.Id +1= b.Id and a.Num = b.Num) and (a.Id +2= c.Id and a.Num = c.Num)
解2(利用lag和lead函数):
1 2 3 4 5
# Write your MySQL query statement below selectdistinct Num as ConsecutiveNums from (select Num, lag(Num, 1) over () as front, lead(Num, 1) over () as behind from `Logs`) as c where Num = front and Num = behind
评论里的一种利用case-when语句的方法:
1 2 3 4 5 6 7 8 9 10 11
selectdistinct Num as ConsecutiveNums from ( select Num, case when@prev= Num then@count :=@count+1 #如果上一行已经给prev赋值了,这一行还是同一个Num的话,就会触发这一句when,然后count+1计数 when (@prev := Num) isnotnullthen@count :=1 #给prev变量赋新值并且直接进入count+1计数,因为isnotnull默认为true endas CNT from Logs, (select@prev :=null, @count :=null) as t ) as temp where temp.CNT >=3 #3换成任何值可以满足不同题目给出的连续N次出现的数字
# Write your MySQL query statement below select a.Name as `Employee` from Employee as a leftjoin Employee as b on a.ManagerId = b.Id where a.ManagerId isnotnull and a.Salary > b.Salary
# Write your MySQL query statement below select `Name` as `Customers` from Customers as c leftjoin Orders as o on c.Id = o.CustomerId where o.Id isnull
# Write your MySQL query statement below select d.`Name` as `Department`, b.`Name` as `Employee`, b.Salary from (select a.`Name`, a.Salary, a.DepartmentId from Employee a rightjoin (select DepartmentId, max(Salary) as max_s from Employee groupby DepartmentId) c on a.Salary = c.max_s and a.DepartmentId = c.DepartmentId) b innerjoin Department as d on b.DepartmentId = d.Id
# Write your MySQL query statement below select Department, Employee, Salary from (select d.`Name` as `Department`, e.`Name` as Employee, e.Salary, dense_rank() over (partitionby DepartmentId orderby Salary desc) as ranking from Employee e innerjoin Department d on e.DepartmentId = d.Id) a where a.ranking <=3
# Write your MySQL query statement below select w1.Id from Weather w1, Weather w2 where DATE_SUB(w1.recordDate, INTERVAL1DAY) = w2.recordDate and w1.temperature > w2.temperature
# Write your MySQL query statement below select Request_at as `Day`, cast(count(Status !='completed'ornull) /count(*) asdecimal(10, 2)) as `Cancellation Rate` from Trips as t, (select*from Users where Role ='client') as cli, (select*from Users where Role ='driver') as dri where t.Client_Id = cli.Users_Id and t.Driver_Id = dri.Users_Id and cli.Banned ='No' and #dri.Banned='No'and t.Request_at between'2013-10-01'and'2013-10-03' groupby Request_at
注:倒数第三行注释掉是因为中英文版力扣题意不一样,英文定义的非禁止用户原话是“unbanned users (both client and driver must not be banned)”,中文版翻译直接把括号部分忽略掉。忽略就算了吧我以为出题人只是语文不及格,结果有一个测试用例就是愉悦地在Users表里设置了3个Banned的司机,计算出来的非禁止用户生成订单也把这些司机的订单算进去了,Banned字段有效的仅仅是乘客,原来出题人思想道德也不及格,这道题“困难”水平是难在了这里,我直接给你妈拍手叫好。
# Write your MySQL query statement below select f.Id as id, f.visit_date, f.people from (select* from (select*, lead(b.CNT, 1) over () as after from (select*, case when s.people >=100then@count :=@count+1 when s.id isnotnullthen@count :=0 endas CNT from Stadium as s, (select@count :=0) as c) as b) as d where (d.after =0or d.after isnull) and d.CNT >=3) as e, Stadium as f where f.Id between e.Id - e.CNT +1and e.Id
评论区提到的利用窗口函数的另一种没这么笨重的方法(用了一个临时表):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Write your MySQL query statement below selectdistinct t2.* from ( select*, lead(people, 1) over (orderby visit_date ) as p2, lead(people, 2) over (orderby visit_date ) as p3 from Stadium ) t, Stadium t2 where t.people >=100 and p2 >=100 and p3 >=100 and t2.id >= t.id and t2.id -2<= t.id
# Write your MySQL query statement below select id, movie, description, rating from cinema where description !='boring' and id %2!=0 orderby rating desc
就这么简单一道题还有人玩得骚,用位运算,奇数和1做&操作得到1,偶数和1做&操作得到0。:
1 2 3 4 5 6 7 8 9 10 11 12
SELECT id, movie, description, rating FROM cinema WHERE id &1 AND description <>'boring' ORDERBY rating DESC
# Write your MySQL query statement below select id, ifnull(case when id &1thenlead(student, 1) over () when id isnotnullthenlag(student, 1) over () end, student) as `student` from seat