SQL学习笔记-2019.11.23

1、统计一下Artist角色的雇员数量 ✓
2、按角色统计一下每个角色的雇员数量 ✓
3、算出Engineer角色的就职年份总计 ✓
4、按角色分组算出每个角色按有办公室和没办公室的统计人数(列出角色,数量,有无办公室,注意一个角色如果部分有办公室,部分没有需分开统计) ✓
SELECT role,building is null as bn,count(*)
FROM employees
–where role=“Engineer”
group by role,bn

5、按角色和就职年份统计人数,年份按0-3,3-6,6-9这种阶梯分组,最后按角色+阶梯分组排序
SELECT role,Years_employed/3 as year_3,count(name)
FROM employees
group by role,year_3
order by role,year_3

1、统计出每一个导演的电影数量(列出导演名字和数量)
SELECT director,count(title)
FROM movies
group by director
2、统计一下每个导演的销售总额(列出导演名字和销售总额)
SELECT director,sum(Domestic_sales+International_sales) as sales
FROM movies as m
left join Boxoffice as b
on b.movie_id=m.id
group by director
3、按导演分组计算销售总额,求出平均销售额冠军(统计结果过滤掉只有单部电影的导演,列出导演名,总销量,电影数量,平均销量)
SELECT director,sum(Domestic_sales+International_sales)as sales,count(director),sum(Domestic_sales+International_sales)/count(director) as avgsales
FROM movies as m
left join Boxoffice as b
on b.movie_id=m.id
group by director
having count(Director)>1
order by Avgsales desc
limit 1

4、找出每部电影和单部电影销售冠军之间的销售差,列出电影名,销售额差额
SELECT title,(select max(Domestic_sales+International_sales) from Boxoffice)-(Domestic_sales+International_sales) as sales
FROM movies as m
left join Boxoffice as b
on b.movie_id=m.id

你可能感兴趣的:(sql)