oracle基本操作

總結:什麼時候使用連接查詢?

--當需要顯示的數據來源於多個表時,使用連接查詢。

--一,連接查詢--分傳統連接與內連接

/*

1,查詢員工姓名和所在部門的名稱(2種 )

--傳統連接  --依賴的是    ,和where

*/

select * from emp,dept where emp.deptno=dept.deptno

select ename,dname,emp.deptno from emp ,dept where emp.deptno=dept.deptno;

select * from emp a ,dept b where a.deptno=b.deptno;

select a.ename,b.dname from emp a,dept b where a.deptno=b.deptno

select a.ename,b.dname,a.deptno from emp a,dept b where a.deptno=b.deptno

--內聯接    --依賴的是     inner join   on 

select * from emp a inner join dept b on a.deptno=b.deptno

   

2,查詢員工姓名和所在部門的名稱,要求部門編號爲30(2種 )

select a.ename,b.dname from emp a inner join dept b on a.deptno=b.deptno where a.deptno=30

select a.ename,b.dname from emp a,dept b where a.deptno=b.deptno and a.deptno=30

3,查詢員工姓名和部門名稱,要求沒有員工的部門的名稱也要查詢出來

--傳統連接 :使用”(+)“

select * from emp a ,dept b where a.deptno(+)=b.deptno;

select * from emp a ,dept b where b.deptno=a.deptno(+);

--使用join連接 :   

右外連接

select * from emp a right outer join dept b on a.deptno=b.deptno

  

左外連接  

select * from emp a left outer join dept b on a.deptno=b.deptno;

       

全外連接: 

--只能用標準sql來書寫

 select * from emp a full outer join dept b on a.deptno=b.deptno;

   

4,查詢員工姓名和其直接上級的姓名,要求沒有經理的員工也查詢出來

--自連接

員工 SMITH 的上級是 FORD

select '員工'||a.ename||'的上級是'||b.ename from emp a left outer join emp b on a.mgr=b.empno;

select '員工'||a.ename||'的上級是'||b.ename from emp a,emp b where a.mgr=b.empno(+)

  

select a.ename,b.ename from emp a,emp b where a.mgr=b.empno(+)

2,Demo 子查詢(相關子查詢,嵌套子查詢)

--========================================================

--ylb:Oracle

--17:13 2011-12-30

--1,子查詢(嵌套子查詢、相關子查詢)

--========================================================

/*** 

連接與子查詢的區別:

1,當需要多個表的數據時用連接,子查詢只能返回單表數據。

2,連接快,子查詢慢。

3,子查詢功能強大。

4,子查詢-兩種(嵌套子查詢,關聯子查詢)

 嵌套簡單,關聯複雜,面試關聯查詢

**/

  

  

--一, 子查詢第一種 : 嵌套子查詢:簡單--子查詢可以獨立運行,自內而外

--1,查詢工資高於SMITH工資的所有員工

select * from emp where sal>(select sal from emp where enAme='SMITH')

go

--2,查詢工資高於公司平均工資的所有員工?

select * from emp where sal>(select avg(sal) from emp)

--附加題,

--> >= < <= = != <> ^= 後面只能跟一個值,

--如果有多個值,>all--大於最大值    >any--大於最小值

--查詢工資高於所有部門的平均工資的員工

select * from emp where sal>all(select avg(sal) from emp group by deptno)

--查詢工資高於任何部門的平均工資的員工

  

select * from emp where sal>any(select avg(sal) from emp group by deptno)

      

      

go

/*********************************************************************************/                

--二, 子查詢第二種 : 關聯子查詢 ,思考:自外而內

--3,查詢工資高於本部門平均工資的所有員工?

select * from emp a where a.sal>(select avg(sal) from emp where deptno=a.deptno)

--4,查詢本部門最高工資的員工?(三種方法)

--方法一,使用嵌套子查詢(非關聯子查詢)

select * from emp a where (a.deptno,a.sal) in (select deptno,max(sal) from emp group by deptno)

--方法二,使用關聯子查詢/*9-******************

select * from emp a where a.sal=(select max(sal) from emp where deptno=a.deptno)

--方法三,使用關聯子查詢的名次問題,名次=人數+1 

sal=800

deptno=20

select * from emp a 

where (

select count(*) from emp 

where deptno=a.deptno and sal>a.sal)=1

/*********************************************************************************/   

  

  

go

--補充題:

--查詢本部門第二高工資的員工?(一種方法)

--5,查詢本部門最低工資的員工 ?

select * from emp a where (select count(*) from emp where deptno=a.deptno and sal<a.sal)=0                                   

  

------------------------------------------------------三,select 語句做表達式

--6,統計每個部門的信息和人數?

select a.*,(select count(*) from emp where deptno=a.deptno) 人數 from dept a

select a.* from dept a

     

select a.deptno,b.dname,b.loc,count(*) from emp a,dept b where a.deptno=b.deptno group by a.deptno,b.dname,b.loc

                 

--7,統計每個部門工資在(500-1000)/(1000-3500)/(3500-7000) 的人數?

select a.*,

(select count(*) from emp where deptno=a.deptno and sal>500 and sal<=1000) "500-1000",

(select count(*) from emp where deptno=a.deptno and sal>1000 and sal<=3500),

(select count(*) from emp where deptno=a.deptno and sal>3500 and sal<=7000) 

from dept a

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