牛客网刷sql下半部分

[编程题]汇总各个部门当前员工的title类型的分配数目

https://www.nowcoder.com/practice/4bcb6a7d3e39423291d2f7bdbbff87f8?tpId=82&tqId=29778&tPage=2&rp=&ru=%2Fta%2Fsql&qru=%2Fta%2Fsql%2Fquestion-ranking

select s.dept_no,s.dept_name,t.title,count(*) as counts
from (departments d inner join dept_emp de on
d.dept_no = de.dept_no) s inner join
titles t on s.emp_no = t.emp_no AND s.to_date = '9999-01-01' AND t.to_date = '9999-01-01'
group by s.dept_no,t.title

这题我用group by用错了,当前员工所有的title以及该类型title对应的数目count,比如说员工1,title = x,应当以这作为一个分组,统计所有一样的分组的个数;

[编程题]给出每个员工每年薪水涨幅超过5000的员工编号emp_no

https://www.nowcoder.com/practice/eb9b13e5257744db8265aa73de04fd44?tpId=82&tqId=29779&tPage=2&rp=&ru=%2Fta%2Fsql&qru=%2Fta%2Fsql%2Fquestion-ranking

select s2.emp_no,s2.from_date,(s2.salary-s1.salary) as salary_growth
from salaries as s1 inner join salaries as s2
on s1.emp_no = s2.emp_no
and salary_growth > 5000
AND (strftime("%Y",s2.to_date) - strftime("%Y",s1.to_date) = 1 
     OR strftime("%Y",s2.from_date) - strftime("%Y",s1.from_date) = 1 )
order by salary_growth desc;

链接:https://www.nowcoder.com/questionTerminal/eb9b13e5257744db8265aa73de04fd44
来源:牛客网

假设s1是涨薪水前的表,s2是涨薪水后的表,因为每个员工涨薪水的时间不全固定,有可能一年涨两次,有可能两年涨一次,所以每年薪水的涨幅,应该理解为两条薪水记录的from_date相同或to_date相同。

#################################

难得一笔,lz都没有看懂

[编程题]给出每个员工每年薪水涨幅超过5000的员工编号emp_no

https://www.nowcoder.com/practice/3a303a39cc40489b99a7e1867e6507c5?tpId=82&tqId=29780&tPage=2&rp=&ru=%2Fta%2Fsql&qru=%2Fta%2Fsql%2Fquestion-ranking

select c.name,count(fc.film_id) from (select category_id,count(film_id) as category_num
                                     from film_category group by category_id having count(film_id)>=5)
as cc,film as f,film_category as fc,category as c
where f.description like '%robot%'
and f.film_id = fc.film_id
and c.category_id = fc.category_id
and c.category_id = cc.category_id;

[编程题]使用join查询方式找出没有分类的电影id以及名称

https://www.nowcoder.com/practice/a158fa6e79274ac497832697b4b83658?tpId=82&tqId=29781&tPage=2&rp=&ru=%2Fta%2Fsql&qru=%2Fta%2Fsql%2Fquestion-ranking

select f.film_id,f.title as film_name
from film f left join film_category fc
on f.film_id = fc.film_id
where fc.category_id is null;

猜你喜欢

转载自blog.csdn.net/qq_17641711/article/details/83028890