本文基于前段时间学习总结的 MySQL 相关的查询语法,在牛客网找了相应的 MySQL 题目进行练习,以便加强对于 MySQL 查询语法的理解和应用。
由于涉及到的数据库表较多,因此本文不再展示,只提供 MySQL 代码与示例输出。
以下内容是牛客题霸-SQL 篇的第 263 - 275 道题目的 MySQL 代码答案。
SQL 263:查询每个日期登录新用户个数,并按照日期升序排列(熟练掌握方法一的通用方法)
# 方法一
with a as(
select user_id,
min(date) as login_date
from login
group by user_id
), -- 每个用户的注册日期
log_date as(
select date
from login
group by date
) -- 所有用户登陆的日期
select ld.date as date,
ifnull(count(distinct a.user_id), 0) as new
from log_date ld
left join a
on ld.date = a.login_date
group by 1
# 方法二(了解)
select a.date as date,
sum(case when rn = 1 then 1 else 0 end) as new
from(
select user_id, date,
row_number() over(partition by user_id order by date) rn
from login
) a
group by 1
order by 1
SQL 264:查询每个日期新用户的次日留存率,结果保留小数点后面3位数,并按照日期升序排列(难点,需要掌握次日留存率计算方法)
with reg as
(
select user_id, min(date) as reg_date
from login
group by 1
), -- 每个用户的注册日期
login_dis as(
select
user_id, date as login_date
from login
group by 1, 2
), -- 每个用户的登陆日期
login_date as(
select date
from login
group by 1
)
# 查询所有信息
select *
# 查询要求信息
select a.date as date,
round(ifnull(count(distinct c.user_id) / count(distinct b.user_id), 0), 3) as p
from login_date a
left join reg b
on a.date = b.reg_date
left join login_dis c
on b.user_id = c.user_id
and b.reg_date = date_sub(c.login_date, interval 1 day)
group by 1
order by 1
查询所有信息中的第一个 left join 结果:
查询所有信息中的第二个 left join 结果:
查询要求信息的最终结果:
SQL 265:查询查询刷题信息,包括: 用户的名字,截止到某天,累计总共通过了多少题,并先按照日期升序排列,再按照姓名升序排列(有登录却没有刷题的那一天的数据不需要输出)
select name as u_n, pn.date,
sum(number) over(partition by u.id order by pn.date) as pn_num
from passing_number pn
join user u
on pn.user_id = u.id
order by 2, 1
SQL 266:查询各个岗位平均分数,并按照平均分数降序排列,结果保留小数点后 3 位
select job, round(avg(score), 3) as avg
from grade
group by job
order by avg desc
SQL 267:查询用户分数大于其所在工作平均分数的 id,job,score,并按照 id 的升序排列
with a as(
select *,
avg(score) over(partition by job) as avg_score
from grade
group by 1, 2
)
select id, job, score
from a
where score > avg_score
order by id
SQL 268:查询每个岗位分数排名前 2 名的用户,并先按照 language 的 name 升序排列,再按照积分降序排列,最后按照 grade 的 id 升序排列
with a as(
select g.id as id, language_id, score, name,
dense_rank() over(partition by name order by score desc) as rn
from grade g
join language l
on g.language_id = l.id
)
select a.id, name, score
from a
where a.rn <= 2
order by name, score desc, a.id
SQL 269:查询每个岗位分数升序排列之后的中位数位置的范围,并按照 job 升序排列
select job,
round((case when count(*) % 2 = 0 then count(*) / 2 else (count(*) + 1) / 2 end),0) as start,
round((case when count(*) % 2 = 0 then count(*) / 2 + 1 else (count(*) + 1) / 2 end),0) as end
from grade
group by job
order by job
SQL 270:查询每个岗位分数的中位数位置上的所有信息,并按照 id 升序排列
with a as(
select *,
dense_rank() over(partition by job order by score desc) as rn,
count(*) over(partition by job) as cnt
from grade
)
select id, job, score, rn from a
# 中位数公式
where rn > (cnt - 1) / 2 and rn < (cnt + 3) / 2
order by id
SQL 271:查询在 2025-10-15 以后状态为购买成功的 C++ 课程或者 Java 课程或者 Python 课程的订单,并按照 order_info 的 id 升序排列
select *
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in('C++', 'Java', 'Python')
order by id
SQL 272:查询在 2025-10-15 以后,同一个用户下单 2 个及 2 个以上状态为购买成功的 C++ 课程或 Java 课程或 Python 课程的 user_id ,并按照 user_id 升序排列
select user_id
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in('C++', 'Java', 'Python')
group by 1
having count(*) >= 2
order by 1
SQL 273:查询在 2025-10-15 以后,同一个用户下单 2 个及 2 个以上状态为购买成功的 C++ 课程或 Java 课程或 Python 课程的订单信息 ,并按照 id 升序排列
with a as(
select *, count(*) over(partition by user_id) cnt
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in ('C++', 'Java', 'Python')
)
select a.id, a.user_id, a.product_name, a.status, a.client_id, a.date
from a
where a.cnt >= 2
order by 1
SQL 274:查询在 2025-10-15 以后,如果有一个用户下单 2 个及 2 个以上状态为购买成功的 C++ 课程或 Java 课程或 Python 课程,那么输出这个用户的user_id,以及满足前面条件的第一次购买成功的 C++ 课程或 Java 课程或 Python 课程的日期 first_buy_date,以及所有日期里购买成功的 C++ 课程或 Java 课程或 Python 课程的次数 cnt,并且输出结果按照 user_id 升序排序(难点,需要熟练掌握方法二和方法三的通用方法)
# 方法一
select user_id,
min(date) as first_buy_date,
count(*) as cnt
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in('C++', 'Java', 'Python')
group by 1
having cnt >= 2
order by 1
# 方法二
with a as(
select user_id,
date,
row_number() over(partition by user_id order by date) as rn,
count(*) over(partition by user_id) as cnt
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in('C++', 'Java', 'Python')
)
select user_id, date as first_buy_date, cnt
from a
where rn = 1 and cnt >= 2
order by 1
# 方法三
with a as(
select user_id,
date,
first_value(date) over(partition by user_id order by date) as first_buy_date,
count(*) over(partition by user_id) as cnt
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in('C++', 'Java', 'Python')
)
select user_id, first_buy_date, cnt
from a
group by 1, 2
having cnt >= 2
order by 1
SQL 275:查询在 2025-10-15 以后,如果有一个用户下单 2 个及 2 个以上状态为购买成功的 C++ 课程或 Java 课程或 Python 课程,那么输出这个用户的user_id,以及满足前面条件的第一次购买成功的 C++ 课程或 Java 课程或 Python 课程的日期 first_buy_date,以及满足前面条件的第二次购买成功的 C++ 课程或 Java 课程或 Python 课程的日期 second_buy_date,以及购买成功的 C++ 课程或 Java 课程或 Python 课程的次数 cnt,并且输出结果按照 user_id 升序排序
with a as(
select user_id,
first_value(date) over(partition by user_id order by date) as first_buy_date,
lead(date, 1) over(partition by user_id order by date) as second_buy_date,
row_number() over(partition by user_id order by date) as rn,
count(*) over(partition by user_id) as cnt
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in('C++', 'Java', 'Python')
)
select user_id, first_buy_date,
second_buy_date, cnt
from a
where rn = 1 and cnt >= 2
order by 1