力扣MySQL刷題記錄(一)

刷題記錄
每道題,自己思路一個,吸取評論區思路一個,若和評論區答案差不多一樣,則保留一個

-- https://leetcode-cn.com/problems/big-countries/
select name , population,area from World where area>3000000 or population> 25000000;

--https://leetcode-cn.com/problems/classes-more-than-5-students/solution/
select class from courses group by class having count(distinct student)>=5 ;

--https://leetcode-cn.com/problems/duplicate-emails/submissions/
--知道使用group by和having。還需要記得優先順序。where>group by>having>order by
--兩種方法都實現了
select Email from Person group by Email having count(distinct Id)>1 ;
select Email from Person group by Email having count(Email) >=2

--https://leetcode-cn.com/problems/not-boring-movies/
select * from cinema where id%2 !=0 and description != 'boring' order by rating desc;
select * from cinema where description <> 'boring' and mod(id,2)=1 order by rating desc;

--https://leetcode-cn.com/problems/swap-salary/
update salary set sex=if(sex = 'f', 'm','f');


--https://leetcode-cn.com/problems/combine-two-tables/
select FirstName, LastName, City, State
from Person left join Address
on Person.PersonId = Address.PersonId

--https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/
-- 沒做出來,看解析學的答案
select a.name as Employee
from employee a inner join employee b on a.managerid = b.id 
and a.salary > b.salary

--https://leetcode-cn.com/problems/delete-duplicate-emails/solution/
DELETE p1 
FROM Person as p1 , Person as p2
WHERE p1.Email = p2.Email  AND p1.Id > p2.Id

--
--我寫了一個不用轉化data的,但這個錯了.因爲給的樣例中id隨日期列增加,但是測試集 id隨着 日期反序排列
select w1.id
from weather w1,weather w2  
where w1.id= w2.id+1  and w1.temperature  > w2.temperature 

--需要改變日期格式後的對比代碼'datediff'
SELECT weather.id AS 'Id'
FROM weather JOIN weather w ON DATEDIFF(weather.recorddate, w.recorddate) = 1  AND weather.Temperature > w.Temperature

--https://leetcode-cn.com/problems/second-highest-salary/submissions/
--從去掉最大的後的取最大,那就是找到第二了
select max(Salary) as SecondHighestSalary from Employee 
where Salary<(select max(Salary) from Employee)
20191124更新

第二篇SQL刷題記錄,點擊此處跳轉閱讀

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章