select a.emp_no,end_salary-begin_salary as growth
from(select emp_no,salary as end_salary
from salaries
where to_date='9999-01-01')a leftjoin(select employees.emp_no,salary as begin_salary
from employees
leftjoin salaries using(emp_no)where hire_date=from_date
)b using(emp_no)orderby growth asc
SQL22统计各个部门的工资记录数
select d.dept_no,dept_name,count(salary)from departments d
leftjoin dept_emp using(dept_no)leftjoin salaries using(emp_no)groupby d.dept_no,dept_name
select
dept_no
,emp_no
,salary
from employees
leftjoin dept_emp using(emp_no)leftjoin salaries using(emp_no)where emp_no notin(select emp_no from dept_manager)
SQL25获取员工其当前的薪水比其manager当前薪水还高的相关信息
select
a.emp_no
,b.emp_no as manager_no
,a.salary as emp_salary
,b.salary as manager_salary
from(select dept_no,emp_no,salary
from dept_emp
leftjoin salaries using(emp_no)) a leftjoin(select dept_no,emp_no,salary
from dept_manager
leftjoin salaries using(emp_no)) b using(dept_no)where a.emp_no!=b.emp_no and a.salary>b.salary
selectavg(salary)as avg_salary
from(select
salary
,dense_rank()over(orderby salary) rk1
,dense_rank()over(orderby salary desc) rk2
from salaries
where to_date='9999-01-01') t
where t.rk1 !=1and t.rk2 !=1
SQL55分页查询employees表,每5行一页,返回第2页的数据
select*from employees
limit5,5
SQL57使用含有关键字exists查找未分配具体部门的员工的所有信息。
select*from employees
wherenotexists(select emp_no
from dept_emp
where employees.emp_no=dept_emp.emp_no)
SQL59获取有奖金的员工相关信息。
select
emp_no
,first_name
,last_name
,btype
,salary
,casewhen btype=1then0.1*salary
when btype=2then0.2*salary
else0.3*salary
endas bonus
from employees
join emp_bonus using(emp_no)join salaries using(emp_no)where to_date='9999-01-01'
SQL60统计salary的累计和running_total
select
emp_no
,salary
,sum(salary)over(orderby emp_no asc)as running_total
from salaries
where to_date ='9999-01-01'
SQL61给出employees表中排名为奇数行的first_name
select employees.first_name
from employees
leftjoin(select
first_name
,row_number()over(orderby first_name) rn
from employees
) t1 using(first_name)where t1.rn%2=1
SQL62出现三次以上相同积分的情况
select number
from grade
groupby number
havingcount(number)>=3
SQL63刷题通过的题目排名
select
id
,number
,dense_rank()over(orderby number desc)as t_rank
from passing_number
SQL64找到每个人的任务
select person.id,name,content
from person
leftjoin task on person.id=task.person_id
SQL65异常的邮件概率
selectdate,round(avg(casewhentype='completed'then0else1end),3)as p
from email
where send_id notin(select id fromuserwhere is_blacklist !=0)and receive_id notin(select id fromuserwhere is_blacklist !=0)groupbydate
SQL66牛客每个人最近的登录日期(一)
select user_id,max(date)as id
from login
groupby user_id
orderby user_id
SQL67牛客每个人最近的登录日期(二)
select
u_n
,c_n
,datefrom(select
u.name as u_n
,c.name as c_n
,date,row_number()over(partitionby u.name orderbydatedesc) rn
from`user`u
join login l on u.id=l.user_id
join client c on c.id=l.client_id
) t
where rn=1
SQL68牛客每个人最近的登录日期(三)
selectround(avg(casewhen l.id isnullthen0else1end),3)from(select
user_id
,min(date)as login_date
,date_add(min(date),interval1day)as login_2
from login
groupby user_id
) t leftjoin login l on l.user_id=t.user_id and l.date=t.login_2
SQL69牛客每个人最近的登录日期(四)
selectdistinct login.date,casewhen t2.num isnullthen0else t2.num endfrom login
leftjoin(select login_date,count(login_date)as num
from(select user_id,min(date)as login_date
from login
groupby user_id) t
groupby login_date
) t2 on login.date=t2.login_date
select
u_n
,date,sum(number)over(partitionby u_n orderbydateasc)as ps_num
from(select
u.name as u_n
,l.dateasdate,number
from`user` u
join login l on u.id=l.user_id
join passing_number p on p.user_id=u.id
where l.date=p.date) t
orderbydate,u_n
SQL72考试分数(一)
select job,round(avg(score),3)as avg
from grade
groupby job
orderby avg desc;
SQL73考试分数(二)
select id,grade.job,grade.score
from grade
leftjoin(select job,round(avg(score),3)as avg
from grade
groupby job
) t using(job)where grade.score>avg
SQL74考试分数(三)
select
id
,name
,score
from(select
g.id as id
,l.name as name
,score
,dense_rank()over(partitionby language_id orderby score desc)as rn
from grade g
leftjoin`language` l on g.language_id=l.id
) t
where rn=1or rn=2orderby name,score desc
select id,a.job,a.score,rk as t_rank
from(select
id
,job
,score
,row_number()over(partitionby job orderby score desc) rk
from grade
) a leftjoin(select
job
,casewhencount(score)%2=1then ceil(count(score)/2)else floor(count(score)/2)endas mid_num
from grade
groupby job
unionselect
job
,casewhencount(score)%2=1then ceil(count(score)/2)else floor(count(score)/2)+1endas mid_num
from grade
groupby job
) b on a.job=b.job
where a.rk=b.mid_num
orderby id
select user_id
from order_info
wheredate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')groupby user_id
havingcount(status)>=2orderby user_id
SQL79牛客的课程订单分析(三)
select*from order_info
where user_id in(select user_id
from order_info
wheredate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')groupby user_id
havingcount(status)>=2)anddate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')
SQL80牛客的课程订单分析(四)
select
user_id
,min(date),count(status)from order_info
where user_id in(select user_id
from order_info
wheredate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')groupby user_id
havingcount(status)>=2)anddate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')groupby user_id
orderby user_id
SQL81牛客的课程订单分析(五)
select user_id,date,lk,cnt
from(select
user_id
,date,row_number()over(partitionby user_id orderbydateasc)as rk
,lead(date,1)over(partitionby user_id orderbydateasc)as lk
,count(status)over(partitionby user_id)as cnt
from order_info
where user_id in(select user_id
from order_info
wheredate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')groupby user_id
havingcount(status)>=2)anddate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')) t
where rk=1orderby user_id
SQL82牛客的课程订单分析(六)
select o.id,is_group_buy,c.name
from order_info o
leftjoin client c on o.client_id=c.id
where user_id in(select user_id
from order_info
wheredate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')groupby user_id
havingcount(status)>=2)anddate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')
SQL83牛客的课程订单分析(七)
select ifnull(c.name,'GroupBuy')as source,count(*)from order_info o
leftjoin client c on o.client_id=c.id
where user_id in(select user_id
from order_info
wheredate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')groupby user_id
havingcount(status)>=2)anddate>'2025-10-15'andstatus='completed'and
product_name in('C++','Python','Java')groupby c.name
orderby source
SQL84实习广场投递简历分析(一)
select job,sum(num)as cnt
from resume_info
whereyear(date)='2025'groupby job
orderby cnt desc
select a.job as job,a.mon as first_year_mon,a.cnt,b.mon,b.cnt
from(select job,date_format(date,'%Y-%m')as mon,sum(num)as cnt
from resume_info
whereyear(date)='2025'groupby job,mon
orderby mon desc,cnt desc) a join(select job,date_format(date,'%Y-%m')as mon,sum(num)as cnt
from resume_info
whereyear(date)='2026'groupby job,mon
orderby mon desc,cnt desc) b on a.job=b.job
whereright(a.mon,2)=right(b.mon,2)orderby first_year_mon desc,job desc
SQL87最差是第几名(一)
select
grade
,sum(number)over(orderby grade) number
from class_grade
SQL88最差是第几名(二)
select grade
from(select
grade
,(selectsum(number)from class_grade)as total
,sum(number)over(orderby grade)as a
,sum(number)over(orderby grade desc)as b
from class_grade
) t
where a>=total/2and b>=total/2orderby grade
SQL89获得积分最多的人(一)
select name,sum(grade_num)as grade_num
from`user` u
join grade_info i on u.id=i.user_id
groupby name
orderby grade_num desclimit0,1
SQL90获得积分最多的人(二)
select user_id,name,grade_num
from(select user_id,name,sum(grade_num)as grade_num
from`user` u
join grade_info i on u.id=i.user_id
groupby user_id,name
) t
where grade_num =(selectmax(a1)from(select user_id,sum(grade_num)as a1
from grade_info
groupby user_id
) a
)
SQL91获得积分最多的人(三)
withtempas(select
user_id
,name
,sum(casewhentype='add'then grade_num
else0end)-sum(casewhentype='reduce'then grade_num
else0end)as grade_num
from`user` u
join grade_info i on u.id=i.user_id
groupby user_id,name
)select
user_id
,name
,grade_num
fromtempwhere grade_num =(selectmax(grade_num)fromtemp)
SQL92商品交易(网易校招笔试真题)
select goods_id,name,weight,total
from goods g
rightjoin(select goods_id,sum(count)as total
from trans
groupby goods_id
) t on g.id=t.goods_id
groupby goods_id,name
having total>20and weight<50orderby goods_id
SQL93网易云音乐推荐(网易校招笔试真题)
select music.music_name
from music
join(selectdistinct music_name
from follow f
leftjoin music_likes m on f.follower_id=m.user_id
leftjoin music s on m.music_id=s.id
where f.user_id =1and music_name notin(select music_name
from music_likes m
leftjoin music s on m.music_id=s.id
where user_id =1)) t on t.music_name=music.music_name
SQL94今天的刷题量(一)
select name,count(1)as cnt
from submission s
leftjoin subject u on s.subject_id=u.id
where create_time=curdate()groupby name,subject_id
orderby cnt desc,subject_id