小匿 | 作者
知乎專欄 | 來源
https://zhuanlan.zhihu.com/p/30212389
SQL,數據分析崗的必備技能,你可以不懂Python,R,不懂可視化,不懂機器學習。但SQL,你必須懂。要不然領導讓你跑個數據來匯.,哦不,你不懂SQL都無法入職數據分析崗,更別說領導了。SQL難嗎?說實話,要寫好,很難很難。但要通過SQL筆試這關,並不難。相信大夥都使用過Excel,用SQL實現excel 常用操作去學,感覺會比較具體。數據是網上找到的銷售數據,命名為sale,長這樣:create table sale_guang SELECT * from sale where city="廣州";
需求:根據訂單明細號關聯兩表,並且sale_guang只有訂單明細號與利潤兩列(SELECT ordernum,profit from sale_guang) b on a.`ordernum`=b.`ordernum`;
(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;
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`);
本文為轉載分享&推薦閱讀,若侵權請聯繫後臺刪除