以本表為例(emp)
(dept)
一、知識點
1.基礎查詢select
語法:select 字段列表 from 表名;
2.#取別名(使用空格,as)
select EMPNO 員工編號,job, sal, sal*12 as 年薪 from emp;
3.#去重復(distinct)
select distinct job from emp;
4.#查詢可以用表達式
select sal*12 from emp;
5.#排序order by asc |desc
#按工資降序排序顯示所有的員工信息
select * from emp ORDER BY sal desc;
6.#指定范圍查詢 BETWEEN and 和not BETWEEN and
7.#不等於查詢(> < >= <= != !> !< <>)
8.like模糊查詢
% 表示0個或多個字符
_表示1個字符
9.in 查詢在某個集合中 not in
10.is null 為空 is not null
11.not 取反
12.多條件查詢 and且 or 或
13.函數:
nvl(x,values) 處理是否為空,為空返回values值
length()長度函數
聚合函數:count() 求列表個數
Max() 求最大值
Min() 求最小值
sum() 求和
avg()求平均值
特點:不能放到where條件中使用
14.分組,在遇到每個的題目時用
select DEPTNO #分組名稱,跟分組項相同
from emp
GROUP BY DEPTNO #分組項
例:#查詢每個部門有多少人
select * from emp;
select DEPTNO,count(empno)
from emp
GROUP BY DEPTNO
15.having進行二次查詢
16.多表連接
(1.)等值連接
語法:select * from 表1 join 表2 on 表1.列名=表2.列名
例:select a.*,b.* from a,b where a.sno=b.sno and a.score>60
select a.*,b.* from a join b on a.sno=b.sno where a.score>60
(2.)左連接右連接(屬於外連接)
左連接語法:select * from 表1 left join 表2 on 表1.列名=表2.列名
右連接語法:select * from 表1 rigth join 表2 on 表1.列名=表2.列名
例:左連接:select a.*,b.* from a left join b on a.sno=b.sno where a.score>60
右連接:select a.*,b.* from a lright join b on a.sno=b.sno where a.score>60
(3.)不等值連接
例:select * from emp e join SALGRADE s on e.sal BETWEEN s.LOSAL and s.HISA
17.子查詢
子查詢是一個數據,在where子句中直接可使用=(子查詢)即可
子查詢是多個數據,在where子句中需要使用 列in(子查詢)
18.刪除部分數據語法
delete from 表 where 篩選條件
19.刪除全部數據語法
delete from 表名稱
20.查詢時使用ifnull如果字段值為空則用0來表示,如果非空則為原來的數據
select sno if null(score,0)from 表格
二、例題
1.#查詢所有的表格信息
select * from emp
2.#選擇部門編號是30的員工
select * from emp where DEPTNO=30
3.#列出所有辦事員(CLERK)的姓名,編號和部門編號
select ENAME,MGR,EMPNO from emp where JOB='CLERK'
4.#找出獎金高於薪金的員工
select * from emp where comm>sal
5.#找出部門10中所有經理(MANAGER)和部門20中所有辦事員(CLERK)的詳細資料
select * from emp where DEPTNO=10 and job='MANAGER' or DEPTNO=20 and job='CLERK'
6.#找出部門10中所有經理(MANAGER),部門20中所有辦事員(CLERK),以及既不是經理又不是辦事員但其薪金大於或等於2000的所有員工和詳細資料
select * from emp where DEPTNO=10 and job='MANAGER' or DEPTNO=20 and job='CLERK' or job!='MANAGER' and job!='CLERK' and sal>=2000
7.#找出有獎金的員工的不同工作
select job from emp where comm is not null
8.#找出沒有獎金或獎金低於100的員工
select * from emp where comm is null or comm<100
9.#找出早於87年前入職的員工
select * from emp where HIREDATE<to_date('1987-01-01','yyyy-mm-dd');
10.#顯示正好為5個字符的員工姓名
select ENAME from emp where length(ENAME)=5
11.#顯示不帶“R”的員工姓名
select ENAME from emp where ENAME not like '%R%'
12.#顯示所有員工的姓名,加入公司的時間,按入職時間進行排序
select ENAME,HIREDATE from emp order by HIREDATE asc
13.#顯示所有員工的姓名、入職日期、工作和工資,按工資的降序排序,若工資相同則按入職日期排序
select ENAME,HIREDATE,job,sal from emp order by sal desc
14.#顯示姓名字段的任何位置包含“A”的所有員工的姓名
select ENAME from emp where ENAME like '%R%'
15.#列出所有員工的年收入,按年薪從低到高排序
select ENAME,SAL*12 from emp order by sal*12 asc
16.#顯示年薪等於獎金加年薪
select ENAME,sal,sal*12+nvl(comm,0) 年薪 from emp;
17.#函數length
select *from emp where length(ENAME)=5
18.#count 函數
#查詢有多少領導
select count(mgr) from emp
#查詢有多少員工
select count(ENAME) from emp
#查詢有多少部門(去重復)
select count(distinct DEPTNO) from emp
19.#查詢30部門什么時候創建的
select min(hiredate) from emp where deptno=30
20#查詢最晚的入職時間
select max(hiredate) from emp
21.#查詢員工工資總和和平均值
select avg(sal) 平均 from emp;
22.#查詢部門人數
select deptno,count(deptno)
from emp
group by deptno
23.查詢每個工作的最高工資和最低工資
select job,max(sal),min(sal)
from emp
group by job
24..查詢每個部門成立日期(最早入職)
select deptno,min(HIREDATE)
from emp
group by deptno
25..查詢每個部門需要發多少工資
select deptno,sum(sal)
from emp
group by deptno
26.查詢每個工作的平均工資和平均獎金
select job,avg(sal),avg(comm)
from emp
group by job
27.查詢每個領導都帶了幾個小弟
select mgr,count(mgr)
from emp
group by mgr
28..查詢每一個部門姓名帶a的員工數量,只顯示小於三個員工的部門
select deptno,min(deptno)
from emp where ENAME like '%A%'
group by deptno
having count(deptno)<3
29..查詢1981年入職的員工數量
select count(HIREDATE) from emp where HIREDATE between to_date('1981-01-01','yyyy-mm-dd') and to_date('1981-12-30','yyyy-mm-dd')
.30.列出至少有一個員工的所有部門
select distinct(deptno) from emp where deptno>=1
31.列出薪金比“SMITH”多的所有員工
select * from emp e where e.sal>(select sal from emp where ENAME='SMITH')
32.列出所有員工的姓名及其直接上級的姓名
select e.ENAME,em.ENAME
from emp e,emp em
where e.empno=em.mgr
多表查詢
等值連接
select * from 表1 join 表2 on 表1.列名=表2.列名
內連接
select* from emp e,dept d where e .DEPTNO=d.DEPTNO
左連接 select * from 表1 left join 表2 on 表1.列名=表2.列名
查詢入職最早的那個員工全部信息
select * from emp e where e.HIREDATE=(select min(HIREDATE) from emp);
select * from emp e where e.sal>(select avg(sal) from emp)
#等值查詢
select * from emp e,dept d where e.DEPTNO=d.DEPTNO
#右連接
select * from dept d rigth join emp e on d.DEPTNO=e.DEPTNO
#不等值查詢
select * from emp e join SALGRADE s on e.sal BETWEEN s.LOSAL and s.HISAL
修改列名
增加列名:alter table 表名 add 新列名 varchar(255)
刪除列名:alter table 表名 drop column 列名 varchar(255)
修改列名:alter table 表名 change 列名 新列名 varchar(255)