欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > SQLLL

SQLLL

2025/9/26 17:40:38 来源:https://blog.csdn.net/zhuiQiuMX/article/details/148605299  浏览:    关键词:SQLLL

595-big-countries

https://leetcode.com/problems/big-countries/description/

面积最大的国家

--
select name, population, area
from World
where area >= 3000000 or population >= 25000000

596-classes-with-at-least-5-students

https://leetcode.com/problems/classes-with-at-least-5-students/description/

超过 5 名学生的课

select class
from Courses
group by class
having count(student) >= 5

having和where关键区别:

  • WHERE:在聚合之前过滤行。
  • HAVING:在聚合之后过滤分组

总结:WHERE 用于过滤行数据,而 HAVING 用于过滤分组后的数据。

601-human-traffic-of-stadium

https://leetcode.com/problems/human-traffic-of-stadium/description/

体育馆的人流量

id连续三行或更多

select distinct a.*
from stadium as a,stadium as b,stadium as c
where a.people >= 100 and b.people >= 100 and c.people >= 100
and ((a.id - b.id = 1 AND b.id - c.id = 1)OR (c.id - b.id = 1 AND b.id - a.id = 1)OR (b.id - a.id = 1 AND a.id - c.id = 1))
order by a.visit_date;
-- 用ID减去排名
with stadium_with_rnk as (select id, visit_date, people, rnk, (id - rnk) as islandfrom (select id, visit_date, people, rank() over (order by id) as rnkfrom stadiumwhere people >= 100) as t0
)
select id, visit_date, people
from stadium_with_rnk
where island in (select islandfrom stadium_with_rnkgroup by islandhaving count(*) >= 3)
order by visit_date

602-friend-requests-ii-who-has-the-most-friends

https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/description/

好友申请 II :谁有最多的好友

拥有最多的好友的人和他拥有的好友数目

with all_ids as (select requester_id as id from RequestAcceptedunion allselect accepter_id from RequestAccepted
)
select id, count(id) as num
from all_ids
group by id
order by count(id) desc limit 1;-- use rank
with all_ids as (select requester_id as id from RequestAcceptedunion allselect accepter_id from RequestAccepted
)
select id, num
from (select id, count(id) as num,rank() over (order by count(id) desc ) as rnkfrom all_idsgroup by id
)t0
where rnk = 1

607-sales-person

https://leetcode.com/problems/sales-person/description/

销售员

select name
from SalesPerson
where sales_id not in (select Orders.sales_idfrom Ordersjoin Company on Orders.com_id = Company.com_id and Company.name = 'RED')

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词