oracle的簡單查詢
v 使用比較運算子
=、>、<、>=、<=、!=、<> 、any(任一)、all(全部)
1.查詢emp表中薪水大於1250小於2500的員工姓名和薪水
select ename,sal from emp where sal > 1250 and sal < 2500;
2. 查詢emp1表中職位為CLERK或者SALESMAN中任意一個的姓名和職位
select ename,job from emp1 where job = any('CLERK','SALESMAN');
v 使用邏輯運算子查詢:
and、or、not;between…and、
1. 查詢出工資在2000--3000之間的員工資訊
SQL> select * from emp where sal between 2000 and 3000;//包含2000和3000
2查詢出10號部門中的所有經理,和20號部門中不是經理但是工資高於2000的員工資訊
SQL> select * from emp where (deptno=10 and job='MANAGER') or
(deptno=20 and job!='MANAGER' and sal>2000);
v 模糊查詢 like
_: 代表一個任意字元
%:代表0到多個任意字元
1. 查找出姓名第二個字母是L的員工資訊
SQL> select * from emp where ename like '_L%';
v 排序 order by [asc/desc]
asc: 從小到大(預設為asc)
desc:從大到小
1 查詢出emp表中員工資訊,要求按照工資從低到高排序
SQL> select * from emp order by sal asc;//asc可以省略
2(多列排序)查找出員工資訊,按照部門從小到大排序,在部門內按照工資從大到小排序
SQL> select * from emp order by deptno asc,sal desc;
v 使用聚合函式/分組函式 max,min,avg,sum,count;
1 查詢出emp表中最高工資
SQL> select max(sal) from emp;
v 使用group by 和 having
1. 查詢出每個部門的最高,最低,平均工資
SQL> select deptno,max(sal),min(sal),avg(sal) from emp group by deptno;
2. 查詢出不同工作崗位的平均工資,只顯示平均工資高於1500的資訊,並且平均工資按照從低到高顯示
SQL> select job,avg(sal) from emp group by job having avg(sal)>1500 order by avg(sal);
v 多表查詢
1. 從emp和dept表中查詢出每個員工姓名,工作,部門名,部門所在地
SQL> select e.ename,e.job,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;
v 自連線:自連線是指在同一張表的連線查詢。
1. 查詢出emp表中所有員工的上級
select e1.empno,e1.ename, e1.mgr,e2.ename "上級姓名" from emp1 e1,emp1 e2 where e1.mgr = e2.empno;
v 子查詢
1查詢smith的上級資訊
select ename from emp1 where empno = (select mgr from emp1 where ename = 'SMITH');
1. (all)查詢出比20號部門所有員工工資都要高的員工資訊
方法一:使用all,all代表所有
SQL>select * from emp where sal>all(select sal from emp where deptno=20);
方法二:使用max函式
SQL>select * from emp where sal>(select max(sal) from emp where deptno=20);
2.查詢出與SMITH同一個部門,且工作崗位相同的員工資訊,不包含SMITH
//使用普通子查詢
SQL> select * from emp where (job=(select job from emp where ename='SMITH') and deptno=(select deptno from emp where ename='SMITH')) and ename!='SMITH';
//使用多列子查詢
SQL> select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH') and ename!='SMITH';
v from子查詢
1. 查詢出高於自己部門平均工資的員工姓名,工資,部門編號,部門平均工資
select e.ename,e.sal,a.deptno,a.avg_sal from emp e,(select deptno,avg(sal) avg_sal from emp group by deptno) a where e.deptno=a.deptno and e.sal>a.avg_sal;
v 外連線查詢
a. 左外連線 left outer join/left join
左邊的表不加限制
1. 所有員工及對應部門的記錄,包括沒有對應部門編號depno的員工記錄
select e.ename,e.deptno,d.dname from emp1 e left join dept1 d on e.deptno = d.deptno;
或者:
select e.ename,e.deptno,d.dname from emp1 e, dept1 d where e.deptno = d.deptno(+);
b. 右外連線
右邊的表不加限制
select * from emp e right join dept d on e.deptno = d.deptno;
或者:select e.*,d.* from emp e,dept d where e.deptno(+) = d.deptno;
c. 全外連線
左右兩表都不加限制
select e.ename,e.deptno "emp部門編號", d.deptno "dept部門編號", d.dname from emp1 e ful
l outer join dept1 d on e.deptno = d.deptno;
注意:用(+)來實現, 這個+號可以這樣來理解: + 表示補充,即哪個表有加號,這個表就是匹配表。
v 合併查詢
union,union all,intersect,minus
並 並 交 差
1. 查詢出,工資>=2500 或者工作為MANAGER的員工資訊
( select * from emp where sal>=2500) union (select * from emp where job='MANAGER');
v Oracle分頁查詢
1.查詢出emp表中前5名員工資訊
select emp.*,rownum from emp where rownum<=5;
2.查詢出emp表中工資排在第3—5的員工資訊,不考慮獎金
select * from (select e.*,rownum rn from (select * from emp order by sal desc) e where rownum<=5) where rn>=3;