MySQL
MySQL
菱形继承
懒癌患者,IT民工,前进路上经常磕磕碰碰
展开
-
Leetcode 618. 学生地理信息报告(分组行转列)
select min(case when continent='America' then name end) America, min(case when continent='Asia' then name end)Asia, min(case when continent='Europe' then name end)Europe from( select *,row_number() over(partition by continent or...原创 2021-05-05 10:08:45 · 157 阅读 · 0 评论 -
Leetcode 612. 平面上的最近距离
select round( pow(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2),1/2),2 ) shortest from point_2d p1 inner join point_2d p2 where p1.x<>p2.x or p1.y<>p2.y order by 1 asc limit 1原创 2021-04-28 16:57:11 · 190 阅读 · 0 评论 -
Leetcode 610. 判断三角形
select * , case when x+y>z and y+z>x and x+z>y then 'Yes' else 'No' end triangle from triangle原创 2021-04-27 17:11:57 · 139 阅读 · 0 评论 -
Leetcode 608. 树节点(case when)
select id, case when p_id is null then 'Root' when id in (select p_id from tree) then 'Inner' else 'Leaf' end typefrom tree原创 2021-04-26 23:00:43 · 118 阅读 · 0 评论 -
Leetcode 607. 销售员(经典嵌套子查询)
select name from salesperson s where sales_id not in ( select sales_id from orders where com_id=( select com_id from company c where name='RED' ) )原创 2021-04-25 16:24:54 · 109 阅读 · 0 评论 -
Leetcode 603. 连续空余座位
(求连续的几个序号都可以用lag和lead这种套路,新增列然后判断)select seat_id from (select * ,lag(free,1) over(order by seat_id) l1,head(free,1) order(order by seat_id) l2 from cinema) A where free=1 and (l1=1 or l2=1)...原创 2021-04-24 23:29:50 · 132 阅读 · 0 评论 -
Leetcode 602. 好友申请 II :谁有最多的好友
本质上就是统计两列中相同数字的最大值,注意到limit 1表示取第一行limit x,y表示从第x+1行开始取y行select id1 id,count(*) num from( select requester_id id1,accepter_id id2 from request_accepted r1 union select accepter_id id1,requester_id id2 ..原创 2021-04-17 14:44:18 · 100 阅读 · 0 评论 -
Leetcode 601. 体育馆的人流量(lag 和head 函数)
select id,visit_date,people from( select *, lag(people,1) over(order by id)l1, lag(people,2)over(order by id) l2, lead(people,1) over(order by id)l3, lead(people,2) over(order by id)l4 from stadium ...原创 2021-04-17 14:21:53 · 191 阅读 · 0 评论 -
Leetcode 597. 好友申请 I:总体通过率(round,计数)
select round( ifnull( 1/(count( distinct f1.sender_id,f1.send_to_id) /count(distinct r1.requester_id,r1.accepter_id)),0),2) accept_rate from friendrequest f1 , reques...原创 2021-04-16 22:52:25 · 166 阅读 · 0 评论 -
Leetcode 596. 超过5名学生的课(group by + having
select class from courses group by 1 having count(distinct student)>=5原创 2021-04-16 22:31:09 · 98 阅读 · 0 评论 -
Leetcode. 1294. 不同国家的天气类型(非常经典的order by +where限制分组查询)
select c.country_name, case when avg(w.weather_state)<=15 then "Cold" when avg(w.weather_state)<25 then "Warm" else "Hot" end weather_type from countries c inner join weather w on c.country_id =w.country_id .原创 2021-04-14 21:43:04 · 119 阅读 · 0 评论 -
Leetcode 595. 大的国家
select name,population,area from world where area>3000000 or population>25000000原创 2021-04-13 17:05:19 · 97 阅读 · 0 评论 -
Leetcode 586. 订单最多的客户
select customer_number from orders group by 1 order by count(*) desclimit 0,1原创 2021-04-13 17:03:04 · 111 阅读 · 0 评论 -
Leetcode 585. 2016年的投资 (partion by 的经典使用)
select round(sum(tiv_2016),2) tiv_2016 from (select *,count(*) over(partition by tiv_2015) a1,count(*) over(partition by lat,lon) a2 from insurance) as a where a1>1 and a2=1原创 2021-04-13 16:51:06 · 128 阅读 · 0 评论 -
Leetcode 584. 寻找用户推荐人(水题)
select name from customer where referee_id <>2 or isnull(referee_id)原创 2021-04-12 11:33:43 · 143 阅读 · 0 评论 -
Leetcode 580. 统计各专业学生人数(常规聚合 寻找非空字段计数)
select dept_name,count(student_id) student_number from department d left join student s on d.dept_id=s.dept_id group by dept_name order by 2 desc,1原创 2021-04-12 11:31:41 · 200 阅读 · 0 评论 -
Leetcode 579. 查询员工的累计薪水(lag 前瞻函数 查看前几行 分组聚合经典例子)
select id,month,salary+ifnull(l1,0)+ifnull(l2,0) salary from (select *,lag(salary,1) over(partition by id order by month ) l1, lag(salary,2) over(partition by id order by month)l2, rank() over(partition by id order by month desc) r ...原创 2021-04-07 15:57:35 · 146 阅读 · 0 评论 -
Leetcode 262. 行程和用户(avg函数的巧用)
select request_at "Day",round(avg(`status`!="completed"),2) "Cancellation Rate" from trips t join users u1 on u1.users_id=t.client_id and u1.banned="No" join users u2 on u2.users_id=t.driver_id and u2.banned=...原创 2021-04-03 21:08:24 · 131 阅读 · 0 评论 -
Leetcode 197. 上升的温度(datediff、暴力双表连接)
select w1.id from weather w1 join weather w2 on datediff(w1.recorddate,w2.recorddate)=1 and w1.temperature>w2.temperature原创 2021-04-03 20:59:43 · 126 阅读 · 0 评论 -
Leetcode 196. 删除重复的电子邮箱(暴力、双表连接或者删最值)
delete from person where id not in( select id from( select min(id) id from person group by email)t )原创 2021-04-03 20:58:39 · 161 阅读 · 0 评论 -
Leetcode 185. 部门工资前三高的所有员工(分组最值变种,用distinct+count)
select d.name "Department",e.name "Employee",Salary from Employee e join Department d on e.Departmentid=d.id where (select count(distinct(e1.salary)) from Employee e1 where e.salary<e1.salary and e.Departmentid=e1.Depart...原创 2021-04-03 20:56:54 · 67 阅读 · 0 评论 -
Leetcode 184. 部门工资最高的员工
select d.name "Department",e.name "Employee",salary "Salary" from employee e join Department d on e.departmentid=d.id where (e.departmentid,salary) in (select departmentid,max(salary) from employee group by departmentid)原创 2021-04-03 20:55:18 · 82 阅读 · 0 评论 -
Leetcode 185. 部门工资前三高的所有员工(查同组前几 经典嵌套子查询)
select d.name "Department",e.name "Employee",Salary from Employee e join Department d on e.Departmentid=d.id where (select count(distinct(e1.salary)) from Employee e1 where e.salary<e1.salary and e.Departmentid=e1.Depart...原创 2021-03-30 19:30:30 · 120 阅读 · 0 评论 -
Leetcode 184. 部门工资最高的员工
select d.name "Department",e.name "Employee",salary "Salary" from employee e join Department d on e.departmentid=d.id where (e.departmentid,salary) in (select departmentid,max(salary) from employee group by departmentid)原创 2021-03-30 19:02:02 · 74 阅读 · 0 评论 -
Leetcode 183. 从不订购的客户(子查询、not in)
# Write your MySQL query statement belowselect Name Customers from Customers where Id not in(select c.Id from Customers c,Orders o where c.Id = o.CustomerId)原创 2021-03-28 20:53:11 · 104 阅读 · 0 评论 -
Leetcode 180. 连续出现的数字(多表连接)
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原创 2021-03-28 20:47:22 · 142 阅读 · 0 评论 -
Leetcode 182. 查找重复的电子邮箱 (having )
# Write your MySQL query statement belowselect Email from Person group by Email having count(Email)>1原创 2021-03-28 20:05:05 · 124 阅读 · 0 评论 -
LeetCode 181. 超过经理收入的员工
# Write your MySQL query statement belowselect l1.Name Employee from Employee l1,Employee l2 where l1.Salary>l2.Salary and l1.ManagerId=l2.Id原创 2021-03-28 19:59:09 · 86 阅读 · 0 评论 -
LeetCode 178. 分数排名(dense_rank 连续排名、与rank区分)
# Write your MySQL query statement belowselect Score ,Id as `Rank` from Scores order by Score desc原创 2021-03-28 19:42:46 · 181 阅读 · 0 评论 -
Leetcode 177. 第N高的薪水
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INTBEGIN SET N:=N-1; RETURN ( # Write your MySQL query statement below. select ifnull( (select distinct Salary from Employee order by Salary desc ...原创 2021-03-28 19:34:26 · 121 阅读 · 0 评论 -
LeetCode 数据库解题目录(mysql版&含vip题)
花重金氪了一个力扣会员 就为了刷上锁的vip题 数据库其实挺重要的以下是解题目录:175. 组合两个表176. 第二高的薪水原创 2021-03-24 23:15:02 · 1343 阅读 · 24 评论 -
LeetCode 数据库 176. 第二高的薪水
原题链接# Write your MySQL query statement belowselect ifnull( (select distinct Salary from Employee order by Salary desc limit 1 offset 1) ,null) as SecondHighestSalary原创 2021-03-24 23:08:55 · 173 阅读 · 0 评论 -
LeetCode 数据库 175. 组合两个表
原题链接# Write your MySQL query statement below/* Write your T-SQL query statement below */select FirstName,LastName,City,State from Person left join Address on Person.PersonId=Address.PersonId原创 2021-03-24 23:04:55 · 201 阅读 · 0 评论