本文基于前段时间学习总结的 MySQL 相关的查询语法,在牛客网找了相应的 MySQL 题目进行练习,以便加强对于 MySQL 查询语法的理解和应用。
由于涉及到的数据库表较多,因此本文不再展示,只提供 MySQL 代码与示例输出。
以下内容是牛客题霸-SQL 篇的第 223 - 262 道题目的 MySQL 代码答案,本文跳过了其中一些更新、删除、修改等操作的题目。
SQL 223:查询没有分类的电影 id 及电影名称
sql
select f.film_id, f.title
from film f
left join film_category fc
on f.film_id = fc.film_id
where fc.category_id is null
SQL 224:查询属于 Action 分类的所有电影名称和对应的电影描述信息
sql
select title, description
from film f
join film_category fc
on f.film_id = fc.film_id
join category c
on fc.category_id = c.category_id
where c.name = 'Action'
SQL 226:将所有员工的 last_name 和 first_name 拼接起来作为 Name
sql
select concat(last_name,' ',first_name) as Name
from employees
SQL 227-228:创建一个 actor 表并批量插入数据
sql
create table if not exists actor(
actor_id smallint(5) not null primary key,
first_name varchar(45) not null,
last_name varchar(45) not null,
last_update timestamp not null
)
insert into actor(actor_id, first_name, last_name, last_update)
values
(1, 'PENELOPE', 'GUINESS', '2006-02-15 12:34:33'),
(2, 'NICK', 'WAHLBERG', '2006-02-15 12:34:33')
SQL 244:将所有员工的 last_name 和 first_name 通过 (') 连接起来
sql
select concat(last_name, "'", first_name)
from employees
SQL 246:查询所有员工的 first_name,并按照 first_name 最后两个字母升序排列
sql
select first_name
from employees
order by right(first_name, 2) 或者 order by substr(first_name, -2)
SQL 247:按照 dept_no 进行汇总,将属于同一个部门的 emp_no 按照逗号进行连接
sql
select dept_no,
group_concat(emp_no separator ',')
from dept_emp
group by dept_no
SQL 248:查询排除在职(to_date = '9999-01-01' )员工的最大、最小 salary 之后,其他的在职员工的平均工资 avg_salary
sql
# 方法一
select (sum(salary) - max(salary) - min(salary))/(count(salary) - 2) as avg_salary
from salaries
where to_date = '9999-01-01'
# 方法二
select avg(salary) as avg_salary
from salaries
where salary>(
select min(salary) from salaries
) and salary<(
select max(salary) from salaries
) and to_date='9999-01-01'
SQL 253:查询 emp_no,first_name,last_name,奖金类型 btype,对应的薪水 salary 以及奖金金额 bonus,bonus 结果保留一位小数,按 emp_no 升序排序
sql
select e.emp_no, first_name, last_name, btype, salary,
case
when btype = 1 then round(salary * 0.1, 1)
when btype = 2 then round(salary * 0.2, 1)
else round(salary * 0.3, 1)
end as bonus
from employees e
join emp_bonus eb
on e.emp_no = eb.emp_no
join salaries s
on e.emp_no = s.emp_no
where to_date = '9999-01-01'
order by e.emp_no
SQL 254:查询每个在职员工(to_date = '9999-01-01')的累计 salary
sql
select emp_no, salary,
sum(salary) over(order by emp_no) as running_total
from salaries
where to_date = '9999-01-01'
SQL 255:查询 employees 表中排名为奇数行的 first_name(输出结果不改变原表中 first_name 的顺序
sql
select e.first_name
from employees e
join
(
select first_name,
rank() over(order by first_name) as rn
from employees
) a
on a.first_name = e.first_name
where rn % 2 = 1
SQL 256:查询出现 3 次及 3 次以上的积分
sql
select number from grade
group by number
having count(*) >= 3
SQL 257:查询通过题目个数的排名,通过题目个数相同的,排名相同,此时按照 id 升序排列
sql
select *,
dense_rank() over(order by number desc) as t_rank
from passing_number
SQL 258:查询每个人的任务情况,没有任务的也要输出,并按照每个人的 id 升序排列
sql
select p.id, name, content
from person p
left join task t
on p.id = t.person_id
order by p.id
SQL 259:查询每一个日期里面,正常用户发送给正常用户邮件失败的概率是多少,结果保留到小数点后 3 位,并按照日期升序排列
sql
with a as(
select id from user
where is_blacklist = 0
) -- 正常用户的 id
select date,
round(avg(if(type = 'completed', 0, 1)), 3) as p
from email e
where e.send_id in(
select id from a
)
and e.receive_id in(
select id from a
)
group by date
order by date
SQL 260:查询每个用户最近一天登录的日子,并按照 user_id 升序排列
sql
# 方法一
select user_id, max(date) as date
from login
group by user_id
order by user_id
# 方法二:窗口函数
with a as(
select user_id, date,
rank() over(partition by user_id order by date desc) as rn
from login
)
select user_id, date
from a
where rn = 1
order by user_id
SQL 261:查询每个用户最近一天登录的日子,用户的名字,及用户用的设备的名字,并按照 user_name 升序排列
sql
# 方法一:rank() 的窗口函数
with a as(
select u.name as u_n, c.name as c_n, l.date as date,
row_number() over(partition by user_id order by l.date desc) as rn
from login l
join user u
on l.user_id = u.id
join client c
on l.client_id = c.id
)
select a.u_n, a.c_n, a.date
from a
where rn = 1
order by a.u_n
# 方法二:max(date) 的窗口函数
with a as(
select u.name as u_n, c.name as c_n, l.date as date,
max(l.date) over(partition by user_id) as max_date
from login l
join user u
on l.user_id = u.id
join client c
on l.client_id = c.id
)
select a.u_n, a.c_n, a.date
from a
where a.date = a.max_date
order by a.u_n
SQL 262:查询新登录用户次日成功的留存率,即第 1 天登陆之后,第 2 天再次登陆的概率,保留小数点后 3 位
sql
# 方法一
with a as(
select *,
min(date) over(partition by user_id) as first_day
from login
), -- 每个 user 首次登陆的日期
b as(
select count(distinct user_id)
from login
) -- 总的 user 数量
select round(count(distinct a.user_id)/(select * from b), 3) as p
from a
where datediff(date, first_day) = 1
# 方法二
select round(count(distinct a.id) / (select count(distinct user_id) from login ),3)
from
(select user_id id, date,
lead(date,1)over(partition by user_id order by date) date2
from login) a
where datediff(date2,date)=1;