SQL第三章 (sql高級查詢)

1、子  查  詢
特點:
①使用靈活,可以成爲SQL語句的多個部分
②降低SQL的語句的複雜度,提高SQL語句的可讀性

/*----------------------------------------子  查  詢-----------------------------------------------------*/
    --查詢李四同學成績大於80分的記錄
        --聯合查詢
        select stuname,subject,score from StuMarks,StuInfo
        where StuInfo.stuid = StuMarks.stuid and score>80 and stuname = '李四'

        --子查詢1:stuinfo作爲子查詢
        select stuname,subject,score from StuMarks s1,
        (select * from stuinfo where stuname = '李四') s2
        where s1.stuid = s2.stuid and s1.score > 80

        --子查詢2:StuMarks作爲子查詢
        select stuname,subject,score from stuinfo s1,
        (select * from stumarks where score > 80) s2
        where s1.stuid = s2.stuid and s1.stuname= '李四'

        --子查詢3:stuinfo、StuMarks都作爲子查詢
        select stuname,subject,score from
        (select * from stuinfo where stuname = '李四') s1,
        (select * from stumarks where score > 80) s2
        where s1.stuid = s2.stuid
        
    --1.子查詢作爲條件
        --查詢學號在王五前邊的同學
        select * from StuInfo where stuid < (select stuid from StuInfo where stuname='王五')
        
    --2.子查詢作爲臨時表
        --查詢李四同學成績大於80分的記錄
        select stuname,subject,score from
        (select * from stuinfo where stuname = '李四') s1,
        (select * from stumarks where score > 80) s2
        where s1.stuid = s2.stuid
        
    --3.子查詢作爲列使用
        --查詢所有學員html成績,沒有成績以null顯示
        select s.*,
        (select score from StuMarks where subject='html' and  s.stuid=StuMarks.stuid) as '成績'
        from StuInfo s

 

 

2、in和not in
    in和not in 通常在where子句中使用
    在in和not in 後接的子查詢中,可以有多個值出現,但必須自能有一列
/*----------------------------------------in  not..in-----------------------------------------------------*/
    --in 符合in後面所有條件都能夠查詢出來,一系列確定的值或表中的某一列
        --查詢學號爲1和3的學員信息
        --select * from stuinfo where stuid = 1 or stuid = 3
        select * from stuinfo where stuid in (1,3)
        --查找Java分數大於80分的學員姓名
        select stuname from stuinfo where stuid in (select stuid from stumarks where subject = 'java' and score > 80)
        --select stuname from stuinfo where stuid in (select stuid,subject from stumarks where subject = 'java' and score > 80)
        
    --not..in 對in取反,不符合in後面所有條件都能夠查詢出來
        --查詢學號除了1和3的以外的學員信息
        select * from stuinfo where stuid not in (1,3)

 

 

3、exiets和 not exists  表示存在不存在
在語句中會判斷exiets和 not exists 後接的句子是否存在和是否不存在,唯一的區別就是意義相反

/*----------------------------------------EXISTS  NOT..EXISTS ---------------------------------------------*/
    --EXISTS 和 NOT..EXISTS 後必須跟子查詢,表示存在和不存在的意思。
        --查詢存在分數的學員的信息
        SELECT * FROM StuInfo WHERE EXISTS (SELECT * FROM StuMarks WHERE StuMarks.StuID = StuInfo.StuID)
        --查詢沒有成績的學員的信息
        SELECT * FROM StuInfo WHERE not EXISTS (SELECT * FROM StuMarks WHERE StuMarks.StuID = StuInfo.StuID)

 


4、some any all
①在sql查詢中,some、any、all後必須跟子查詢
②在sql查詢中,some、any作用一樣,表示其中任何一項;any表示其中所有項

/*----------------------------------------some any all-----------------------------------------------------*/
    --SOME、 ANY、 ALL後必須跟子查詢
        --SOME 和 ANY 的作用是一樣的,表示其中的任何一項
        select * from StuInfo where stuid > any(select stuid from StuInfo where stuid>1)
        select * from StuInfo where stuid > some(select stuid from StuInfo where stuid>1)
        --all表示其中的所有的項
        select * from StuInfo where stuid > all(select stuid from StuInfo where stuid>1)

 

5、compute和compute by     
①compute子句需要下列信息:
可選by關鍵字。它基於每一列計算指定的行聚合
行聚合函數名稱 sum、avg、min、max或count。
要對其行聚合函數的列。

②還有些場景中我們需要對結果先進行分組,然後進行彙總計算。這中情況下我們可以使用compute by進行分組彙總查詢。

/*----------------------------------------compute  聚合技術-----------------------------------------------*/
    --對信息進行查詢並統計
    select * from StuMarks  where subject='html'
    compute max(score),min(score),avg(score)

    --對信息進行分組查詢並統計
    select * from StuMarks order by stuid desc
    compute avg(score),sum(score) by stuid

 


6、/*----------------------------------------排 序 函 數-----------------------------------------------*/
    --排序函數 over([分組子句] 排序語句[排序方式])    
        --row_number()    行號
        select row_number() over(order by score desc) as '排名',
        StuInfo.stuid,stuname,score from StuInfo,StuMarks
        where StuInfo.stuid=StuMarks.stuid  and StuMarks.subject='java'

        --rank()  存在並列時跳空
        select rank() over(order by score desc) as '排名',
        StuInfo.stuid,stuname,stusex,score from StuInfo,StuMarks
        where StuInfo.stuid=StuMarks.stuid  and StuMarks.subject='java'

        --dense_rank()  存在並列時不跳空
        select dense_rank() over(order by score desc) as '排名',
        StuInfo.stuid,stuname,stusex,score from StuInfo,StuMarks
        where StuInfo.stuid = StuMarks.stuid  and StuMarks.subject='java'

        --dense_rank()  存在並列時不跳空
        select dense_rank() over(order by sum(score) desc) as '排名',
        StuInfo.stuid,stuname,stusex,sum(score) '總分' from StuInfo,StuMarks
        where StuInfo.stuid = StuMarks.stuid
        group by StuInfo.stuid,stuname,stusex
        
        --partition by 分組子句
        select dense_rank() over(partition by subject order by score desc)as '排名',
        StuInfo.stuid,stuname,subject,score from StuInfo,StuMarks
        where StuInfo.stuid=StuMarks.stuid

 

 

 

7、/*----------------------------------------公式表表達式-----------------------------------------------*/
    --在一個批中建立一個臨時表,保存所有學生的SQL成績
    --可以用別名,但要與查詢結果列一一對應(學號,姓名,成績)
    WITH StuInfo_StuMarks (StuID, StuName, Score)
    AS
    (
        SELECT S1.StuID, S1.StuName, S2.Score
        FROM StuInfo S1, StuMarks S2
        WHERE S1.StuID=S2.StuID and subject = 'SQL'
    )
    select * from StuInfo_StuMarks
    go

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章