-
SQL基本CRUD
-
- SELECT *
- FROM EMP E
- WHERE E.DEPTNO = 20;
-
- SELECT *
- FROM EMP
- WHERE COMM > SAL;
-
- SELECT *
- FROM EMP
- WHERE COMM > SAL*0.2;
-
- SELECT *
- FROM EMP E
- WHERE (E.DEPTNO = 10 AND E.JOB = 'MANAGER')
- OR (E.DEPTNO = 20 AND E.JOB = 'CLERK');
-
- SELECT * FROM EMP
- WHERE JOB NOT IN ('MANAGER','CLERK')
- AND SAL >= 2000;
-
- SELECT *
- FROM EMP
- WHERE COMM IS NULL
- OR COMM < 100;
-
- SELECT *
- FROM EMP
- WHERE MONTHS_BETWEEN(SYSDATE,HIREDATE)/12 > 10;
-
- SELECT INITCAP(ENAME)
- FROM EMP;
- SELECT UPPER(SUBSTR(ENAME,1,1))||
- LOWER(SUBSTR(ENAME,2))
- FROM EMP;
-
- SELECT *
- FROM EMP
- WHERE TO_CHAR(HIREDATE,'MM') = '02';
-
- SELECT ENAME,TO_CHAR(HIREDATE,'YYYY') YEAR,
- TO_CHAR(HIREDATE,'MM') MONTH
- FROM EMP
- ORDER BY MONTH,YEAR
-
- SELECT E.*
- FROM EMP E
- START WITH ENAME = 'JONES'
- CONNECT BY PRIOR EMPNO = MGR;
-
- SELECT E.*
- FROM EMP E
- START WITH ENAME = 'SCOTT'
- CONNECT BY PRIOR MGR = EMPNO;
-
- SELECT A.ENAME,A.JOB,A.DEPTNO,
- B.ENAME,B.JOB,B.DEPTNO
- FROM EMP A,EMP B
- WHERE A.JOB = B.JOB AND A.DEPTNO != B.DEPTNO;
-
- SELECT D.DEPTNO,COUNT(E.EMPNO),AVG(E.SAL),
- D.DNAME,D.LOC
- FROM EMP E,DEPT D
- WHERE E.DEPTNO = D.DEPTNO
- GROUP BY D.DEPTNO,D.DNAME,D.LOC;
-
- SELECT *
- FROM EMP
- WHERE EMPNO IN
- (
- SELECT MGR
- FROM EMP
- WHERE DEPTNO = 10
- )
- OR DEPTNO = 10;
-
- SELECT *
- FROM EMP
- WHERE SAL IN
- (
- SELECT AVG(SAL)
- FROM EMP
- GROUP BY DEPTNO
- );
-
- SELECT *
- FROM EMP E1
- WHERE SAL >
- (
- SELECT AVG(SAL)
- FROM EMP E2
- WHERE E2.DEPTNO = E1.DEPTNO
- );
-
- SELECT E.*,A.AVGSAL
- FROM EMP E,
- (
- SELECT DEPTNO,AVG(SAL) AS AVGSAL
- FROM EMP
- GROUP BY DEPTNO
- )A
- WHERE A.DEPTNO = E.DEPTNO
- AND E.SAL > A.AVGSAL;
-
- SELECT COUNT(*),E.JOB,AVG(E.SAL)
- FROM EMP E
- GROUP BY E.JOB;
-
- SELECT ENAME,TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12)||
- '年'||TRUNC(MOD(MONTHS_BETWEEN(SYSDATE,HIREDATE),12))||
- '月'||TRUNC(SYSDATE - ADD_MONTHS(HIREDATE,
- MONTHS_BETWEEN(SYSDATE, HIREDATE)))||'日'
- FROM EMP;
-
- SELECT *
- FROM DEPT
- WHERE DEPTNO IN
- (
- SELECT DEPTNO
- FROM
- (
- SELECT COUNT(*) COUNT,DEPTNO
- FROM EMP
- GROUP BY DEPTNO
- )
- WHERE COUNT IN
- (
- SELECT MAX(COUNT)
- FROM
- (
- SELECT COUNT(*) COUNT,DEPTNO
- FROM EMP
- GROUP BY DEPTNO
- )
- )
- );
-
- SELECT SUBSTR(SYS_CONNECT_BY_PATH(ENAME,'->'),3),
- LEVEL
- FROM EMP
- START WITH MGR IS NULL
- CONNECT BY PRIOR EMPNO = MGR;
-
- SELECT *
- FROM
- (
- SELECT *
- AVG(SAL) AVGSAL,DEPTNO
- FROM EMP
- GROUP BY DEPTNO
- ORDER BY AVGSAL DESC
- )
- WHERE ROWNUM = 1;
- SELECT DEPTNO, AVG(SAL)
- FROM EMP
- GROUP BY DEPTNO
- HAVING AVG(SAL) =
- (
- SELECT MAX(AVG(SAL)) AVGSAL
- FROM EMP
- GROUP BY DEPTNO
- );
-
- SELECT D.*
- FROM DEPT D
- WHERE DEPTNO IN
- (
- SELECT DEPTNO
- FROM EMP
- GROUP BY DEPTNO
- HAVING AVG(SAL)=
- (
- SELECT MAX(AVG(SAL)) AVGSLA
- FROM EMP
- GROUP BY DEPTNO
- )
- );
-
- SELECT D.DNAME
- FROM DEPT D
- WHERE DEPTNO IN
- (
- SELECT DEPTNO
- FROM EMP
- GROUP BY DEPTNO
- HAVING AVG(SAL)=
- (
- SELECT MIN(AVG(SAL)) AVGSLA
- FROM EMP
- GROUP BY DEPTNO
- )
- );
-
- SELECT D.DNAME
- FROM DEPT D
- WHERE D.DEPTNO IN
- (
- SELECT A.DEPTNO
- FROM
- (
- SELECT E.DEPTNO
- FROM EMP E, SALGRADE S
- GROUP BY E.DEPTNO
- ORDER BY AVG(S.GRADE)
- )A
- WHERE ROWNUM = 1
- );
-
- SELECT DNAME
- FROM DEPT
- WHERE DEPTNO =
- (
- SELECT DEPTNO
- FROM
- (
- SELECT DEPTNO
- FORM EMP
- WHERE JOB = 'MANAGER'
- GROUP BY DEPTNO
- ORDER BY MIN(SAL)
- )
- WHERE ROWNUM = 1
- );
-
- SELECT ENAME
- FROM EMP
- WHERE SAL >
- (
- SELECT MAX(SAL)
- FROM EMP
- WHERE JOB NOT IN
- ('MANAGER','PRESIDENT')
- )
- AND JOB = 'MANAGER'
- OR JOB = 'PRESIDENT';
-
- SELECT *
- FROM DEPT
- WHERE
- DEPTNO IN
- (
- SELECT DEPTNO
- FROM EMP
- WHERE DEPTNO NOT IN
- (
- SELECT DISTINCT DEPTNO
- FROM EMP
- WHERE SAL < 1000
- )
- );
-
- SELECT *
- FROM EMP E JOIN DEPT D
- ON D.DEPTNO = E.DEPTNO
- AND D.DEPTNO IN
- (
- SELECT DEPTNO
- FROM EMP
- WHERE DEPTNO NOT IN
- (
- SELECT DISTINCT DEPTNO
- FROM EMP
- WHERE SAL < 1000
- )
- );
-
- SELECT *
- FROM DEPT
- WHERE DEPTNO NOT IN
- (
- SELECT DEPTNO
- FROM EMP
- WHERE SAL NOT BETWEEN 900 AND 30000
- );
-
- SELECT *
- FROM EMP A
- WHERE A.DEPTNO IN
- (
- SELECT DISTINCT E.DEPTNO
- FROM EMP E
- WHERE E.SAL BETWEEN 900 AND 3000
- );
-
- SELECT D.*
- FROM DEPT D
- WHERE D.DEPTNO IN
- (
- SELECT DISTINCT E2.DEPTNO
- FROM EMP E1,EMP E2
- WHERE E1.EMPNO = E2.MGR
- );
-
- SELECT *
- FROM
- (
- SELECT SAL
- FROM EMP
- WHERE DEPTNO = 30
- ORDER BY SAL DESC
- )E
- WHERE ROWNUM < 4;
-
- SELECT E.ENAME,E.EMPNO,E.SAL
- FROM EMP E,SALGRADE S,DEPT D
- WHERE (E.SAL BETWEEN S.LOSAL AND S.HISAL)
- AND S.GRADE = 2
- AND TO_CHAR(E.HIREDATE,'YYYY') > 1985
- AND E.DEPTNO = D.DEPTNO
- AND D.LOC = 'DALLAS';
-
- UPDATE EMP E
- SET SAL = 1000 +
- (
- SELECT AVG(SAL)
- FROM EMP
- WHERE DEPTNO = E.DEPTNO
- );
-
- DELECT FROM DEPT D
- WHERE ROWID !=
- (
- SELECT MIN(ROWID)
- FROM DEPT
- WHERE DNAME = D.DNAME
- AND LOC = D.LOC
- );
-
- UPDATE EMP E
- SET SAL =
- (
- SELECT SAL
- FROM EMP
- WHERE EMPNO = E.MGR
- ),
- COMM =
- (
- SELECT COMM
- FORM EMP
- WHERE
- EMPNO = E.MGR
- );
- UPDATE EMP E
- SET (SAL,COMM) =
- (
- SELECT SAL, COMM
- FROM EMP
- WHERE EMPNO = E.MGR
- );
-
-
- CREATE TABLE BORROW
- (
- CNO NUMBER REFERENCES CARD(CNO),
- BNO NUMBER REFERENCES BOOKS(BNO),
- RDATE DATE,
- PRIMARY KEY(CNO,BNO)
- );
-
- ALTER TABLE BOOKS
- ADD PRIMARY KEY(BNO);
-
- ALTER TABLE CARD
- MODIFY NAME VARCHAR2(10);
-
- ALTER TABLE CARD
- ADD 系名 VARCHAR2(20);
-
- SELECT CNO,COUNT(*)
- FROM BORROW
- GROUP BY CNO
- HAVING COUNT(*) > 5;
-
- SELECT NAME, CLASS
- FROM CARD
- WHERE CNO IN
- (
- SELECT CNO
- FROM BORROW BW, BOOKS BK
- WHERE BW.BNO = BK.BNO
- AND BK.NAME = '水浒'
- );
-
- SELECT *
- FROM BORROW
- WHERE RDATE < SYSDATE;
-
- SELECT BNO, BNAME,AUTHOR
- FORM BOOKS
- WHERE BNAME LIKE '%网络%';
-
- SELECT BNAME,AUTHOR
- FROM BOOKS
- WHERE PRICE =
- (
- SELECT MAX(PRICE)
- FROM BOOKS
- );
-
- SELECT A.CNO
- FROM BORROW A, BOOKS B
- WHERE A.BNO = B.BNO
- AND B.BNAME = '计算方法'
- AND A.CNO NOT IN
- (
- SELECT AA.CNO
- FORM BORROW AA,BOOKS BB
- WHERE AA.BNO = BB.BNO
- AND BB.BNAME = '计算方法习题集'
- )
- ORDER BY A.CNO DESC;
-
- SELECT DISTINCT A.CNO
- FROM BORROW A,BOOKS B
- WHERE A.BNO = B.BNO
- AND B.BNAME IN
- ('计算方法','组合数学')
- ORDER BY A.CNO;
-
- UPDATE BORROW
- SET RDATE = RDATE + 7
- WHERE CNO IN
- (
- SELECT DISTINCT CNO
- FROM CARD
- WHERE CLASS = 'C01'
- );
-
- DELETE FROM BOOKS
- BNO NOT IN
- (
- SELECT DISTINCT BK, BNO
- FROM BORROW BR,BOOKS BK
- WHERE BR.BNO = BK.BNO
- );
-
- CREATE INDEX INX_BOOKS_BNAME
- ON BOOKS(BNAME);
-
- CREATE TRIGGER TR_SAVE
- ON BORROWFOR INSERT,UPDATE AS IF
- @@ROW COUNT > 0
- INSERT BORROW_SAVE
- SELECT I.*
- FROM INSERTED I,BOOKS
- WHERE I.BNO = B.BNO
- AND B.BNAME = N'数据库应用技术';
-
- CREATE VIEW V_VIEWASSELECT
- A.NAME,B.BNAME
- FROM BORROW AB,CARD A, BOOKS B
- WHERE AB.CNO = A.CNO
- AND AB.BNO = B.BNO
- AND A.CLASS = N'01班'
-
相关阅读:
Leetcode 647 回文子串
计算机专业课程——答案搜集
Centos搭建go环境以及go入门
android studio实现圆角的button
关于区块链的认识和看法
LeetCode 96. 不同的二叉搜索树
LeetCode 120. Triangle (三角形最小路径和)详解
LeetCode 64最小路径和
微信发起摄像头接口请求 上传头像 图片
Vue解决项目白屏以及(反向代理)
-
原文地址:https://www.cnblogs.com/guanghe/p/6054113.html
Copyright © 2020-2023
润新知