柚子快報邀請碼778899分享:數據庫 Oracle 面試二
柚子快報邀請碼778899分享:數據庫 Oracle 面試二
1.查詢語句
* 通配符(所有) select * from emp;
只想要部分內容,指定的列-- 需求:查詢 emp 表中員工編號、姓名、工資
select empno,ename,sal from emp;
算術運算符 +-*/ 也可以使用
運算規(guī)則和數學中的運算規(guī)則是一致的先乘除后加減 有括號先括號 從左往右以此執(zhí)行
空值 null :表示空值空值它本身沒有結構、沒有內容
去重distinct:去重,去掉重復的數據,相同的結果只顯示一個
?1.1 Where 使用 作為條件過濾
where 后面跟條件表達式(要滿足什么要的條件)
-- 查看 10 號部門的員工信息
select * from emp where deptno = 10;
比較運算符?>、<、>=、<=、=、!=(<>)
is null 專門處理空值的,找到空的
between...and... 區(qū)間:在什么之間 區(qū)間是一個連續(xù)閉合的空間(閉合空間:尾和頭都包含其中)
-- 查詢工資在 1500-3000(包含)之前的員工信息
select * from emp where sal between 1500 and 3000;
like 模糊查詢 根據局部查詢整體 _匹配任意一個字符 %匹配0-N個任意字符
-- 查詢名字中,包含A 的
select * from emp where ename like '%A%';
邏輯運算符 and 與,?or 或 , not 非 取反
-- 查詢工資大于 2000 并且是 20號部門的員工信息
select * from emp where sal > 2000 and deptno = 20;
-- 查詢工資大于 1500 或者名字當中包含 A 的員工信息
select * from emp where sal > 1500 or ename like '%A%';
-- 查詢工資不在 1500-3000(包含)之前的員工信息
select * from emp where sal not between 1500 and 3000;
1.1.1 運算的優(yōu)先級
在一個SQL 中,同時出現了以下符號,算術運算符、or、and、not、between、is
算術運算符? 連接符 比較符, is null、like、in, between,? not,? and , or
(括號里的先執(zhí)行,可以使用括號提高代碼的優(yōu)先級)
1.2 Order by 排序
select * from emp order by sal;-- 默認使用升序
asc 升序 desc 降序
select * from emp order by sal asc; -- 按照工資 升序
select * from emp order by sal desc;-- 按照工資 降序
1.3 條件表達式?表達式分為 case 和 decode 函數
1.3.1 case when?
case
when 條件判斷1 then 執(zhí)行結果1
when 條件判斷2 then 執(zhí)行結果2
...
else 執(zhí)行結果
end
/*
如果部門編號是 10、20、30 分別打印 10 返回工資的 1.5 倍 20 返回工資的 2 倍 30 返回工資的 3 倍
*/
select e.*,case deptno? when 10 then sal*1.5 when 20 then sal*2 when 30 then sal*3
? else 0 end 工資倍數
from emp e;
1.3.2 decode函數
decode(參數1,參數2,參數3...)
參數1 對那個列進行判斷
參數2 列是什么值
參數3 怎么處理
decode(列名,值1,返回值1,值2,返回值2,...返回值N)
/*
如果部門編號是 10、20、30 分別打印 10 返回工資的 1.5 倍 20 返回工資的 2 倍 30 返回工資的 3 倍
*/
select e.*,decode(deptno,10,sal*1.5,20,sal*2,sal*3)
from emp e where deptno in(10,20,30);
1.4?鏈接查詢(多表查詢)
1.4.1等值鏈接 顯示兩張具有聯系的數據
select * from emp e,dept d where e.deptno = d.deptno;
1.4.2?非等值鏈接?不是所有表中的列都是等值鏈接的,有的時候就需要用到非等值鏈接
-- 輸出員工信息,并輸出員工工資等級
select e.*,s.grade from emp e,salgrade s
where sal between losal and hisal;
1.4.3?內鏈接?滿足條件的顯示,不滿足條件的過濾掉
select *from emp e join dept d on e.deptno = d.deptno;
內連接只顯示相互關聯的數據,如果是不關聯的數據不顯示
1.4.4?外連接?兩張表相互連接,把滿足條件的數據返回,返回主表不滿足條件的數據, 這種鏈接形式叫做外連接
左外連接,左表是主表? ,右外鏈接,右表是主表
左外鏈接 left join
select * from emp left join dept? on emp.deptno = dept.deptno;
右外鏈接right join
select * from emp right join dept on emp.deptno = dept.deptno;
滿外鏈接 屬于 Oracle 的特性 full、兩張表中不滿足條件的內容全部都顯示出來
select * from emp e full join dept d on e.deptno = d.deptno;
Oracle 中可以用 + 做鏈接 -- 左外鏈接, + 在右邊 ,-- 右外鏈接, + 在左邊?-- + 放到相反的方向
select * from emp e,dept d where e.depeno=d.deptno(+);
select *?from emp e,dept d where e.deptno(+) = d.deptno;
自然鏈接 自然鏈接其實是 內連接和外連接的特殊形式? ,關鍵字 natural 不需要添加 on 會自動識別不需要手動匹配鏈接條件,系統(tǒng)會根據同名的列自動的進行匹配
-- 自然內鏈接
select * from emp natural join dept;
-- 自然外鏈接
select * from emp natural left join dept;
select * from emp natural right join dept;
表中沒有同名的列就鏈接不上,表中同名的列不止一個,就都會進行匹配,都要匹配上
using 子句 指定列進行鏈接
-- 內連接? select * from emp join dept using(deptno);
叉集 笛卡爾積? cross join
-- 返回笛卡爾積? select * from emp cross join dept;
1.5? 聚合函數
sum 求和? max 最大值? min 最小值? ?avg 平均值? count 統(tǒng)計條數
1.6 分組?分組關鍵字 group by?后面跟分組的參照列(按照那個列進行分組)
-- 統(tǒng)計 10 號部門的平均工資
>>>>>>select avg(sal) from emp where deptno = 10;
-- 統(tǒng)計每個部門的平均工資
>>>>>>select deptno,avg(sal) from emp group by deptno;
2. 子查詢? 一個查詢作為另一個查詢的條件,這個查詢稱為子查詢。
2.1 單行子查詢:查詢結果只返回一行數據
#例,查詢工作與7369相同且工資大于7876的員工信息 select empno,ename,sal from emp where job=(select job from emp where empno=7369) and sal > (select sal from emp where empno=7876);
#例 ,查詢工資最低的員工
select empno,ename,sal from emp where sal=(select min(sal) from emp);
上述子查詢等價于:
select empno,ename,sal from emp order by sal asc fetch first 1 rows only;
子查詢使用HAVING首先執(zhí)行子查詢? 向主查詢的having語句返回結果?
#例 查詢哪些部門最低工資比20號部門最低工資高
select deptno,min(sal) from emp group by deptno having min(sal) >(select min(sal) from emp where deptno=20);
# 查詢平均工資最高的職位
select job,avg(sal) from emp group by job having avg(sal)=(select max(avg(sal)) from emp group by job);
2.2?、多行子查詢
操作符
IN 等于列表中的某一個值
ANY 與列表的任意值比較
ALL 與列表所有值比較
#查詢是領導的員工,mgr存的是員工的領導工號
select empno,ename from emp where empno in (select mgr from emp);
?any 使用? < any?:小于最大值 > any?:大于最小值? = any?:等價于 in
#查詢工資比職位是 clerk 的員工中任意(某)一個低的員工信息
select empno,ename,sal from emp where sal < any (select sal from emp where job='CLERK') and job !='CLERK';
all 使用:? < all?:小于最小值? > all?:大于最大值? ?!= all?:等價于not? in
# 例,查詢工資比各部門平均工資都高的員工
select empno,ename,sal from emp where sal > all (select avg(sal) from emp group by deptno);
例,查詢與 7788 號員工 job,sal 相匹配的員工 select empno,ename,sal,job from emp where job=(select job from emp where empno=7788) and sal=(select sal from emp where empno=7788) and empno<>7788;
多行多列子查詢要使用多行操作符,如:
select empno,ename,sal,job from emp where (job,sal) in (select job,sal from emp where deptno=10) ? and empno<>7788;
#例:查詢比本部門平均工資高的員工 select a.empno,a.ename,a.sal,a.job,a.deptno,b.avgsal from emp a, (select deptno,avg(sal) avgsal from emp group by deptno) b where a.deptno=b.deptno and a.sal > b.avgsal;
>>>>>普通子查詢:在主查詢執(zhí)行之前,子查詢首先執(zhí)行一次。子查詢的結果要在主查詢中使用。
2.3 關聯子查詢?:需要重復執(zhí)行子查詢。(where寫在子查詢中)
#例:查詢比本部門平均工資高的員工
select a.empno,a.ename,a.sal,a.deptno from emp a where a.sal > (select avg(sal) from emp b where a.deptno=b.deptno);
#例:查詢員工所屬的部門名
select empno,ename,deptno, (select dname from dept d where a.deptno=d.deptno) from emp a;
2.4?使用exists 和 not?exists
#,查詢是經理的員工
select a.empno,a.ename from emp a where exists (select 1 from emp b where b.mgr=a.empno);
in和exists
in 是把外表和內表作hash?連接,而exists是對外表作loop循環(huán),每次loop循環(huán)再對內表進行查詢。一直以來認為exists比in效率高的說法是不準確的。
如果查詢的兩個表大小相當,那么用in和exists差別不大。
如果兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in:
2.5?集合運算符
集合運算符:union?并集:排序、去重 intersect?交集:排序、去重? minus?差集:排序、去重?
minus要注意操作符前后的表的順序,如:test1 minus test2:參考test1,test2中沒有的;
在進行集合運算時,兩張表的列名可以不一樣,但是數據類型必須一樣(可以隱式轉換都不行)
列column的個數必須一樣
order by 只能在語句最后出現,且只能按第一個SELECT查詢表中的列名來排序
2.6 merge into
柚子快報邀請碼778899分享:數據庫 Oracle 面試二
精彩內容
本文內容根據網絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯系刪除。