SQL輕松玩轉(zhuǎn)Excel的10大功能
來(lái)源:知乎-小匿

vlookup是excel幾乎最常用的公式,一般用于兩個(gè)表的關(guān)聯(lián)查詢等。所以我先創(chuàng)建一個(gè)新表:復(fù)制sale表并篩選出地區(qū)僅為廣州的,命名為sale_guang。
create table sale_guang
SELECT * from sale where city="廣州";需求:根據(jù)訂單明細(xì)號(hào)關(guān)聯(lián)兩表,并且sale_guang只有訂單明細(xì)號(hào)與利潤(rùn)兩列
SELECT * from sale a
inner JOIN
(SELECT ordernum,profit from sale_guang) b
on a.ordernum=b.ordernum需求:對(duì)比sale的訂單明細(xì)號(hào)與sale_guang訂單明細(xì)號(hào)的差異;
SELECT * from sale a
WHERE a.ordernum not in
(SELECT b.ordernum from sale_guang b);需求:去除業(yè)務(wù)員編碼的重復(fù)值
SELECT * FROM sale
where salesnum not in
(SELECT salesnum from sale
GROUP BY salesman
HAVING COUNT(salesnum)>1)需求:用0填充缺失值或則刪除有地區(qū)名稱缺失值的行。
--用0填充:
update sale set city = 0 where city = NULL
--刪除有缺失值的行:
delete from sale where city = NULL;需求:想知道業(yè)務(wù)員張愛(ài),在北京區(qū)域賣的商品訂單金額大于等于6000的信息。
SELECT * from sale
where salesman = "張愛(ài)"
and city = "北京"
and orderaccount >=6000;需求:篩選存貨名稱含有"三星"或則含有"索尼"的信息。
SELECT * from sale
where inventoryname like "%三星%"
or 存貨名稱 like "%索尼%";需求:北京區(qū)域各業(yè)務(wù)員的利潤(rùn)總額。
SELECT city,sum(`profit`)
from sale
WHERE city = "北京"
GROUP BY `city`;需求:存貨名稱含“三星字眼”并且稅費(fèi)高于1000的訂單有幾個(gè)?這些訂單的利潤(rùn)總和和平均利潤(rùn)是多少?
--有多少個(gè)?
SELECT COUNT(*) from sale
where inventoryname like "%三星%"
and `tax` > 1000 ;
--這些訂單的利潤(rùn)總和和平均利潤(rùn)是多少?
SELECT `ordernum`,SUM(profit),AVG(`profit`)
from sale
where inventoryname like "%三星%"
and `tax` > 1000
GROUP BY `ordernum`;需求:刪除存貨名稱兩邊的空格。
SELECT trim(inventoryname) from sale;需求:計(jì)算每個(gè)訂單號(hào)的成本并從高到低排序(成本 = 不含稅金額 - 利潤(rùn))
SELECT city,ordernum,
(Nontaxamount - profit) as cost
from sale
order by cost DESC;總結(jié):結(jié)構(gòu)化查詢語(yǔ)言(Structured Query Language)簡(jiǎn)稱SQL,果然和它名字一樣,查詢起來(lái)得心應(yīng)手,但做想做數(shù)據(jù)處理方面,能明細(xì)感受到比Python和excel吃力(也可能是我還沒(méi)學(xué)好orz)。
貼一些在面試時(shí)遇到過(guò)的SQL筆試題吧:
某數(shù)據(jù)服務(wù)公司:

Student:

Score
(1)查詢Student表中的所有記錄的Sname、Ssex和Class列。
select sname,ssex,class from student;(2)查詢Score表中成績(jī)?cè)?0到80之間的所有記錄。
select * from score between 60 and 80;(3)查詢95033班和95031班的平均分。
select class,avg(degree) from Score a
join student b
on a.sno = b.sno
GROUP BY CLASS;總之是比較簡(jiǎn)單的SQL筆試題了,當(dāng)時(shí)很快就寫(xiě)完了。實(shí)際上這不是原題,不過(guò)我有印象就是考察這幾個(gè)知識(shí)點(diǎn),并且蠻簡(jiǎn)單的。
某手游公司的SQL筆試題(原題)

(1)建立表Student的語(yǔ)句寫(xiě)下來(lái),表Student是由學(xué)號(hào)Sno,姓名Sname,性別Ssex,年齡Sage,所在系Sdept五個(gè)屬性組成,其中學(xué)號(hào)屬性不能為空,并且其值是唯一的。
create table Student_new
(sno varchar(20) PRIMARY KEY,
sname varchar(10),ssex char(2),
sage int,sdept varchar(25));(2)在student 表中查詢Sdept是“計(jì)算機(jī)”的學(xué)生所有信息并按SNO列排序。
select * from student
where sdept = "計(jì)算機(jī)"
order by sno ;(3)在以上三個(gè)表中查詢Ccredit為5并且Grade大于60的學(xué)生的學(xué)號(hào)、姓名和性別。
select a.sno,a.sname,a.ssex from student a
join (Course b ,SC c)
on a.sno=c.sno and b.cno =c.cno
where Ccredit = 5 and Grade > 60;某互聯(lián)網(wǎng)金融公司SQL筆試題(原題)

(1)表A和表B的交集:
SELECT a.cus_id from `表a` as a
INNER JOIN `表b` as b
on a.cus_id=b.cus_id;SELECT * from `表a`
UNION
SELECT * from `表b`;(3)表A和表B的對(duì)稱差:
SELECT * from `表a`
where cus_id not in (SELECT * from `表b`)
UNION
SELECT * from `表b`
where cus_id not in (SELECT * from `表a`);(4)表A中存在但表B中不存在:
SELECT * from `表a`
WHERE cus_id not in (SELECT cus_id from `表b`);
加入知識(shí)星球【我們談?wù)摂?shù)據(jù)科學(xué)】
SQL數(shù)據(jù)分析打卡課程進(jìn)行中,限時(shí)優(yōu)惠
500+小伙伴一起學(xué)習(xí)!
· 推薦閱讀 ·
在windows和linux上高效快捷地發(fā)布Dash應(yīng)用
超簡(jiǎn)單,讓別人也能訪問(wèn)到你的Dash應(yīng)用
