小匿 | 作者
知乎专栏 | 来源
https://zhuanlan.zhihu.com/p/30212389
SQL,数据分析岗的必备技能,你可以不懂Python,R,不懂可视化,不懂机器学习。但SQL,你必须懂。要不然领导让你跑个数据来汇......,哦不,你不懂SQL都无法入职数据分析岗,更别说领导了。SQL难吗?说实话,要写好,很难很难。但要通过SQL笔试这关,并不难。相信大伙都使用过Excel,用SQL实现excel 常用操作去学,感觉会比较具体。数据是网上找到的销售数据,命名为sale,长这样:
vlookup是excel几乎最常用的公式,一般用于两个表的关联查询等。所以我先创建一个新表:复制sale表并筛选出地区仅为广州的,命名为sale_guang。create table sale_guang SELECT * from sale where city="广州";
需求:根据订单明细号关联两表,并且sale_guang只有订单明细号与利润两列
(SELECT ordernum,profit from sale_guang) b on a.`ordernum`=b.`ordernum`;
需求:对比sale的订单明细号与sale_guang订单明细号的差异
(SELECT b.ordernum from sale_guang b);
(SELECT salesnum from sale GROUP BY salesman HAVING COUNT(salesnum)>1)
update sale set city = 0 where city = NULL delete from sale where city = NULL;
需求:想知道业务员张爱,在北京区域卖的商品订单金额大于等于6000的信息
需求:筛选存货名称含有"三星"或则含有"索尼"的信息
SELECT * from sale where inventoryname like "%三星%" or 存货名称 like "%索尼%";
SELECT city,sum(`profit`) from sale
需求:存货名称含“三星字眼”并且税费高于1000的订单有几个?这些订单的利润总和和平均利润是多少?
#有多少个?
SELECT COUNT(*) from sale
where inventoryname like "%三星%"
and `tax` > 1000 ;
#这些订单的利润总和和平均利润是多少?
SELECT `ordernum`,SUM(profit),AVG(`profit`) from sale
where inventoryname like "%三星%"
and `tax` > 1000
GROUP BY `ordernum`;
SELECT trim(inventoryname) from sale;
需求:计算每个订单号的成本并从高到低排序(成本 = 不含税金额 - 利润)
SELECT city,ordernum,(Nontaxamount - profit) as cost from sale
总结:结构化查询语言(Structured Query Language)简称SQL,果然和它名字一样,查询起来得心应手,但做想做数据处理方面,能明细感受到比Python和excel吃力(也可能是我还没学好orz)。 SQL笔试题
(1)查询Student表中的所有记录的Sname、Ssex和Class列select sname,ssex,class from student;
(2)查询Score表中成绩在60到80之间的所有记录select * from score between 60 and 80;
select class,avg(degree) from Score a
总之是比较简单的SQL笔试题了,当时很快就写完了。实际上这不是原题,不过我有印象就是考察这几个知识点,并且蛮简单的。(1)建立表Student的语句写下来,表Student是由学好Sno,姓名Sname,性别Ssex,年龄Sage,所在系Sdept五个属性组成,其中学号属性不能为空,并且其值是唯一的。(sno varchar(20) PRIMARY KEY, sname varchar(10),ssex char(2), sage int,sdept varchar(25));
(2)在student 表中查询Sdept是“计算机”的学生所有信息并按SNO列排序。select * from student where sdept = "计算机" order by sno ;
(3)在以上三个表中查询Ccredit为5并且Grade大于60的学生的学号、姓名和性别。select a.sno,a.sname,a.ssex from student a on a.sno=c.sno and b.cno =c.cno where Ccredit = 5 and Grade > 60;
SELECT a.cus_id from `表a` as a
where cus_id not in (SELECT * from `表b`) where cus_id not in (SELECT * from `表a`);
WHERE cus_id not in (SELECT cus_id from `表b`);
送大礼包(包含PPT模板、图表可视化模板、sql面试题、数据分析案例等)