1、聚合函數
1)sum 求和彙總
select sum(列) from 表名
--查詢所有成績的總分
select sum(score) as sum_score from score
2)avg 平均值
--查詢班級的平均分
select round(avg(score)) as avg_score from score
3)max 最大值
--查詢最高分
select max(score) as max_score from score
4)min 最小值
--查詢最低分
select min(score) as min_score from score
5)count 行數
count函數的參數如果爲列名則計算該有值的行數
--查詢考試表的總行數
select count(*) as score_count from score
例子:
--查詢編號爲2的課程的最高分
select max(score) from score where courseId=2
--查詢學號爲1001的同學的考試的次數
select count(*) from score where stuId=1001
--查詢學號爲1000的同學所考的編號爲2的課程的總分和平均分
select sum(score),round(avg(score),1) from score where stuId=1000 and courseId=2
2、分組查詢
select 列名 from 表名 group by 列
聚合函數在分組查詢的過程將執行多次,取決於分組的組數。
如果使用分組查詢或聚合查詢時,只能查詢被分組的列或者是聚合列
如下是錯誤的:
select stuId,sum(score) from score where stuId=1000
正確的例子:
select courseId,avg(score) from score group by courseId
先執行where條件,當滿足條件的數據篩選後再進行分組操作
select courseId,avg(score) from score where stuId=1000 group by courseId
允許對多個列進行分組
將多個列作爲整體進行分組。
select stuId,,courseId from score group by courseId,stuId
--查詢所有及格的每一個同學在每門課程中的最低分
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 條件
--查詢單科考試4次以上的平均分
select courseId,avg(score) from score group by courseId having count(*)>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 條件
--查詢學生的姓名和對應的成績
select stuName,score from student stu inner join score s on stu.stuId=s.stuId
--作用等同於上述方式
select stuName,score from student,score where student.stuid=score.stuid
例:
--查詢編號爲102的員工姓名和所在部門的名稱
select first_name||last_name as empName,department_name
from emp inner join dep on emp.department_id=dep.department_id where employee_id=102
select first_name||last_name as empName,department_name
from emp,dep where emp.department_id=dep.department_id and employee_id=102
--查詢學生的姓名,課程名稱,成績
select stuName,courseName,score from student stu
inner join score s on stu.stuId=s.stuId
inner join course c on s.courseId=c.courseId
--查詢每一個員工的姓名,崗位名稱,薪水和部門名稱(要求使用inner join實現)
select first_name||last_name as empName,job_title,department_name,salary from
emp inner join dep on emp.department_id=dep.department_id
inner join job on emp.job_id=job.job_id
--查詢部門編號爲100的員工姓名,部門名稱,崗位名稱(要求是用from a,b,c實現)
select first_name||last_name as empName,job_title,department_name,salary from
emp,dep,job where emp.department_id=dep.department_id and emp.job_id=job.job_id and emp.department_id=100
--查詢所有發帖用戶的姓名,發帖的標題,內容,回帖人的姓名,回帖內容
select u1.uname as publish_name,ttopic,tcontents,u2.uname as reply_name,rcontents
from bbsusers u1 inner join bbstopic t on u1.userid=t.tuid
inner join bbsreply r on t.tid=r.rtid
inner join bbsusers u2 on u2.userid=ruid
2)left join
左連接 獲取左表中的所有數據和右表中匹配的數據
左連接查詢左表的所有數據以及右表中和左表關聯條件相同的共性數據
insert into card
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以外的運算符來關聯該結果時,則結果集必須是單行單列的數據
select * from users where age>(select age from users where lower(userName)='tom');
--查詢Java課程的平均分
select avg(score) from score where courseId=(select courseId from course where courseName='Java')
子查詢的注意點:
子查詢必須寫在括號中
子查詢的結果集可以作爲列值或新表繼續查詢
子查詢不能作爲order by的表達式
例子:
--查詢成績比jack的Oracle課程平均分高的學生的編號和課程編號以及成績
select stuId,courseId,score from score where score>
(select avg(score) from score where stuId=(select stuId from student where stuName='jack')
and courseId=(select courseId from course where courseName='Oracle'))
--查詢成績比jack的Oracle課程平均分高的學生的姓名和課程名稱以及成績
select stuName,courseName,score from student stu,course c,
(select stuId,courseId,score from score where score>
(select avg(score) from score where stuId=(select stuId from student where stuName='jack')
and courseId=(select courseId from course where courseName='Oracle'))) new_table
where stu.stuId=new_table.stuId and c.courseId=new_table.courseId
嵌套子查詢
標量子查詢
標量子查詢,將子查詢的結果作爲常量列呈現
--查詢所有員工最高的薪資和最低薪資
select (select max(salary) from emp),(select min(salary) from emp) from dual
--查詢出員工最最高的薪資以及部門編號最高的部門名稱
select (select max(salary) from emp),(select department_name from dep where department_id=(select max(department_id) from dep)) from dual
--distinct用來篩選掉重複的數據
select distinct(department_id) from emp;
相關子查詢
子查詢語句依賴於外部的主查詢語句,因此相關子查詢是先執行主查詢語句再執行子查詢語句
--查詢學生的姓名和他的成績
select score,(select stuName from student where student.stuId=score.stuId) from score
--查詢emp表中員工的姓名,崗位名稱和部門名稱
select first_name||last_name as empName,
(select department_name from dep where dep.department_id=emp.department_id) as depName,
(select job_title from job where job.job_id=emp.job_id) as jobName from emp