日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oracle高级查询案例,oracle高级查询(实例基于scott用户四张表)

發布時間:2024/1/23 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle高级查询案例,oracle高级查询(实例基于scott用户四张表) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

oracle高級查詢(實例基于scott用戶四張表)

分組查詢

多表查詢

子查詢

綜合實例

=======================================================================

scott用戶的四張表(emp,dept,bonus,salgrade)

沒有這四張表的可參考http://blog.csdn.net/love_legain/article/details/54311040進行創建

-------------------------------------------

desc emp

名稱 空值 類型

-------- -------- ------------

EMPNO NOT NULL NUMBER(4)

ENAME VARCHAR2(10)

JOB VARCHAR2(9)

MGR NUMBER(4)

HIREDATE DATE

SAL NUMBER(7,2)

COMM NUMBER(7,2)

DEPTNO NUMBER(2)

---------------------------------------------

desc dept

名稱 空值 類型

------ -------- ------------

DEPTNO NOT NULL NUMBER(2)

DNAME VARCHAR2(14)

LOC VARCHAR2(13)

------------------------------------------------

desc salgrade

名稱 空值 類型

----- -- ------

GRADE NUMBER

LOSAL NUMBER

HISAL NUMBER

------------------------------------------------

desc bonus

名稱 空值 類型

----- -- ------------

ENAME VARCHAR2(10)

JOB VARCHAR2(9)

SAL NUMBER

COMM NUMBER

=============================分組查詢==================================

①分組函數的概念

分組函數作用于一組數據,并對一組數據返回一個值

②分組函數的使用

--select AVG(sal),sum(sal) from emp;

-- select max(sal),min(sal) from emp;

--select count(*) from emp;

--select count (distinct DEPTNO) from emp;

wm_concat:行轉列

select deptno,wm_concat(ename) from emp group by deptno--11gr2和12C上已經摒棄了wm_concat函數

在分組函數中使用nvl函數:nvl函數使分組函數無法忽略空值

select count(*),count(NVL(comm,0)) from emp;

③使用group by子句數據分組

select deptno,avg(sal) from emp group by deptno;

注意:在select列表中所有未包含在組函數中的列都應該包含在group by子句中

包含在group by子句中的列不必包含在select列表中

④使用having子句過濾分組結果集

不能再where子句中使用分組函數

可以在having子句中使用分組函數

select deptno,avg(sal) from emp group by deptno having deptno=10;

select deptno,avg(sal) from emp where deptno=10 group by deptno;

⑤在分組查詢中使用order by子句

⑥group by語句得增強

select deptno,job,sum(sal) from emp group by deptno,job;

+

select deptno,sum(sal) from emp group by deptno;

+

select sum(sal) from emp;

=

select deptno,job,sum(sal) from emp group by rollup(deptno,job);

sql*plus的報表功能

================================多表查詢================================

①什么是多表查詢

從多個表中獲取數據

②笛卡爾積

③等值連接

select e.empno,e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno;

④不等值連接

select e.empno,e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;

⑤外連接

核心:通過外鏈接,把鏈接不成立的記錄,任然包含在最后的結果中

左外連接:當連接條件不成立的時候,等號左邊的表依然被包含

右外連接:當連接條件不成立的時候,等號右邊的表依然被包含

select d.deptno部門號,d.dname部門名稱,count(e.empno)人數 from emp e,dept d where e.deptno(+)=d.deptno group by d.deptno,d.dname;--右外連接

⑥自連接

核心:通過別名,將同一張表視為多張表

select e.ename員工姓名,b.ename老板姓名 from emp e,emp b where e.mgr=b.empno;

自連接存在的問題:不適合操作大表

⑦解決方法:層次查詢

select level,empno,ename,sal,mgr from emp connect by prior empno=mgr start with mgr is null order by 1;

==============================子查詢===================================

①子查詢概述

--查詢比scott工資高的員工信息

select * from emp where sal>(select sal from emp where ename=‘scott‘);

②子查詢的使用

可以使用子查詢的位置:where,select,having,from

主查詢和子查詢可以不是同一張表

select * from emp where deptno=(select deptno from dept where dname=‘SALES‘);

select e.* from emp e,dept d where e.deptno=d.deptno and d.dname=‘SALES‘;

一般不在子查詢中,使用排序,但在top-n分析問題中,必須對子查詢排序

--rownum行號 偽列

select rownum,empno,ename,sal from (select * from emp order by sal desc) where rownum <= 3;

行號永遠按照默認的順序生成

行號只能使用,>=

一般先執行子查詢,再執行主查詢,但相關子查詢除外

select empno,ename,sal,(select avg(sal)from emp where deptno=e.deptno) avgsal from emp e where sal>(select avg(sal)from emp where deptno=e.deptno);

單行子查詢和多行子查詢

操作符(多行)

in等于列表中的任何一個

any和子查詢返回的任意一個值比較

all和子查詢返回的所有值比較

操作符(單行)

= equal to

> greater than

>=greater than or equal to

<= less than or equal to

<>not equal to

select * from emp where job=(select job from emp where empno=7566) and sal >(select sal from emp where empno=7782);

select * from emp where sal=(select min(sal) from emp);

--查詢最低工資大于20號部門最低工資的部門號和部門的最低工資

select deptno,min(sal) from emp group by deptno having min(sal) > (select min(sal) from emp where deptno=20);

--查詢部門名稱是SALES和ACCOUNTING的員工信息

select * from emp where deptno in (select deptno from dept where dname=‘SALES‘ or dname=‘ACCOUNTING‘);

select e.* from emp e,dept d where e.deptno=d.deptno and (d.dname=‘SALES‘ OR d.dname=‘ACCOUNTING‘);

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);

子查詢對控制問題

--查詢不是老板的員工

select * from emp where empno not in (select mgr from emp where mgr is not null);

===================================綜合實例=============================

實例一

分頁查詢顯示員工信息:顯示員工號,姓名,月薪

-每頁顯示四條記錄

-顯示第二頁的員工

-按照月薪降序排列

select r,emp,ename,sal

from(select rownum r,empno,ename,sal

from(select rownum,empno,ename,sal from emp order by sal desc) e1 where rownum <=8) e2

where r>=5;

--oracle分頁通過子查詢實現

實例二

找到員工表中薪水大于本部門平均薪水的員工

select e.empno,e.name,e.sal,d.avgsal from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d where e.deptno=d.deptno and e.sal>d.avgsal;

實例三

按部門統計員工人數,按照如下格式輸出(員工的入職年份已知)

total 1980 1981 1982 1987

14  1  10  ? 1  ??2

select count(*) total,

sum(decode(to_char(hiredate,‘YYYY‘),‘1980‘,1,0)) "1980",

sum(decode(to_char(hiredate,‘YYYY‘),‘1981‘,1,0)) "1981",

sum(decode(to_char(hiredate,‘YYYY‘),‘1982‘,1,0)) "1982"

from emp;

--使用子查詢方式

select

(select count(*) from emp) total,

(select count(*) from emp where to_char(hiredate,‘yyyy‘)=‘1980‘) "1980",

(select count(*) from emp where to_char(hiredate,‘yyyy‘)=‘1981‘) "1981",

(select count(*) from emp where to_char(hiredate,‘yyyy‘)=‘1982‘) "1982"

from dual;

原文:http://www.cnblogs.com/gawain-ma/p/7240085.html

總結

以上是生活随笔為你收集整理的oracle高级查询案例,oracle高级查询(实例基于scott用户四张表)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。