2 单表
SELECT * FROM EMP WHERE DEPTNO = 30;
SELECT EMPNO,ENAME,DEPTNO FROM EMP WHERE JOB = ‘CLERK’;
SELECT * FROM EMP WHERE COMM > SAL;
SELECT * FROM EMP WHERE COMM > SAL * 0.6;
SELECT * FROM EMP WHERE DEPTNO=10 AND JOB=‘MANAGER’ OR DEPTNO=20 AND JOB=‘CLERK’;
SELECT * FROM EMP WHERE (DEPTNO=10 AND JOB=‘MANAGER’ OR DEPTNO=20 AND JOB=‘CLERK’) OR (JOB!=‘MANAGER’ AND JOB!=‘CLERK’ AND SAL>2000);
SELECT * FROM EMP WHERE COMM;
SELECT * FROM EMP WHERE COMM<100 OR COMM IS NULL;
SELECT * FROM EMP WHERE ENAME LIKE ‘A%’ OR ENAME LIKE ‘B%’ OR ENAME LIKE ‘S%’;
SELECT * FROM EMP WHERE LENGTH(ENAME)=6;
SELECT * FROM EMP WHERE ENAME NOT LIKE ‘%R%’;
SELECT * FROM EMP ORDER BY ENAME ASC;
SELECT * FROM EMP ORDER BY JOB DESC, SAL ASC;
SELECT ENAME,ROUND(SAL/30, 2) AS ‘日薪’ FROM EMP;
SELECT * FROM EMP WHERE ENAME LIKE ‘%A%’;
3 多表
SELECT DNAME,DEPTNO FROM DEPT WHERE DEPTNO IN (SELECT DEPTNO FROM EMP WHERE DEPTNO);
SELECT * FROM EMP WHERE SAL>(SELECT SAL FROM EMP WHERE ENAME=‘SMITH’);
SELECT E.ENAME, M.ENAME FROM EMP E LEFT OUTER JOIN EMP M ON E.MGR=M.EMPNO;
or (没有经理是否显示)
SELECT E.ENAME, M.ENAME FROM EMP E, EMP M WHERE E.MGR=M.EMPNO;
SELECT E.ENAME, M.ENAME FROM EMP E LEFT OUTER JOIN EMP M ON E.MGR=M.EMPNO WHERE E.HIREDATE
HIREDATE;
SELECT ENAME,DNAME FROM EMP,DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO;
SELECT ENAME,DNAME FROM EMP,DEPT WHERE JOB=‘CLERK’ AND EMP.DEPTNO=DEPT.DEPTNO;
SELECT DEPTNO,MIN(SAL) FROM EMP GROUP BY DEPTNO;
SELECT ENAME FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME=‘SALES’);
SELECT * FROM EMP WHERE SAL>(SELECT AVG(SAL) FROM EMP WHERE SAL);
SELECT * FROM EMP WHERE JOB=(SELECT JOB FROM EMP WHERE ENAME=‘SCOTT’);
如果不包含自己
SELECT * FROM EMP WHERE JOB=(SELECT JOB FROM EMP WHERE ENAME=‘SCOTT’) AND ENAME!=‘SCOTT’;
SELECT ENAME,SAL FROM EMP WHERE SAL IN (SELECT SAL FROM EMP WHERE DEPTNO=30);
SELECT * FROM EMP WHERE SAL > ALL(SELECT SAL FROM EMP WHERE DEPTNO=30);
SELECT DEPT.DEPTNO,DNAME,LOC,COUNT(EMP.DEPTNO) AS NUMBER FROM DEPT,EMP WHERE DEPT.DEPTNO=EMP.DEPTNO GROUP BY EMP.DEPTNO;
SELECT ENAME,DNAME,SAL FROM EMP,DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO;
SELECT EMP.*,DEPT.DNAME FROM EMP,DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO;
SELECT JOB, MIN(SAL) AS SAL FROM EMP GROUP BY JOB;
SELECT DEPT.DEPTNO,ENAME,JOB,MIN(SAL) FROM DEPT,EMP WHERE JOB=‘MANAGER’ GROUP BY EMP.DEPTNO;
SELECT ENAME,SAL*12 AS ySalary FROM EMP ORDER BY ySalary ASC;
SELECT ENAME,SAL FROM EMP WHERE SAL BETWEEN
(SELECT LOSAL FROM SALGRADE WHERE GRADE=4)
AND (SELECT HISAL FROM SALGRADE WHERE GRADE=4);
SELECT ENAME,LOC,SAL,LOSAL,HISAL FROM DEPT,EMP,SALGRADE
WHERE EMP.DEPTNO=DEPT.DEPTNO AND GRADE=2 AND SAL BETWEEN LOSAL AND HISAL;
SELECT MIN(SAL),MAX(SAL) FROM DEPT,EMP,SALGRADE
WHERE EMP.DEPTNO=DEPT.DEPTNO AND GRADE=2 AND SAL BETWEEN LOSAL AND HISAL;
SELECT * FROM EMP WHERE SAL>(SELECT SAL FROM EMP WHERE ENAME=‘smith’);