第五章 數據高級查詢

1、聚合函數
    1)sum   求和彙總
            select    sum(列)    from    表名
  1. --查詢所有成績的總分
  2. select sum(score) as sum_score from score
    
    2)avg    平均值
  1. --查詢班級的平均分
  2. select round(avg(score)) as avg_score from score

    3)max    最大值
  1. --查詢最高分
  2. select max(score) as max_score from score

    4)min    最小值
  1. --查詢最低分
  2. select min(score) as min_score from score

    5)count    行數
            count函數的參數如果爲列名則計算該有值的行數
  1. --查詢考試表的總行數
  2. select count(*) as score_count from score

例子:
  1. --查詢編號爲2的課程的最高分
  2. select max(score) from score where courseId=2
  3. --查詢學號爲1001的同學的考試的次數
  4. select count(*) from score where stuId=1001
  5. --查詢學號爲1000的同學所考的編號爲2的課程的總分和平均分
  6. select sum(score),round(avg(score),1) from score where stuId=1000 and courseId=2

2、分組查詢
        select    列名    from    表名    group    by    列
        
        聚合函數在分組查詢的過程將執行多次,取決於分組的組數。
        如果使用分組查詢或聚合查詢時,只能查詢被分組的列或者是聚合列
        如下是錯誤的:
  1. select stuId,sum(score) from score where stuId=1000
        正確的例子:        
  1. select courseId,avg(score) from score group by courseId

        先執行where條件,當滿足條件的數據篩選後再進行分組操作
  1. select courseId,avg(score) from score where stuId=1000 group by courseId
        
        允許對多個列進行分組
                將多個列作爲整體進行分組。
  1. select stuId,,courseId from score group by courseId,stuId
  1. --查詢所有及格的每一個同學在每門課程中的最低分
  2. select stuId,courseId,min(score) from score where score>=60 group by stuId,courseId
    
        where 條件不能使用聚合函數,可以使用having。
        having條件篩選(having只能和group by 結合使用)
            select    列名    from    表名    group    by    列    having    條件
        
  1. --查詢單科考試4次以上的平均分
  2. select courseId,avg(score) from score group by courseId having count(*)>4
  3. --查詢參加過補考的學生學號
  4. select stuId from score group by stuId,courseId having count(*)>1

         having 和 where條件的相同點和區別:
                1)都表示條件的過濾
                2)having只能寫在group 後,where可以出現任何情況
                3)先執行where再執行group    by最後執行having
                4)having中可以使用聚合函數,where不能使用

3、多表連接
        表連接查詢
            1)inner join
                    內連接查詢,獲取兩表中共同部分的數據
                    select    列    from    表A    inner    join    表B    on    表A.列=表B.列    where    條件
                    下面方式等同內連接:
                    select    列 from    表A,表B    where    表A.列=表B.列    and  條件   
  1. --查詢學生的姓名和對應的成績
  2. select stuName,score from student stu inner join score s on stu.stuId=s.stuId
  3. --作用等同於上述方式
  4. select stuName,score from student,score where student.stuid=score.stuid
