<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SQL輕松玩轉(zhuǎn)Excel的10大功能

          共 4344字,需瀏覽 9分鐘

           ·

          2021-06-15 20:55

          來(lái)源:知乎-小匿
          最近是招聘季,數(shù)分筆試又經(jīng)常會(huì)考到SQL,今天特意分享一篇SQL干貨,幫助大家查漏補(bǔ)缺。


          SQL,數(shù)據(jù)分析崗的必備技能,你可以不懂Python,R,不懂可視化,不懂機(jī)器學(xué)習(xí)。但SQL,你必須懂。要不然領(lǐng)導(dǎo)讓你跑個(gè)數(shù)據(jù)來(lái)匯......,哦不,你不懂SQL都無(wú)法入職數(shù)據(jù)分析崗,更別說(shuō)領(lǐng)導(dǎo)了。

          SQL難嗎?說(shuō)實(shí)話,要寫(xiě)好,很難很難。但要通過(guò)SQL筆試這關(guān),并不難。相信大伙都使用過(guò)Excel,用SQL實(shí)現(xiàn)excel 常用操作去學(xué),感覺(jué)會(huì)比較具體。我自身也剛?cè)霐?shù)據(jù)崗不久,本文也是為自己鞏固一下SQL。

          數(shù)據(jù)是網(wǎng)上找到的銷售數(shù)據(jù),命名為sale,長(zhǎng)這樣:



          01. 關(guān)聯(lián)公式:Vlookup


          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



          02. 對(duì)比兩列差異


          需求:對(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);



          03. 去除重復(fù)值


          需求:去除業(yè)務(wù)員編碼的重復(fù)值


          SELECT * FROM sale
          where salesnum not in 
          (SELECT salesnum from sale
          GROUP BY salesman
          HAVING COUNT(salesnum)>1)



          04. 缺失值處理


          需求:用0填充缺失值或則刪除有地區(qū)名稱缺失值的行。


          --用0填充:
          update sale set city = 0 where city = NULL
          --刪除有缺失值的行:
          delete from sale where city = NULL;



          05. 多條件篩選


          需求:想知道業(yè)務(wù)員張愛(ài),在北京區(qū)域賣的商品訂單金額大于等于6000的信息。


          SELECT * from sale
          where salesman = "張愛(ài)" 
          and city = "北京"
          and orderaccount >=6000;



          06. 模糊篩選數(shù)據(jù)


          需求:篩選存貨名稱含有"三星"或則含有"索尼"的信息。


          SELECT * from sale
          where inventoryname like "%三星%" 
          or 存貨名稱 like "%索尼%";



          07. 分類匯總


          需求:北京區(qū)域各業(yè)務(wù)員的利潤(rùn)總額。


          SELECT city,sum(`profit`)
          from sale
          WHERE city = "北京"
          GROUP BY `city`;



          08. 條件計(jì)算


          需求:存貨名稱含“三星字眼”并且稅費(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`;



          09. 刪除數(shù)據(jù)間的空格


          需求:刪除存貨名稱兩邊的空格。


          SELECT trim(inventoryname) from sale;



          10. 合并與排序列


          需求:計(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;


          (2)表A和表B的并集:

          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í)!




          · 推薦閱讀 ·

          聊聊Pandas的前世今生

          在windows和linux上高效快捷地發(fā)布Dash應(yīng)用

          超簡(jiǎn)單,讓別人也能訪問(wèn)到你的Dash應(yīng)用


          瀏覽 35
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费无码人成视在线观看不卡 | 懂色av懂色av粉嫩av无码 | 国产91乱码一区二区三区 | 国产无遮挡又黄又爽免费软件 | 黄色以及电影毛片 |