Oracle資料庫筆記之子查詢

2021-08-03 20:44:14 字數 3031 閱讀 3491

1.子查詢返回的結果是乙個值  --單行子查詢

使用 = > < <= >= <> between...and... 

查詢與scott同乙個職位的員工資訊

select * 

from emp 

where job=(select job from emp where ename='scott');

查詢與smith同乙個部門的員工資訊

select *

from emp 

where deptno=(select deptno from emp where ename='smith');

查詢比ford工資高的員工資訊

select *

from emp 

where sal>(select sal from emp where ename='ford');

查詢比adams入職晚的員工資訊

select * from emp

where hiredate>(select hiredate from emp where ename='adams');

2.子查詢返回的結果是多個值 --多行子查詢

使用in 

not in 

查詢與ford同一部門或與clark同一部門的員工資訊

select * from emp

where deptno in (select deptno from emp where ename='ford' or ename='clark');

查詢與clark和smith不同工作的員工資訊

select * from emp

where job not in (select job from emp where ename='clark' or ename='smith');

any --任何乙個

all --全部

查詢比部門30中工資高的員工資訊

select * from emp

where sal > any (select sal from emp where deptno=30);

select * from emp

where sal >  (select min(sal) from emp where deptno=30);

查詢比部門30中工資都高的員工資訊

select * from emp

where sal > all (select sal from emp where deptno=30);

select * from emp

where sal >  (select max(sal) from emp where deptno=30);

查詢與smith同乙個部門,並且工作相同的員工資訊

select * from emp

where deptno=(select deptno from emp where ename='smith') 

and job=(select job from emp where ename='smith');

select * from emp

where (deptno,job)=(select deptno,job from emp where ename='smith') ;

將子查詢作為臨時表和其他表連線

注意:要為當前子查詢和子查詢中的字段起別名

查詢每個部門工資最高的員工資訊

select * from emp e,(select max(sal) as max_sal,deptno dno from emp group by deptno) t 

where e.sal=t.max_sal  and e.deptno=t.dno;

查詢每個部門最早入職的員工資訊

select * from emp e,(select min(hiredate) min_hiredate,deptno dno from emp group by deptno) t

where t.min_hiredate=e.hiredate and t.dno=e.deptno;

查詢每個部門的員工數,部門名稱,部門位址

select num,dname,loc from dept d,(select count(*) num,deptno dno from emp group by deptno) t

where t.dno=d.deptno;

分頁查詢:

不同資料庫的處理方式不同

mysql ------limit關鍵字

sqlserver ------top關鍵字

oracle ------rownum偽列

從m到n 

1.確定查詢條件

2.設定n

3.設定m

分頁查詢的語法

select b.* 

from  (select t.*,rownum as num

from (select * from 表名 where 條件) t

where rownum <=n) b

where num>m;

查詢員工表中前六條資料

select * from emp where rownum<=6;

查詢員工表中第3—7條資料

select * from emp where rownum>= 3 and rownum<=7;--錯誤

select * from emp where rownum between 3 and 7;  --錯誤

查詢員工表中名字帶s的第2-4名員工資訊

select e.* 

from  (select t.*,rownum as num

from (select * from emp where ename like '%s%') t

where rownum <=4) e

where num>=2;

select t.*

from (select rownum num,emp.* from emp where ename like '%s%' and rownum<=4) t

where num>=2;

mySQL資料庫之子查詢與多表查詢

user info表 create table user info id int 2 primary key,user name varchar 12 unique password varchar 15 not null real name varchar 8 not null age int 3...

Oracle資料庫查詢

取得該使用者下所有的表 select from user tables 取得表名為classinfo的注釋資訊 select from user tab comments where table name classinfo 取得該使用者下表名為classinfo表的結構 select from u...

Oracle之子查詢(巢狀查詢select巢狀)

查詢最高工資的員工資訊 1.查詢出最高工資 5000 select max sal from emp 2.工資等於最高工資 select from emp where sal select max sal from emp 查詢出比雇員7654的工資高,同時和7788從事相同工作的員工資訊 1.雇員...