LeetCode 高频 SQL 50 题(基础版)之 【聚合函数】部分

题目:620. 有趣的电影



题解:

sql 复制代码
select * from cinema
where description !='boring' and id%2=1
order by rating desc

题目:1251. 平均售价




题解:

sql 复制代码
select p.product_id product_id,round(ifnull(sum(p.price*u.units)/sum(u.units),0),2) average_price 
from Prices p left join UnitsSold u
on p.product_id=u.product_id
where (p.start_date<=u.purchase_date and u.purchase_date<=p.end_date) or u.units is null
group by p.product_id

题目:1075. 项目员工 I




题解:

sql 复制代码
select p.project_id project_id, round(avg(e.experience_years),2) average_years from Project p,Employee e
where p.employee_id=e.employee_id
group by p.project_id

题目:1633. 各赛事的用户注册率




题解:

sql 复制代码
select contest_id,round(count(user_id)*100/(select count(*) from Users),2) as percentage
from Register r 
group by contest_id
order by percentage desc,contest_id asc

题目:1211. 查询结果的质量和占比



题解:

sql 复制代码
select query_name,round(avg(rating/position),2) quality,
round(100*avg(rating<3),2) poor_query_percentage 
from Queries
group by query_name

题目:1193. 每月交易 I



题解:

sql 复制代码
select  left(trans_date,7) month,country,
count(*) trans_count,
sum(amount) approved_count,
sum(if(state='approved',1,0)) trans_total_amount,
sum(if(state='approved',amount,0)) approved_total_amount
from Transactions
group by month,country

题目:1174. 即时食物配送 II



题目:

sql 复制代码
select round(100*avg(order_date=customer_pref_delivery_date),2) immediate_percentage 
from Delivery
where (customer_id,order_date) in(
    select customer_id,min(order_date) 
    from Delivery
    group by customer_id
)

题目:550. 游戏玩法分析 IV



题解:

sql 复制代码
select round(avg(a2.event_date is not null),2) fraction from (
    select player_id,min(event_date) event_date from Activity
    group by player_id) a1 left join Activity a2
on a1.player_id=a2.player_id and datediff(a2.event_date,a1.event_date)=1
相关推荐
杨云龙UP7 分钟前
CentOS Linux 7 (Core)上部署Oracle 11g、19C RAC详细图文教程
数据库·oracle
tkevinjd14 分钟前
图论\dp 两题
leetcode·动态规划·图论
ezl1fe18 分钟前
RAG 每日一技(十八):手写SQL-RAG太累?LangChain的SQL智能体(Agent)前来救驾!
数据库·人工智能·后端
小咖张24 分钟前
spring声明式事务,finally 中return对事务回滚的影响
数据库·java 声明式事务
JSON_L26 分钟前
MySQL 加锁与解锁函数
数据库·mysql
白鲸开源2 小时前
收藏!史上最全 Apache SeaTunnel Source 连接器盘点 (2025版),一篇通晓数据集成生态
大数据·数据库·开源
MonKingWD2 小时前
MySQL事务篇-事务概念、并发事务问题、隔离级别
数据库·后端·mysql
Java水解2 小时前
深入理解 SQL 中的 COALESCE、NULLIF 和 IFNULL 函数
后端·sql
我科绝伦(Huanhuan Zhou)2 小时前
银河麒麟V10一键安装Oracle 11g脚本分享
数据库·oracle