JOINING of Tables - Retrieving
data from multiple tables
JOINS: JOINs are used to retrieve information from multiple tables.
TYPES OF JOINS:
1. EQUI-JOIN : JOINING of EMP and DEPT tables Using
DEPTNO in WHERE clause.
2. NON-EQUI JOIN : JOINING of EMP and SALGRADE tables
Using WHERE clause
3. OUTER JOIN
4. SELF JOIN
EQUI-JOIN
SQL> SELECT EMP.ENAME,EMP.JOB,EMP.DEPTNO,
DEPT.DNAME,DEPT.LOC
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO;
ENAME JOB DEPTNO DNAME LOC
SMITH CLERK 20 RESEARCH DALLAS
ALLEN SALESMAN 30 SALES CHICAGO
SQL> SELECT E.ENAME,E.JOB,E.DEPTNO,D.DNAME,D.LOC
2 FROM EMP E,DEPT D
3 WHERE E.DEPTNO=D.DEPTNO;
SQL> SELECT ENAME,JOB,E.DEPTNO,DNAME,LOC
2 FROM EMP E,DEPT D
3 WHERE E.DEPTNO=D.DEPTNO;
NON-EQUI JOIN
SQL> SELECT ENAME,JOB,SAL,GRADE
2 FROM EMP E,SALGRADE S
3 WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL;
ENAME JOB SAL GRADE
---------- --------- ---------- ----------
SMITH CLERK 800 1
ADAMS CLERK 1100 1
JAMES CLERK 950 1
WARD SALESMAN 1250 2
MARTIN SALESMAN 1250 2
MILLER CLERK 1300 2
ALLEN SALESMAN 1600 3
TURNER SALESMAN 1500 3
JONES MANAGER 2975 4
BLAKE MANAGER 2850 4
JOINING of EMP,DEPT &
SALGRADE tables
SQL> SELECT ENAME,SAL,E.DEPTNO,DNAME,LOC,GRADE
2 FROM EMP E,DEPT D,SALGRADE S
3 WHERE E.DEPTNO=D.DEPTNO AND
4 E.SAL BETWEEN S.LOSAL AND S.HISAL;
ENAME SAL DEPTNO DNAME LOC GRADE
SMITH 800 20 RESEARCH DALLAS 1
ADAMS 1100 20 RESEARCH DALLAS 1
JAMES 950 30 SALES CHICAGO 1
WARD 1250 30 SALES CHICAGO 2
MARTIN 1250 30 SALES CHICAGO 2
OUTER JOIN- operator “(+)”
Used for displaying missing data.
SELECT ENAME,JOB,SAL,E.DEPTNO,D.DEPTNO,DNAME,LOC
FROM EMP E, DEPT D WHERE E.DEPTNO=D.DEPTNO;