第一章SQL
1.Oracle數(shù)據(jù)庫(kù)基礎(chǔ)
(1).開(kāi)始>運(yùn)行>輸入:sqlpusw/nolog>確定sqlplus程序
本地連接
-連接方式-conn[ect]yonghuming/口令[AS SYSDBA|AS SYSOPER]
使用Oracle SQLPlus工具進(jìn)行本地連接
[圖片上傳失敗...(image-e55399-1513146680888)]
(2)常用數(shù)據(jù)庫(kù)訪問(wèn)工具
Oracle SQLPlus
Oracle SQLPlus是與oracle進(jìn)行交互的客戶(hù)端工具,Oracle數(shù)據(jù)庫(kù)軟件安裝后就可使用牌借,在SQLPlus中,可以運(yùn)行sqlplus命令與sql語(yǔ)句。
常用SqlPlus命令:
show user;//顯示當(dāng)前進(jìn)行連接的用戶(hù)名;
Show parameter db_name;//顯示當(dāng)前操作的數(shù)據(jù)庫(kù)名;
Conn[ect] ;//連接數(shù)據(jù)庫(kù)
desc[ribe] 對(duì)象名;//查看指定對(duì)象的詳細(xì)信息
第二章.編寫(xiě)簡(jiǎn)單的查詢(xún)語(yǔ)句
基本SELECT語(yǔ)句語(yǔ)法
SELECT [DISTINCT]{|column|expression [alias],...}
FROM table;
(1)選擇列
SQL> SELECT deptno,dname,loc
FROM dept;
結(jié)果:DEPTNO DNAME LOC 地方
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
(2)算術(shù)運(yùn)算符
SQL> SELECT ename, sal, sal+300
FROM emp;
(3)算術(shù)運(yùn)算符先級(jí)
SQL> SELECT ename, sal, 12sal+100
FROM emp;
結(jié)果:ENAME SAL 12*SAL+100
KING 5000 60100
BLAKE 2850 34300
CLARK 2450 29500
JONES 2975 35800
MARTIN 1250 15100
ALLEN 1600 19300
rows selected.
(4)空值NULL
SQL> SELECT ename, job, sal, comm
FROM emp;
結(jié)果:ENAME JOB SAL COMM
KING PRESIDENT 5000
BLAKE MANAGER 2850
TURNER SALESMAN 1500 0
rows selected.
(5)列別名
SQL> SELECT ename AS name, sal salary
FROM emp;
SQL> SELECT ename "Name",
sal*12 "Annual Salary"
FROM emp;
(6)連接操作符
SQL> SELECT ename||job AS "Employees"
FROM emp;
結(jié)果:Employees
KINGPRESIDENT
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER
MARTINSALESMAN
ALLENSALESMAN
...
rows selected.
(7)原義字符串
SQL> SELECT ename ||' is a '||job
AS "Employee Details"
FROM emp;
結(jié)果:Employee Details
KING is a PRESIDENT
BLAKE is a MANAGER
CLARK is a MANAGER
JONES is a MANAGER
MARTIN is a SALESMAN
(8)消除重復(fù)行
SQL> SELECT deptno
FROM emp;
結(jié)果: DEPTNO
10
30
10
20
rows selected.
(9)顯示表的結(jié)構(gòu)
在SQL*Plus中,可以使用DESCRIBE 命令來(lái)查看表結(jié)構(gòu)
DESC[RIBE] tablename
第三章.限制數(shù)據(jù)和對(duì)數(shù)據(jù)排序
(1)選擇限定數(shù)據(jù)行
SELECT [DISTINCT] {*| column | expression [alias], ...}
FROM table
[WHERE condition(s)]; {where子句緊跟在from字句值后}
通常格式為:列名 比較操作符 要比較的值
(2)選擇限定數(shù)據(jù)行
操作符 含義
= 等于
> 大于
= 大于或等于
< 小于
<= 小于或等于
<> 不等
(3) 使用WHERE子句
比較數(shù)值型數(shù)據(jù)
SQL> SELECT ename, job, deptno
FROM emp
WHERE deptno=20;
結(jié)果:ENAME JOB DEPTNO
JAMES CLERK 20
SMITH MANAGER 20
ADAMS ANALYST 20
MILLER CLERK 20
(4) 比較字符型數(shù)據(jù)
SQL> SELECT ename, job, deptno
FROM emp
WHERE job='CLERK';
結(jié)果:ENAME JOB DEPTNO
JAMES CLERK 30
SMITH CLERK 20
ADAMS CLERK 20
MILLER CLERK 10
(5) 比較日期型數(shù)據(jù)
SQL> SELECT ename, job, deptno
FROM emp
WHERE hiredate > '01-1月-85';
結(jié)果:ENAME HIREDATE DEPTNO
SCOTT 1987-4-19 20
ADAMS 1987-5-23 20
特殊比較運(yùn)算符
SQL> SELECT ename, sal
FROM emp
WHERE sal BETWEEN 1000 AND 1500;
結(jié)果:ENAME SAL
MARTIN 1250
TURNER 1500
WARD 1250
ADAMS 1100
MILLER 1300
(6) IN運(yùn)算符
SQL> SELECT empno, ename, sal, mgr
FROM emp
WHERE mgr IN (7902, 7566, 7788);
結(jié)果: EMPNO ENAME SAL MGR
--------- ---------- --------- ---------
7902 FORD 3000 7566
7369 SMITH 800 7902
7788 SCOTT 3000 7566
7876 ADAMS 1100 7788
(7) LIKE運(yùn)算符
SQL> SELECT ename
FROM emp
WHERE ename LIKE 'S%';{%代表零和任何字符}
?與百分號(hào)組合使用
SELECT ename
FROM emp
WHERE ename LIKE 'L%';
屈留??可以使用ESCAPE標(biāo)識(shí)符實(shí)現(xiàn)對(duì)“%”和 “”的查找
SELECT ename,job
FROM emp
WHERE job LIKE 'MAN@_%' ESCAPE '@';
(8) IS NULL 運(yùn)算符
SELECT ename, mgr
FROM emp
WHERE mgr IS NULL;
結(jié)果:ENAME MGR
KING NULL(空值)
(8)邏輯與(AND)
SELECT empno, ename, job, sal
FROM emp
WHERE sal>=1100
AND job='CLERK';
結(jié)果:EMPNO ENAME JOB SAL
7876 ADAMS CLERK 1100
7934 MILLER CLERK 1300
(9)邏輯或(OR):
SELECT empno, ename, job, sal
FROM emp
WHERE sal>=1100
OR job='CLERK';
結(jié)果:EMPNO ENAME JOB SAL
7839 KING PRESIDENT 5000
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7566 JONES MANAGER 2975
7654 MARTIN SALESMAN 1250
7900 JAMES CLERK 950
(10)SELECT ename, job
FROM emp
WHERE job NOT IN ('CLERK','MANAGER','ANALYST');
結(jié)果:ENAME JOB
KING PRESIDENT
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
WARD SALESMAN
(11)運(yùn)算符的優(yōu)先級(jí)
SELECT ename, job, sal
FROM emp
WHERE job='SALESMAN'
OR job='PRESIDENT'
AND sal>1500;
結(jié)果:ENAME JOB SAL
KING PRESIDENT 5000
MARTIN SALESMAN 1250
ALLEN SALESMAN 1600
TURNER SALESMAN 1500
WARD SALESMAN 1250
(12)運(yùn)算符的優(yōu)先級(jí),使用括號(hào)強(qiáng)制改變優(yōu)先權(quán)
SELECT ename, job, sal
FROM emp
WHERE (job='SALESMAN'
OR job='PRESIDENT')
AND sal>1500;
結(jié)果:ENAME JOB SAL
KING PRESIDENT 5000
ALLEN SALESMAN 1600
(13)ORDER BY子句
SELECT ename, job, deptno, hiredate
FROM emp
ORDER BY hiredate;
結(jié)果:ENAME JOB DEPTNO HIREDATE
SMITH CLERK 20 17-DEC-80
ALLEN SALESMAN 30 20-FEB-81
强法??按列名降序排序
SELECT ename, job, deptno, hiredate
FROM emp
ORDER BY hiredate DESC;
結(jié)果:ENAME JOB DEPTNO HIREDATE
ADAMS CLERK 20 12-JAN-83
SCOTT ANALYST 20 09-DEC-82
MILLER CLERK 10 23-JAN-82
JAMES CLERK 30 03-DEC-81
FORD ANALYST 20 03-DEC-81
KING PRESIDENT 10 17-NOV-81
MARTIN SALESMAN 30 28-SEP-81
湾笛?饮怯??按列別名排序
SELECT empno, ename, sal*12 annsal
FROM emp
ORDER BY annsal;
結(jié)果: EMPNO ENAME ANNSAL
7369 SMITH 9600
7900 JAMES 11400
7876 ADAMS 13200
7654 MARTIN 15000
7521 WARD 15000
7934 MILLER 15600
7844 TURNER 18000
嚎研?蓖墅??临扮?多列參與排序
SELECT ename, deptno, sal
FROM emp
ORDER BY deptno, sal DESC;
結(jié)果: ENAME DEPTNO SAL
KING 10 5000
CLARK 10 2450
MILLER 10 1300
FORD 20 3000
论矾??杆勇?贪壳??按結(jié)果集列序號(hào)排序
SELECT ename, deptno, sal
FROM emp
ORDER BY 2, 3 DESC;
第四章.單行函數(shù)
(1)單行函數(shù)語(yǔ)法
~變量
~列名
~表達(dá)式
(1)大小寫(xiě)轉(zhuǎn)換函數(shù)
函數(shù):LOWER('SQL Course')
UPPER('SQL Course')
INITCAP('SQL Course')【列明|表達(dá)式】
結(jié)果:sql course
SQL COURSE
Sql Course
(2)大小寫(xiě)轉(zhuǎn)換函數(shù)
在顯示Blake的雇員編號(hào)蚜退、姓名和部門(mén)編號(hào)闰靴。
第一種:SELECT empno, ename, deptno
FROM emp
WHERE ename = 'blake';
第二種:SELECT empno, ename, deptno
FROM emp
WHERE ename = UPPER('blake');
結(jié)果:EMPNO ENAME DEPTNO
7698 BLAKE 30
(3)字符處理函數(shù)
第一種:CONCAT(column1|expression1,column2|expression2)
連接兩個(gè)值 ,等同于||
SUBSTR (column|expression,n1[,n2])
返回第一個(gè)參數(shù)中,從第n1位開(kāi)始关霸,長(zhǎng)度為n2的子串传黄。
如果n2省略,取第n1位開(kāi)始的所有字符。
如果n1是負(fù)值队寇,表示從第一個(gè)參數(shù)的后面第abs(n1)位開(kāi)始向右取長(zhǎng)度為n2的子串膘掰。
LENGTH(column | expression)
########取字符長(zhǎng)度?
第二種:
INSTR(s1,s2,[,n1],[n2])
返回s1中,子串s2從n1開(kāi)始佳遣,第n2次出現(xiàn)的位置识埋。n1,n2默認(rèn)值為1
LPAD(s1,n1,s2)
返回s1被s2從左面填充到n1長(zhǎng)度后的字符串。?
RPAD(s1,n1,s2)
返回s1被s2從右面填充到n1長(zhǎng)度后的字符串零渐。?
TRIM:去除字符串頭部或尾部(頭尾)的字符?格式:TRIM(leading | trailing | both trim_character From trim_source)
REPLACE(s1,s2,s3)
把s1中的s2用s3替換窒舟。?
第三種查詢(xún)
SELECT ename, CONCAT (ename, job), LENGTH(ename),
INSTR(ename, 'A')
FROM emp
WHERE SUBSTR(job,1,5) = 'SALES';
結(jié)果: ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A')
-------------------
MARTIN MARTINSALESMAN 6 2
ALLEN ALLENSALESMAN 5 1
TURNER TURNERSALESMAN 6 0
WARD WARDSALESMAN 4 2
(4) SELECT ROUND(45.923,2),
ROUND(45.923,0),
ROUND(45.923,-1)
FROM DUAL;
結(jié)果:ROUND(45.923,2) ROUND(45.923,0) ROUND(45.923,-1)
--------------
45.92 46 50
(5) TRUNC函數(shù)
SELECT TRUNC(45.923,2), TRUNC(45.923),
TRUNC(45.923,-1)
FROM DUAL;
結(jié)果:TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1)
-------------
45.92 45 40
(6) MOD函數(shù)
SELECT ROUND(45.923,2), ROUND(45.923,0),
ROUND(45.923,-1)
FROM DUAL;
結(jié)果:ROUND(45.923,2) ROUND(45.923,0) ROUND(45.923,-1)
--------------
45.92 46 50
(6)TRUNC函數(shù)
SELECT TRUNC(45.923,2), TRUNC(45.923),
TRUNC(45.923,-1)
FROM DUAL;
結(jié)果:
TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1)
-------------
45.92 45 40
(7)計(jì)算工作為SALESMAN的雇員的月薪和獎(jiǎng)金相除后的余數(shù)。
SELECT ename, sal, comm, MOD(sal, comm)
FROM emp
WHERE job = 'SALESMAN';
結(jié)果:ENAME SAL COMM MOD(SAL,COMM)
MARTIN 1250 1400 1250
ALLEN 1600 300 100
TURNER 1500 0 1500
WARD 1250 500 250
(8)日期的運(yùn)算
SELECT ename, (SYSDATE-hiredate)/7 WEEKS
FROM emp
WHERE deptno = 10;
結(jié)果:ENAME WEEKS
---------- ---------
KING 830.93709
CLARK 853.93709
MILLER 821.36566
(9)常用日期函數(shù)
SELECT ename, sal, MONTHS_BETWEEN(SYSDATE,hiredate) months
FROM emp
ORDER BY months
诵盼?ADD_MONTHS 函數(shù)演示——查詢(xún)82年后入職的員工轉(zhuǎn)正日期惠豺,按照3個(gè)月試用期考慮
SELECT ename, sal, hiredate, ADD_MONTHS(hiredate,3) new_date
FROM emp
WHERE hiredate>'01-1月-82';
银还??1~NEXT_DAY 函數(shù)演示——返回在02-2月-06之后的下一個(gè)周一是什么日期洁墙。
SELECT NEXT_DAY('02-2月-06','星期一') NEXT_DAY
FROM DUAL;
2~LAST_DAY 函數(shù)演示——返回06年2月2日所在月份的最后一天蛹疯。
SELECT LAST_DAY('02-2月-06') "LAST DAY"
FROM DUAL;
3~ROUND函數(shù)演示——查詢(xún)81年入職的員工姓名,入職日期按月四舍五入的日期热监。
SELECT empno, hiredate,
ROUND(hiredate, 'MONTH')
FROM emp
WHERE SUBSTR(hiredate,-2,2)=‘81';
4~TRUNC 函數(shù)演示——查詢(xún)81年入職的員工姓名捺弦,入職日期按月截?cái)嗟娜掌凇?br>
SELECT empno, hiredate, TRUNC(hiredate, 'MONTH')
FROM emp
WHERE SUBSTR(hiredate,-2,2)=‘81';
(10)常用日期函數(shù)-EXTRACT 函數(shù)語(yǔ)法
EXTRACT ([YEAR] [MONTH][DAY]
FROM [日期類(lèi)型表達(dá)式])
1~.部門(mén)編號(hào)是10的部門(mén)中所有員工入職月份。
SELECT ename, hiredate,
EXTRACT (MONTH FROM HIREDATE) MONTH
FROM emp
WHERE deptno= 10;
7.轉(zhuǎn)換函數(shù)
通常是在字符類(lèi)型孝扛、日期類(lèi)型列吼、數(shù)值類(lèi)型之間進(jìn)行顯式轉(zhuǎn)換。主要有3個(gè)顯式轉(zhuǎn)換函數(shù):
TO_CHAR
TO_NUMBER
TO_DATE
(1)TO_CHAR 用于日期型
SELECT ename,
TO_CHAR(hiredate, 'DD Month YYYY') HIREDATE
FROM emp;
結(jié)果:ENAME HIREDATE
---------- -----------------
KING 17 November 1981
BLAKE 1 May 1981
CLARK 9 June 1981
JONES 2 April 1981
MARTIN 28 September 1981
ALLEN 20 February 1981
第五章 多表連接
(1)什么是連接
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
(2)笛卡爾積的寫(xiě)法
SELECT emp.empno, emp.ename, emp.deptno,? dept.deptno, dept.loc
FROM emp, dept;
(3)用等值連接檢索數(shù)據(jù)
SELECT emp.empno, emp.ename, emp.deptno,? dept.deptno, dept.loc
FROM emp, dept
WHERE emp.deptno=dept.deptno;
結(jié)果:EMPNO ENAME DEPTNO DEPTNO LOC
----- ------ ------ ------ ---------
7839 KING 10 10 NEW YORK
7698 BLAKE 30 30 CHICAGO
7782 CLARK 10 10 NEW YORK
7566 JONES 20 20 DALLAS
(4)使用AND運(yùn)算符增加其它查詢(xún)條件
SELECT emp.empno, emp.ename, emp.deptno,?
dept.deptno, dept.loc
FROM emp, dept
WHERE emp.deptno=dept.deptno and loc= ‘NEW YORK’;
結(jié)果:EMPNO ENAME DEPTNO DEPTNO LOC
7839 KING 10 10 NEW YORK
7782 CLARK 10 10 NEW YORK
(5)通過(guò)使用表的別名來(lái)簡(jiǎn)化查詢(xún)語(yǔ)句
SELECT emp.empno, emp.ename, emp.deptno,
dept.deptno, dept.loc
FROM emp, dept
WHERE emp.deptno=dept.deptno;
(6)查詢(xún)每個(gè)顧客都訂購(gòu)了哪些商品
SELECT c.name,o.itemid
FROM customer c, order o,item i
WHERE c.custid = o.custid and o.ordid = i.ordid;
*[多表連接給每個(gè)表自己定義個(gè)語(yǔ)句來(lái)查找]
(7)非等值連接的數(shù)據(jù)檢索
查詢(xún)每個(gè)員工的姓名苦始,工資寞钥,工資等級(jí)
SELECT e.ename, e.sal, s.grade
FROM emp e, salgrade s
WHERE e.sal
BETWEEN s.losal AND s.hisal;
結(jié)果:ENAME SAL GRADE
JAMES 950 1
SMITH 800 1
SMITH 1100 1
(8)外部連接
查詢(xún)所有雇員姓名,部門(mén)編號(hào)盈简,部門(mén)名稱(chēng)凑耻,包括沒(méi)有員工的部門(mén)也要顯示出來(lái)
SELECT e.ename, d.deptno, d.dname
FROM emp e, dept d
WHERE e.deptno(+) = d.deptno
ORDER BY e.deptno;
結(jié)果:ENAME DEPTNO DNAME
---------- --------- -------------
KING 10 ACCOUNTING
CLARK 10 ACCOUNTING
(9)自身連接
SELECT worker.ename||' leader is '||manager.ename
FROM emp worker, emp manager
WHERE worker.mgr = manager.empno;
兩種解決方法:WORKER.ENAME||'leader is'||MANAG
-------------------------------
BLAKE leader is KING
CLARK leader is KING
JONES leader is KING
(10)交叉連接
交叉連接會(huì)產(chǎn)生連個(gè)表的交叉乘積,和兩個(gè)表之間的笛卡爾積是一樣的柠贤;
使用CROSS JOIN 子句完成香浩。
SELECT emp.empno,emp.ename,emp.sal,emp.deptno,dept.loc
FROM emp
CROSS JOIN dept;
(10)自然連接
SELECT empno,ename,sal,deptno,loc
FROM emp
NATURAL JOIN dept;
(11)USING子句
SELECT e.ename,e.ename,e.sal,deptno,d.loc
FROM emp e JOIN dept d USING (deptno)
WHERE deptno = 20 ;
(12)ON子句
SELECT e.empno, e.ename, e.deptno, d.deptno, d.loc
FROM emp e
JOIN dept d
ON (e.deptno = d.deptno);
(13)左外連接
左外連接以FROM子句中的左邊表為基表,該表所有行數(shù)據(jù)按照連接條件無(wú)論是否與右邊表能匹配上臼勉,都會(huì)被顯示出來(lái)邻吭。
SELECT e.ename,e.deptno,d.loc
FROM emp e
LEFT OUTER JOIN dept d
ON (e.deptno = d.deptno);
(14)右外連接 右外連接以FROM子句中的右邊表為基表,該表所有行數(shù)據(jù)按照連接條件無(wú)論是否與左邊表能匹配上宴霸,都會(huì)被顯示出來(lái)囱晴。
SELECT e.ename,e.deptno,d.loc
FROM emp e
RIGHT OUTER JOIN dept d
ON (e.deptno = d.deptno);
(15)全外鏈接
全外連接返回兩個(gè)表等值連接結(jié)果,以及兩個(gè)表中所有等值連接失敗的記錄
SELECT e.ename,e.deptno,d.loc
FROM emp e
FULL OUTER JOIN dept d
ON (e.deptno = d.deptno);
6.多組函數(shù)
(1)分組函數(shù)
有五種函數(shù)(MIN,MAX,SUM,AVG,COUNT)
(2)MIN函數(shù)和MAX函數(shù)
MIN和MAX函數(shù)主要是返回每組的最小值和最大值
查詢(xún)?nèi)肼毴掌谧钤绾妥钔淼娜掌?br>
SELECT MIN(hiredate), MAX(hiredate)
FROM emp;
(3)SUM函數(shù)和AVG函數(shù)
SUM和AVG函數(shù)分別返回每組的總和及平均值查詢(xún)職位以SALES開(kāi)頭的所有員工平均工資瓢谢、最低工資畸写、最高工資、工資和
SELECT AVG(sal), MAX(sal),
MIN(sal), SUM(sal)
FROM emp
WHERE job LIKE 'SALES%';
(4)COUNT函數(shù)
COUNT():返回表中滿足條件的行記錄數(shù)
查詢(xún)部門(mén)30有多少個(gè)員工
SELECT COUNT()
FROM emp
WHERE deptno = 30;
(5)組函數(shù)中DISTINCT
查詢(xún)有員工的部門(mén)數(shù)量
SELECT COUNT(DISTINCT deptno)
FROM emp;
(6)分組函數(shù)中空值處理
除了COUNT(*)之外
SELECT AVG(comm)
FROM EMP
(7)在分組函數(shù)中使用NVL函數(shù)
SELECT AVG(NVL(comm,0))
FROM emp;
(8)用GROUP BY子句創(chuàng)建數(shù)據(jù)組
SELECT column, group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];
舉例:查詢(xún)每個(gè)部門(mén)的編號(hào)氓扛,平均工資
SELECT deptno, AVG(sal)
FROM emp
GROUP BY deptno;
枯芬?GROUP BY 所指定的列并不是必須出現(xiàn)在SELECT
SELECT AVG(sal)
FROM emp
GROUP BY deptno;
??按多列分組的GROUP BY子句
查詢(xún)每個(gè)部門(mén)每個(gè)崗位的工資總和
SELECT deptno, job, sum(sal)
FROM emp
GROUP BY deptno, job;
結(jié)果:DEPTNO JOB SUM(SAL)
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 ANALYST 6000
20 CLERK 1900
(9)使用組函數(shù)的非法的查詢(xún)
不能在 WHERE子句中限制組,可以通過(guò) HAVING 子句限制組
SELECT deptno, max(sal)
FROM emp
WHERE max(sal) > 2900
GROUP BY deptno;
(10)用 HAVING Clause子句排除組結(jié)果
使用 HAVING 子句限制組與 HAVING 子句匹配的結(jié)果才輸出
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
列子:查詢(xún)每個(gè)部門(mén)最高工資大于2900的部門(mén)編號(hào),最高工資
SELECT deptno, max(sal)
FROM emp
GROUP BY deptno
HAVING max(sal)>2900;
(11)使用HAVING子句
SELECT job, SUM(sal) PAYROLL
FROM emp
WHERE job NOT LIKE 'SALES%'
GROUP BY job
ORDER BY SUM(sal);
結(jié)果:JOB PAYROLL
--------- ---------
ANALYST 6000
MANAGER 8275
(12)SELECT語(yǔ)句的執(zhí)行過(guò)程
ELECT deptno,job,avg(sal)
FROM emp
WHERE job in ('SALESMAN','MANAGER','CLERK')
GROUP BY deptno,job
HAVING avg(sal)>1000
ORDER BY 3 DESC;
結(jié)果:DEPTNO JOB AVG(SAL)
20 MANAGER 2975
30 MANAGER 2850
10 MANAGER 2450
30 SALESMAN 1400
10 CLERK 1300
(13)組函數(shù)和多表連接
查詢(xún)每個(gè)部門(mén)的部門(mén)編號(hào)采郎、部門(mén)名稱(chēng)千所、部門(mén)人數(shù)
SELECT e.deptno,d.dname,count(empno)
FROM emp e,dept d
WHERE e.deptno = d.deptno
GROUP BY e.deptno;
改正錯(cuò)誤
SELECT e.deptno,d.dname,count(empno)
FROM emp e,dept d
WHERE e.deptno = d.deptno
GROUP BY e.deptno,d.dname
(14)顯示平均薪水的最大值
SELECT max(avg(sal))
FROM emp
GROUP BY deptno;
結(jié)果:MAX(AVG(SAL))
-------------
2916.6667