練習一:導入數據
將上機作業中school.dmp文件中的數據導入到資料庫中。
該文件中包含4個表數據,分別是:StudentInfo表,TeacherInfo表,ClassInfo表,StudentExam表。
select * from studentinfo;
練習二:單行函數練習
1、查詢所有學員從入學到今天,一共度過了多少天
select studentinfo.*, ceil(sysdate-stujointime) as time from studentinfo;
2、查詢每月2號入學的學員信息
select * from
(select studentinfo.*,to_char(stujointime,'mm') from studentinfo ) where to_char(stujointime,'mm')=2
select * from studentinfo where to_char(stujointime,'dd')=2
select * from studentinfo where to_char(stujointime,'mm')=3
3、查詢所有學員的畢業日期,假定按每個學員入學時間1年半之後將畢業。
select stuname,add_months(stujointime,18) from studentinfo
4、查詢星期四入學的學員姓名,性別,年齡,班級編號
select stuname,stusex,stuage,sclassid from studentinfo where to_char(stujointime,'day')='星期四'
5、查詢『2007-3-10』之前入學的學員信息
select * from studentinfo where stujointime < to_date('2007-3-10','yyyy-mm-dd')
6、查詢所有學員姓名的長度
select studentinfo.stuname,length(stuname) from studentinfo
7、查詢身份證中第9,10位為『89』的學員信息(要求使用字符串函數)
select * from studentinfo where substr(stucard,instr(stucard,'89'),2)='89'
8、修改班主任信息,將郵箱中的『yahoo』替換為『accp』
select * from teacherinfo
select replace(teacheremail,'yahoo','accp') from teacherinfo
9、查詢所有班主任的郵箱的用戶名
select substr(teacheremail,1,instr(teacheremail,'@')-1) from teacherinfo
10、查詢所有班主任的郵箱的所屬網站
提示:如果郵箱為qtz@yahoo.com,用戶名即qtz,所屬網站即yahoo。可先查找出『@』和『.』的下標,再截取
select substr(teacheremail,instr(teacheremail,'@')+1,instr(teacheremail,'.')-instr(teacheremail,'@')-1)
from teacherinfo
11、編寫查詢語句去掉字符串『 愛你 要你 我 愛 你 』中的空格
select replace(' 愛你 要你 我 愛 你 ',' ','') from dual
12、計算每個學員身份證中字符『1』出現的次數
select length(stucard)-length(replace(stucard,'1')) from studentinfo where stuid = 8
13、求小於-58.9的最大整數
select ceil(examresult) from studentexam where examresult<60
14、求大於78.8的最小整數
select floor(examresult) from studentexam where examresult>78.8
15、求64除以7的餘數
select mod(64,7) from dual
16、查詢所有學員入學時間,要求顯示格式為『2007年03月02日』
select to_char(stujointime,'yyyy"年"mm"月"dd日')期
17、查詢當前時間,要求顯示格式為『22時57:37』
select to_char(sysdate,'hh"時"mi":"ss') from dual;
18、查詢2007年入學的學員信息
select * from studentinfo where to_char(stujointime,'yyyy') = '2007'
triggers
練習三:分組函數練習
1、查詢所有學員的平均年齡(要求保留兩位小數)
select trunc(avg(stuage),2) from studentinfo ;
2、查詢所有考試的總成績
select examsubject, avg(examresult) from studentexam group by examsubject
3、查詢SQL考試的最低分數
select * from studentexam
select examresult from ( select row_number()over(order by examresult)r,studentexam.* from studentexam where examsubject='SQL')
where r=1
4、查詢Java考試成績最高的學員姓名
select examresult from (select dense_rank()over(order by examresult desc)r ,studentexam.* from studentexam where examsubject='Java')
where r=1
5、查詢學員『火雲邪神』一共參加了幾次考試
select * from studentinfo
select * from studentexam
select count(*) from studentinfo,studentexam where studentinfo.stuid=studentexam.estuid and stuname='火雲邪神'
6、查詢各科目的平均成績
select examsubject ,avg(examresult) from studentexam group by examsubject
7、查詢每個班級學員的最小年齡
select sclassid,min(stuage) from studentinfo group by sclassid;
8、查詢考試不及格的人數
select count(*) from studentexam where examresult<60
9、查詢各學員的總成績,要求篩選出總成績在140分以上的
select * from studentexam
select * from (select sum(examresult)>140 from studentexam where group by estuid ) group by estuid
select * from ( select sum(examresult),estuid from studentexam
group by estuid )m where
select * from
(select sum(examresult) total,estuid from studentexam
group by estuid
)where total>140
10、查詢男女學員的平均年齡
select stusex ,avg(stuage) from studentinfo group by stusex
11、查詢每門功課的平均分,要求顯示平均分在80分以上的(包括80分)
select * from (select avg(examresult)total from studentexam group by examsubject) where total>80
12、按班主任姓名分組,查所帶班級的總成績分(假定每個班主任只帶一個班級)(提示:4表連接)
--查去來的值,類型也是一樣的
select sclassid from studentinfo --這個一個真正的集合,值是唯一的
union
select classid from classinfo;
select sclassid from studentinfo
union all
select classid from classinfo;--這是別種一集合,沒有過濾相同的值,沒有對值進行
select * from studentexam
union all
select * from teacherinfo;
select sclassid from studentinfo
intersect
select classid from classinfo; -- 這是一個交集
select sclassid from studentinfo
minus
select classid from classinfo;
練習四:分析函數練習
查詢學員成績,按成績排序,並計算出名次
1、 要求不論成績是否相同,名次是連續的序號
select studentexam.* , row_number()over(order by examresult)rown from studentexam
2、 要求成績相等的排位相同,名次隨後跳躍
select studentexam.* ,rank()over(order by examresult)rank_ from studentexam
3、 要求成績相等的排位相同,名次是連續的
select studentexam.* , dense_rank()over(order by examresult)denserank from studentexam