Oracle 練習

一、簡單查詢

1.檢索所有教師的姓名、職稱、工資、參加工作時間等信息。
select name,title,wage,hrie_date from teachers;

2.檢索所有教師的姓名、職稱、工資、參加工作時間等信息,日期格式按”YYYY-MM-DD”輸出.
select name,title,wage,TO_CHAR(hire_date,’YYYY-MM-DD’) from teachers;

3.檢索students表中所有行,所有列的信息。要求(*)
select * from students;

4.檢索students表,顯示學生專業列(不帶DISTINCT關鍵字)
select specialty from students

5.檢索students表,顯示學生專業列(帶DISTINCT關鍵字)
select distinct specialty from students;

6.檢索students表,選擇name,dob兩列,並分別使用別名姓名和生日。
select name as姓名,dob as生日from students;

7.檢索students表,選擇name、dob兩列,使用字符串連接,形成學生生日清單。
如:王曉芳生日是:07-5月-88
select name||’生日是:’||dob as學生生日清單 from students;

8.計算所有教師的月總收入
select name as姓名,bonus+wage as月總收入from teachers;

9.計算所有教師的月總收入(bonus+wage)。
注意:當某位教師的bonus不爲NULL時,該教師的月總收入顯示爲(bonus+wage)的 計算結果;但當某位教師的bonus爲null時,該教師月總收入也爲null。
使用NVL(expr1,expr2)函數解決這個問題,如果expr1爲空,取expr2的值,否則取本身的值。
select name as ‘姓名’ ,NVL(bonus,0)+wage as ‘月總收入’ from teachers;

10.計算所有教師的月總收入,並利用函數NVL2()處理bonus出現的情況
NVL2(expr1,expr2,expr3):如果參數表達式expr1值爲null,則NVL2()函數返回參數表達式expr3的值;如果參數表達式expr1的值不爲null,則NVL2()函數返回參數表達式expr2的值。
select name as姓名,NVL2(bonus,bonus+wage,wage)as月總收入from teachers;

11.計算所有教師的月總收入,並利用函數coalesce()處理bonus出現的情況
coalesce(expr1[,expr2]…):返回參數列表中的第一個非空值。如果所有的表達式都是空值,最終將返回一個空值。
select name as姓名,coalesce(bonus,bonus+wage,wage)as月總收入from teachers;

二、條件查詢

1.檢索teachers表中工資大於等於2000元的教師信息
select name,hire_date,title,wage from teachers where wage>=2000;

2.檢索students表中計算機專業的學生信息
select student_id,name,specialty,dob
from students

3.檢索students表中1990年1月1日以前出生的學生信息
select student_id,name,specialty,dob from students where dob<’1-1月-1990’;

4.檢索teachers表中獲得獎金爲500和600元的教師信息

5.檢索students表中1989年12月26日和1990年8月10日出生的學生信息。
select student_id,name,specialty,dob
from students where dob in(’08-10月-1990’,’26-12月-1989’);

6.檢索teachers表中獲得獎金在500~600元之間的教師信息。
select name,hire_date,title,bonus

7.檢索students表中所有姓王的學生信息
select student_id,name,specialty,dob
from students
where name like ‘王%’;

8.檢索teachers表中獎金未定的教師信息
select name,hire_date,title,bonus from teachers where bonus is null;

9.檢索teachers表中無職稱的教師信息
select name,hire_date,title,bonus from teachers where title is null;

10.檢索students表中出生日期未知的學生信息
select student_id,name,specialty,dob
from students
where dob is null;

11.檢索students表中計算機專業男生的學生信息
select student_id,name,sex,specialty
from students
where specialty=’計算機’ AND sex=’男’;

12.檢索students表中計算機和自動化專業的學生信息
select student_id,name,sex,specialty
from students
where specialty=’計算機’ or specialty=’自動化’;

13.檢索students表中不是計算機專業的學生信息
select student_id,name,sex,specialty
from students
where not specialty=’計算機’;

14.檢索students表中歐陽春嵐和高山以外的學生信息
select student_id,name,specialty,dob
from students where name not in(‘歐陽春嵐’,’高山’)

15.檢索students表中在1989年1月1日於1990年1月1日之間出生的學生信息
select student_id,name,specialty,dob
from students
where dob between ‘1-1月-1989’ and ‘1-1月-1990’;

16.檢索students表中不姓張的學生信息
select student_id,name,specialty,dob
from students
where name not like ‘張%’;

17.檢索teachers表中獎金已經確定的教師信息
select name,hire_date,title,bonus
from teachers
where bonus is not null;

18.檢索students表中計算機專業女生和機電工程專業男生的學生信息的學生信息
select student_id,name,sex,specialty from students
where specialty=’計算機’ AND sex=’女’
OR specialty=’機電工程’ AND sex=’男’;

19.檢索teachers表中不是工程師,並且2002年1月1日前參加工作,工資低於3000元的教師信息
select name,hire_date,title,bonus,wage
from teachers
where not title=’工程師’ and hire_date<’1-1月-2002’ and wage<3000;

三、記錄排序

1.按工資由小到大的順序檢索teachers表。
select name,hire_date,title,bonus,wage
from teachers order by wage ASC;

2.按工資由小到大的順序檢索teachers表(升序排序可省略ASC)
select name,hire_date,title,bonus,wage
from teachers order by wage;

3.按學生姓名降序檢索students表(使用列序號)。
select student_id,name,specialty,dob
from students order by 2 desc;

4.按出生日期降序檢索students表(使用列別名)
select name as姓名,dob as出生日期
from students order by出生日期desc;

5.按專業、姓名升序檢索students表
select student_id,name,specialty,dob
from students
order by specialty ,name ;

6.按專業升序,姓名降序檢索students表。
select student_id,name,specialty,dob
from students order by specialty,name desc;

四、分組查詢

1.計算教師的平均工資。
select avg(wage) from teachers;

2.統計全體學生的人數。
select count(*) from students;

3.找出全體學生中最大的及最小的出生日期。
select max(dob) ,min(dob) from students;

4.求出全體教師工資總額
select sum(wage) from teachers;

5.按系部號對teachers表進行分組
select department_id from teachers group by department_id;

6.按系部號及職稱對teachers表進行分組
select department_id , title
from teacher
group by department_id,title;

7.查詢每一個系部的教師工資最大值及工資最小值
select department_id , max(wage) , min(wage)
from teachers
group by department_id;

8.求每一個系部的教師工資總和和工資平均值
select department_id,sum(wage) ,avg(wage)
from teachers
group by department_id;

10.求每一個系部的教師人數
select department_id , count(*)
from teachers
group by department_id;

11.求出每一個系部工資在1000元以上的教師工資平均值
select department_id , avg(wage)
from teachers
where wage>1000
group by department_id;

12.檢索平均工資高於2200元的系部,顯示系部號、平均工資
select department_id,avg(wage) from teachers
group by department_id
having avg(wage)>2200;

13.在工資低於3000元的教師中檢索平均工資高於2000元的系部,顯示系部號、平均工資。
select department_id,avg(wage)
from teachers
where wage<3000
group by department_id
having avg(wage)>2000;

14.在工資低於3000元的教師中檢索平均工資高於2000元的系部,顯示系部號、平均工資,並將顯示結果按平均工資升序排列
select department_id,avg(wage) from teachers
where wage<3000
group by department_id
having avg(wage)>=2000
order by 2;

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