Oracle 數據庫基礎練習②

使用oracle專用語法中的decode()函數,職位是分析員的,工資+1000;職位是經理的,工資+800;職位是其它的,工資+400
decode(字段,條件1,表達式1,條件2,表達式2,…表達式n)
select ename as “姓名”,
sal as “漲前工資”,
job as “職位”,
decode(
job,’ANALYST’,sal+1000,
‘MANAGER’,sal+800,
sal+400)
“漲後工資”
from scott.emp;

MySQL 語句
select ename “姓名”,job “職位”,sal “漲前工資”,
case job
when ‘ANALYST’ then sal+1000
when ‘MANAGER’ then sal+800
else sal+400
end “漲後工資”
from emp;

單引號出現的地方如下:
1)字符串,例如:’hello’
2)日期型,例如:’17-12月-80’
3)to_char/to_date(日期,’YYYY-MM-DD HH24:MI:SS’)

雙引號出現的地方如下:
1)列別名,例如:select ename “姓 名” from emp
2)to_char/to_date(日期,’YYYY”年”MM”月”DD”日” HH24:MI:SS’)‘’號中的英文字符大小寫不敏感

統計emp表中員工總人數
select count(*) from emp;
*號適用於表字段較少的情況下,如果字段較多,掃描多間多,效率低,項目中提倡使用某一個非null唯一的字段,通常是主鍵

統計公司有多少個不重複的部門
select count(distinct deptno) from emp;

統計有佣金的員工人數
select count(comm) from emp;
注意:今天講的這些多個行函數,不統計NULL值

員工總工資,平均工資,四捨五入,保留小數點後0位
select sum(sal) “總工資”,round(avg(sal),0) “平均工資”
from emp;

查詢員工表中最高工資,最低工資
select max(sal) “最高工資”,min(sal) “最低工資”
from emp;

入職最早,入職最晚員工
select max(hiredate) “最晚入職時間”,min(hiredate) “最早入職時間”
from emp;

多行函數:count/sum/avg/max/min

按部門求出該部門平均工資,且平均工資取整數,採用截斷
select deptno “部門編號”,trunc(avg(sal),0) “部門平均工資”
from emp
group by deptno;

(繼續)查詢部門平均工資大於2000元的部門
select deptno “部門編號”,trunc(avg(sal),0) “部門平均工資”
from emp
group by deptno
having trunc(avg(sal),0) > 2000;

(繼續)按部門平均工資降序排列
select deptno “部門編號”,trunc(avg(sal),0) “部門平均工資”
from scottemp
group by deptno
having trunc(avg(sal),0) > 2000
order by 2 desc;

除10號部門外,查詢部門平均工資大於2000元的部門,方式一【having deptno<>10】
select deptno,avg(sal)
from scott.emp
where deptno<> 10
group by deptno
having trunc(avg(sal),0)>2000;

除10號部門外,查詢部門平均工資大於2000元的部門,方式二【where deptno<>10】
select deptno,avg(sal)
from scott.emp
where deptno<>10
group by deptno
having trunc(avg(sal),0)>2000;

顯示部門平均工資的最大值
select max(avg(sal)) “部門平均工資的最大值”
from emp
group by deptno;

思考:顯示部門平均工資的最大值和該部門編號?
select max(avg(sal)) “部門平均工資的最大值”,deptno “部門編號”
from scott.emp
group by deptno;
錯誤

group by 子句的細節:
1)在select子句中出現的非多行函數的所有列,【必須】出現在group by子句中
2)在group by子句中出現的所有列,【可出現可不現】在select子句中

where和having的區別:
where:
1)行過濾器
2)針對原始的記錄
3)跟在from後面
4)where可省
5)先執行

having:
1)組過濾器
2)針對分組後的記錄
3)跟在group by後面
4)having可省
5)後執行

oracle中綜合語法:
1)select子句—–必須
2)from子句——-必須,不知寫什麼表了,就寫dual
3)where子句——可選
4)group by子句—可選
5)having子句—–可選
6)order by 子句–可選,如果出現列名,別名,表達式,字段

————————————————————————————-多表查詢

員工表emp和部門表dept的笛卡爾集(笛卡爾集表=列數之和,行數之積,笛卡爾集表內中有些數據是不符合要求的)
select emp.ename,dept.dname
from scott.emp,scott.dept;

使用等值連接/內連接(只能使用=號),顯示員工的編號,姓名,部門名,使用表別名簡化
select emp.empno,emp.ename,dept.dname,dept.deptno
from scott.emp,scott.dept
where scott.emp.deptno = scott.dept.deptno;

使用非等值連接(不能使用=號,其它符號可以,例如:>=,<=,<>,betwen and等),顯示員工的編號,姓名,月薪,工資級別
select e.empno,e.ename,e.sal,s.grade
from scott.emp e,scott.salgrade s
where e.sal between s.losal and s.hisal;

