Oracle进阶(五)SQL进阶语法

1、常用SQL语法

点击链接了解SQL基础语法

1.1 进阶查询

--1 ANY 
--查询工资比部门编号为30的任意员工工资高的员工
SELECT ENAME, SAL, DEPTNO FROM EMP WHERE SAL> ANY (SELECT SAL FROM EMP WHERE DEPTNO='30');
-- 等价
SELECT ENAME, SAL, DEPTNO FROM EMP WHERE SAL > (SELECT MIN(SAL) FROM EMP WHERE DEPTNO='30');

--2 查询与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');

--3 查找每个部门工资最高的员工
SELECT E.ENAME, E.SAL, T.DEPTNO FROM EMP E, (SELECT MAX(SAL) MAXSAL, DEPTNO FROM EMP GROUP BY DEPTNO) T WHERE E.SAL >=T.MAXSAL AND E.DEPTNO=T.DEPTNO;
-- 等价
SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO);

--4 根据查询结果创建新表
CREATE TABLE TEMP (EMP_ID, EMP_NAME) AS SELECT EMPNO,ENAME FROM EMP;

--5 自我复制数据
INSERT INTO TEMP (EMP_ID, EMP_NAME) SELECT EMP_ID,EMP_NAME FROM TEMP;

--6 UNION并集

-- 查询工资大于2500或工作等于MANAGER的员工
SELECT ENAME, SAL, JOB FROM EMP WHERE SAL>2500
UNION
SELECT ENAME, SAL, JOB FROM EMP WHERE JOB='MANAGER';
-- 等价
SELECT ENAME, SAL, JOB FROM EMP WHERE SAL>2500 OR JOB='MANAGER';

--7 INTERSECT交集
-- 查询工资大于2500且工作等于MANAGER的员工
SELECT ENAME, SAL, JOB FROM EMP WHERE SAL>2500
INTERSECT
SELECT ENAME, SAL, JOB FROM EMP WHERE JOB='MANAGER';
-- 等价
SELECT ENAME, SAL, JOB FROM EMP WHERE SAL>2500 AND JOB='MANAGER';

--8 MINUS差集
-- 查询工资大于2500且工作不等于MANAGER的员工
SELECT ENAME, SAL, JOB FROM EMP WHERE SAL>2500
MINUS
SELECT ENAME, SAL, JOB FROM EMP WHERE JOB='MANAGER';
--等价
SELECT ENAME, SAL, JOB FROM EMP WHERE SAL>2500 AND JOB<>'MANAGER';


--*************9 层次查询*****************************

 --9.1、START WITH ...... CONNECT BY
 --注:START WITH: 指定起始节点条件
    --CONNECT BY: 指定父子行的条件关系
		
--找到职位PRESIDENT的下级以及下级的下级
 SELECT EMPNO, ENAME, JOB, MGR, DEPTNO, LEVEL FROM EMP 
 START WITH JOB='PRESIDENT' 
 CONNECT BY PRIOR EMPNO= MGR;
 SELECT EMPNO, CAST(LPAD(' ', LEVEL*2-1,' ')||ENAME AS VARCHAR2(20)) ENAME, JOB, MGR, DEPTNO, LEVEL 
FROM EMP 
START WITH JOB='PRESIDENT' 
CONNECT BY PRIOR EMPNO= MGR;
 --找到职位PRESIDENT的上级及上级的上级
 SELECT EMPNO, ENAME, JOB, MGR, DEPTNO, LEVEL FROM EMP 
 START WITH JOB='PRESIDENT' 
 CONNECT BY  MGR = PRIOR EMPNO;

--9.2、SYS_CONNECT_BY_PATH ( COLUMN , CHAR )

--从雇员KING开始,查出所有下级雇员相关信息以及雇员名路径用’/'连接)
SELECT EMPNO,
       CAST(LPAD(' ', LEVEL * 2 - 1, ' ') || ENAME AS VARCHAR2(20)) ENAME,
       JOB,
       MGR,
       DEPTNO,
       LEVEL,
       CAST(SYS_CONNECT_BY_PATH(ENAME, '/') AS VARCHAR2(40)) NAMEPATH
  FROM EMP
 START WITH ENAME = 'KING'
CONNECT BY PRIOR EMPNO = MGR;

--9.3、CONNECT_BY_ROOT  查找根节点

SELECT EMPNO,
       CAST(LPAD(' ', LEVEL * 2 - 1, ' ') || ENAME AS VARCHAR2(20)) ENAME,
       JOB,
       MGR,
       DEPTNO,
       LEVEL,
       CAST(SYS_CONNECT_BY_PATH(ENAME, '/') AS VARCHAR2(40)) NAMEPATH,
       CONNECT_BY_ROOT(EMPNO) ROOTNO
  FROM EMP
 START WITH ENAME IN ('JONES','BLAKE','CLARK')
CONNECT BY PRIOR EMPNO = MGR;

--9.4、NOCYCLE、CONNECT_BY_ISCYCLE 让语句不循环 是否循环
--注:CONNECT_BY_ISCYCLE 为1表示是循环的点

SELECT EMPNO, E

你可能感兴趣的:(Oracle,PL/SQL,Oracle进阶,oracle,plsql,数据库,sql)