例:
  1. --查詢編號爲102的員工姓名和所在部門的名稱
  2. select first_name||last_name as empName,department_name
  3. from emp inner join dep on emp.department_id=dep.department_id where employee_id=102
  4. select first_name||last_name as empName,department_name
  5. from emp,dep where emp.department_id=dep.department_id and employee_id=102
  6. --查詢學生的姓名,課程名稱,成績
  7. select stuName,courseName,score from student stu
  8. inner join score s on stu.stuId=s.stuId
  9. inner join course c on s.courseId=c.courseId
  10. --查詢每一個員工的姓名,崗位名稱,薪水和部門名稱(要求使用inner join實現)
  11. select first_name||last_name as empName,job_title,department_name,salary from
  12. emp inner join dep on emp.department_id=dep.department_id
  13. inner join job on emp.job_id=job.job_id
  14. --查詢部門編號爲100的員工姓名,部門名稱,崗位名稱(要求是用from a,b,c實現)
  15. select first_name||last_name as empName,job_title,department_name,salary from
  16. emp,dep,job where emp.department_id=dep.department_id and emp.job_id=job.job_id and emp.department_id=100
  17. --查詢所有發帖用戶的姓名,發帖的標題,內容,回帖人的姓名,回帖內容
  18. select u1.uname as publish_name,ttopic,tcontents,u2.uname as reply_name,rcontents
  19. from bbsusers u1 inner join bbstopic t on u1.userid=t.tuid
  20. inner join bbsreply r on t.tid=r.rtid
  21. inner join bbsusers u2 on u2.userid=ruid

            2)left   join
                    左連接 獲取左表中的所有數據和右表中匹配的數據
                    左連接查詢左表的所有數據以及右表中和左表關聯條件相同的共性數據
  1. insert into card
  2. select cardId.nextval,a.accountId,1 from bank_account a left join card c on a.accountId=c.accountId where c.accountId is null

            3)right  join
                    右連接 獲取右表中的所有數據和左表中匹配的數據

4、子查詢
        子查詢即查詢語句使用嵌套的查詢,在SQL語句中可以將查詢的結果作爲呈現數據或者將結果作爲條件再次進行條件篩選
        select    列名    from    表名    where    列名=(select    列名    from    表名)
        子查詢的應用場景:
               1) 將查詢的結果作爲值繼續進行查詢
               2)將查詢的結果作爲表格繼續進行查詢。
        如果將結果集作爲值來查詢的,同時使用in來關聯該結果時,則結果集可以是一行或者是多行的單列數據
        如果將結果集作爲值來查詢的,使用in以外的運算符來關聯該結果時,則結果集必須是單行單列的數據
  1. select * from users where age>(select age from users where lower(userName)='tom');
  2. --查詢Java課程的平均分
  3. select avg(score) from score where courseId=(select courseId from course where courseName='Java')

子查詢的注意點:
            子查詢必須寫在括號中
            子查詢的結果集可以作爲列值或新表繼續查詢
            子查詢不能作爲order by的表達式

例子:
  1. --查詢成績比jackOracle課程平均分高的學生的編號和課程編號以及成績
  2. select stuId,courseId,score from score where score>
  3. (select avg(score) from score where stuId=(select stuId from student where stuName='jack')
  4. and courseId=(select courseId from course where courseName='Oracle'))
  5. --查詢成績比jackOracle課程平均分高的學生的姓名和課程名稱以及成績
  6. select stuName,courseName,score from student stu,course c,
  7. (select stuId,courseId,score from score where score>
  8. (select avg(score) from score where stuId=(select stuId from student where stuName='jack')
  9. and courseId=(select courseId from course where courseName='Oracle'))) new_table
  10. where stu.stuId=new_table.stuId and c.courseId=new_table.courseId

嵌套子查詢
標量子查詢
    標量子查詢,將子查詢的結果作爲常量列呈現
  1. --查詢所有員工最高的薪資和最低薪資
  2. select (select max(salary) from emp),(select min(salary) from emp) from dual
  3. --查詢出員工最最高的薪資以及部門編號最高的部門名稱
  4. select (select max(salary) from emp),(select department_name from dep where department_id=(select max(department_id) from dep)) from dual
  5. --distinct用來篩選掉重複的數據
  6. select distinct(department_id) from emp;

相關子查詢
    子查詢語句依賴於外部的主查詢語句,因此相關子查詢是先執行主查詢語句再執行子查詢語句
  1. --查詢學生的姓名和他的成績
  2. select score,(select stuName from student where student.stuId=score.stuId) from score
  3. --查詢emp表中員工的姓名,崗位名稱和部門名稱
  4. select first_name||last_name as empName,
  5. (select department_name from dep where dep.department_id=emp.department_id) as depName,
  6. (select job_title from job where job.job_id=emp.job_id) as jobName from emp
發佈了46 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章