內連接查詢:只能查詢出符合條件的記錄
外連接查詢:既能查詢出符合條件的記錄,也能根據一方強行將另一個方查詢出來

使用外連接,按部門10,20,30,40號,統計各部門員工人數,要求顯示部門號,部門名,人數
部門號 部門名 人數
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6
40 OPERATIONS 0

等值連接/非等值連接/內連接:只會查詢出多張表中,根據某個字段匹配,符合條件的記錄,不符合條件的記錄是不會存在的

左外連接[是oracle專用的,不是SQL99規則]:
select dept.deptno as “部門號”,dept.dname as “部門名”,count(emp.empno) as “人數”
from scott.dept,scott.emp
where scott.dept.deptno = scott.emp.deptno(+)
group by dept.deptno,dept.dname;

右外連接:
select dept.deptno “部門號”,dept.dname “部門名”,count(emp.empno) “人數”
from scott.dept,scott.emp
where emp.deptno(+) = dept.deptno
group by dept.deptno,dept.dname;

使用左外連接,按部門10,20,30,40號,統計各部門員工人數,要求顯示部門號,部門名,人數,且按人數降序排列
select dept.deptno “部門號”,dept.dname “部門名”,count(emp.empno) “人數”
from scott.dept,scott.emp
where dept.deptno = emp.deptno(+)
group by dept.deptno,dept.dname
order by 3 desc;

使用自連接,顯示”SMITH的上級是FORD”這種格式
select users.ename || ‘的上級是’ ||boss.ename
from scott.emp users,scott.emp boss
where users.mgr = boss.empno;
只有13條記錄,不含有KING

基於上述問題,將KING的上級是“”顯示出來
select users.ename || ‘的上級是’ ||boss.ename
from scott.emp users,scott.emp boss
where users.mgr = boss.empno(+);
14條記錄
注意:自連接也用到內連接和外連接

————————————————————————————-子查詢

子查詢的作用:查詢條件未知的事物

查詢條件已知的問題:例如:查詢工資爲800的員工信息
查詢條件未知的問題:例如:查詢工資爲20號部門平均工資的員工信息
一個條件未知的問題,可以分解爲多個條件已知的問題

查詢工資比WARD高的員工信息
第一:查詢WARD的工資?
select sal from emp where ename = ‘WARD’;

第二:查詢工資比1250高的員工信息?
select * from emp where sal > 1250;

子查詢:
select *
from emp
where sal > (
select sal
from emp
where ename = ‘WARD’
);

查詢部門名爲’SALES’的員工信息(方式一:子查詢)

第一:查詢部門名爲’SALES’的編號?
select deptno from dept where dname = ‘SALES’;
第二:查詢部門號爲30的員工信息?
select * from emp where deptno = 30;
子查詢:
select *
from emp
where deptno = (
select deptno
from dept
where dname = ‘SALES’
);

子查詢細節:
1)子查詢與父查詢可以針對同一張表
2)子查詢與父查詢可以針對不同張表
3) 子查詢與父查詢在傳統參數時,數量要相同
4) 子查詢與父查詢在傳統參數時,類型要相同
5) 子查詢與父查詢在傳統參數時,含義要相同

查詢部門名爲’SALES’的員工信息(方式二:多表查詢)
select emp.*
from dept,emp
where (dept.deptno=emp.deptno) and (dept.dname=’SALES’);

查詢每個員工編號,姓名,部門名,工資等級(三表查詢,這三張表並無外健關聯)
select e.empno as “員工編號”,e.ename as “姓名”,d.dname as “部門名”,s.grade as “工資等級”
from scott.emp e,scott.dept d,scott.salgrade s
where (e.deptno=d.deptno) and (e.sal between s.losal and s.hisal);

查詢工資最低的員工信息(單行子查詢,使用=號)
第一:查詢出工資最低是多少?
select min(sal) from emp;
第二:查詢工資爲800的員工信息?
select * from emp where sal = 800;
子查詢:
select *
from emp
where sal = (
select min(sal)
from emp
);

查詢部門名爲’ACCOUNTING’或’SALES’的員工信息(多行子查詢,使用in關鍵字)
第一:查詢部門名爲’ACCOUNTING’或’SALES’的部門編號?
select deptno from dept where dname in (‘ACCOUNTING’,’SALES’);
第二:查詢部門號爲10或30號的員工信息?
select * from emp where deptno in (10,30);
子查詢:
select *
from emp
where deptno in (
select deptno
from dept
where dname in (‘ACCOUNTING’,’SALES’)
);

查詢工資比20號部門【任意any】一個員工工資【低<】的員工信息(多行子查詢,使用any關鍵字)
第一:查詢20號部門的所有工資?
select sal from emp where deptno = 20;
第二:查詢工資比(800,2975,3000,1100,3000)任意一個低的員工信息?
select * from emp where sal < any (800,2975,3000,1100,3000);
在oracle看來,

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