SQL实战分析
数据来源:https://pan.baidu.com/s/1zBobf63JTbhNKxZ9P-Ju1Q
正式开始(只用到一个表,以下用x代替)
1.先看一下所有数据
select * from x
2.看过数据之后正式开始查找,接下来查看地点在北京且职位是数据分析师的数据
(即北京的数据分析师)
select * from x
where city = '北京' and positionName = '数据分析师'
3.接上面查找地点在北京或者职位是数据分析师的数据
select * from x
where city = '北京' or positionName = '数据分析师'
4.查找北京或者上海的数据分析师的数据
select * from x
where city in('上海','北京') and positionName = '数据分析师'
以上的逻辑判断的都只用到了'=',按实际需求可以有 大于'>',小于'<',不等于'!='
5.再按上面举个例子,查找公司ID超过超过500的数据
select * from x
where companyId >= 500
6.positionLables这一列描述很杂,下面我们模糊查找一下包含数据分析的数据
select * from x
where positionLables like '%数据分析%'
7.以城市为组看一下,不同城市的职位数量情况
select city,count(distinct positionId) from x
group by city
8.在7的条件下再加入招聘学历限制
select city,workYear,count(distinct positionId) from x
group by city,workYear
9.有些数据拿到手,不一定能直接用,需要清洗,如本表的salary(格式为nk-nk)
我们需要得到平均工资,我们先把最低工资洗出来,此处用left函数截取,用location定位。
select salary,left(salary,locate('k',salary)-1) as bottomsalary from x
得到最低工资,再来看看最高工资
10.清洗最高工资
select salary,substr(salary,locate('-',salary)+1,(length(salary)-locate('-',salary))-1) as topsalary from x
11.结合起来再结合招聘学历要求可以得到以下结果
select city,workYear,avg((bottomsalary+topsalary)/2) as avgsalary
from (select city,salary,workYear,left(salary,locate('K',salary)-1) as bottomsalary,substr(salary,locate('-',salary)+1,(length(salary)-locate('-',salary))-1) as topsalary
from 11dataanalyst) as t1 where salary not like '%以上%'
group by city,workYear
order by city,avgsalary
以上便得到,各个城市,不同学历之间的平均工资。
12.查询招聘数量top3的公司数据
select companyId,companyShortName,count(distinct positionId)
from 11dataanalyst
group by companyId
order by count(distinct positionId) desc limit 3
13.查询出O2O、电子商务、互联网金融这三个行业,哪个行业的平均薪资最高.
select industryField,avg((bottomsalary+topsalary)/2)
from (select city,salary,industryField,left(salary,locate('K',salary)-1) as bottomsalary,substr(salary,locate('-',salary)+1,(length(salary)-locate('-',salary))-1) as topsalary
from 11dataanalyst) as t1 where salary not like '%以上%' and industryField in ('o2o','电子商务','互联网')
group by industryField
order by avg((bottomsalary+topsalary)/2) desc
网友评论