Given Query
|
Alternative
|
SELECT ename, hiredate, sal FROM emp WHERE SUBSTR(ename,1,3) = 'SCO'; |
SELECT ename, hiredate, sal FROM emp WHERE ename LIKE 'SCO%'; |
VARIABLE name VARCHAR2(20) exec name := 'SCOTT' SELECT ename, hiredate, sal FROM emp WHERE ename = NVL (:name, ename); |
VARIABLE name VARCHAR2(20) exec name := 'SCOTT' SELECT ename, hiredate, sal FROM emp WHERE ename LIKE NVL (:name, '%'); |
SELECT ename, hiredate, sal FROM emp WHERE TRUNC (hiredate) = TRUNC (SYSDATE); |
SELECT ename, hiredate, sal FROM emp WHERE hiredate BETWEEN TRUNC (SYSDATE) AND TRUNC (SYSDATE) + .99999; |
SELECT ename, hiredate, sal FROM emp WHERE ename || empno = 'SCOTT7788'; |
SELECT ename, hiredate, sal FROM emp WHERE ename = 'SCOTT AND empno = 7788; |
SELECT ename, hiredate, sal FROM emp WHERE sal + 3000 < 5000; |
SELECT ename, hiredate, sal FROM emp WHERE sal < 2000; |
SELECT ename, hiredate, sal FROM emp WHERE sal != 0; |
SELECT ename, hiredate, sal FROM emp WHERE sal > 0; |
Given Query
|
Alternative
|
SELECT d.dname, AVG (e.sal) FROM emp e, dept d WHERE e.deptno = d.deptno GROUP BY d.dname HAVING dname != 'RESEAECH' AND dname != 'SALES'; |
SELECT d.dname, AVG (e.sal) FROM emp e, dept d WHERE e.deptno = d.deptno AND dname != 'RESEAECH' AND dname != 'SALES' GROUP BY d.dname; |
Separate Subqueries
|
Combined Subqueries
|
SELECT ename FROM emp WHERE sal = (SELECT MAX (sal) FROM lookup) AND comm = (SELECT MAX (comm) FROM lookup); |
SELECT ename FROM emp WHERE (sal,comm) = (SELECT MAX (sal), MAX(comm) FROM lookup); |
SELECT ename FROM emp E WHERE EXISTS (SELECT 'X' FROM dept WHERE deptno = E.deptno AND dname = 'ACCOUNTING'); |
SELECT ename FROM emp E WHERE deptno IN (SELECT deptno FROM dept WHERE deptno = E.deptno AND dname = 'ACCOUNTING'); |
SELECT ename FROM dept D, emp E WHERE E.deptno = D.deptno AND D.dname = 'ACCOUNTING'; |
Given Query
|
Alternative
|
SELECT DISTINCT d.deptno, d.dname FROM dept D, emp E WHERE D.deptno = E.deptno; |
SELECT d.deptno, d.dname FROM dept D WHERE EXISTS (SELECT 'X' FROM emp E WHERE E.deptno = D.deptno); |
UNION
|
UNION ALL
|
SELECT acct, balance FROM debit WHERE trandate = '31-DEC-95' UNION SELECT acct, balance FROM credit WHERE trandate = '31-DEC-95'; |
SELECT acct, balance FROM debit WHERE trandate = '31-DEC-95' UNION ALL SELECT acct, balance FROM credit WHERE trandate = '31-DEC-95'; |
SELECT COUNT(*) FROM emp WHERE status = 'Y' AND ename LIKE 'SMITH%'; ---------- SELECT COUNT(*) FROM emp WHERE status = 'N' AND ename LIKE 'SMITH%'; |
SELECT COUNT(DECODE(status, 'Y', 'X', NULL)) Y_count, COUNT(DECODE(status, 'N', 'X', NULL)) N_count FROM emp WHERE ename LIKE 'SMITH%'; |
SELECT * FROM dept WHERE deptno NOT IN (SELECT deptno FROM EMP); |
SELECT dept.* FROM dept, emp WHERE dept.deptno = emp.deptno (+) AND emp.ROWID IS NULL; |
SELECT * FROM dept WHERE NOT EXISTS (SELECT NULL FROM emp WHERE emp.deptno = dept.deptno); |
Conventional SQL
|
New Syntax
|
SELECT empno, ename, dept.deptno, dname FROM emp, dept WHERE emp.deptno(+) = dept.deptno UNION ALL SELECT empno, ename, emp.deptno, NULL FROM emp, dept WHERE emp.deptno = dept.deptno(+) AND dept.deptno IS NULL ORDER BY 1,2,3,4;
EMPNO ENAME DEPTNO DNAME ---------- ---------- ---------- -------------- 7369 SMITH 20 RESEARCH 7499 ALLEN 30 SALES 7521 WARD 30 SALES 7566 JONES 20 RESEARCH 7654 MARTIN 30 SALES 7698 BLAKE 30 SALES 7782 CLARK 9 7788 SCOTT 20 RESEARCH 7839 KING 9 7844 TURNER 30 SALES 7876 ADAMS 20 RESEARCH 7900 JAMES 30 SALES 7902 FORD 20 RESEARCH 7934 MILLER 9 10 ACCOUNTING 40 OPERATIONS |
SELECT empno, ename, NVL(dept.deptno,emp.deptno) deptno, dname FROM emp FULL OUTER JOIN dept ON (emp.deptno = dept.deptno) ORDER BY 1,2,3,4; EMPNO ENAME DEPTNO DNAME ---------- ---------- ---------- -------------- 7369 SMITH 20 RESEARCH 7499 ALLEN 30 SALES 7521 WARD 30 SALES 7566 JONES 20 RESEARCH 7654 MARTIN 30 SALES 7698 BLAKE 30 SALES 7782 CLARK 9 7788 SCOTT 20 RESEARCH 7839 KING 9 7844 TURNER 30 SALES 7876 ADAMS 20 RESEARCH 7900 JAMES 30 SALES 7902 FORD 20 RESEARCH 7934 MILLER 9 10 ACCOUNTING 40 OPERATIONS |