0% found this document useful (0 votes)
6 views9 pages

ORACLE11AM

The document discusses different types of joins in SQL including equi joins, non-equi joins, outer joins, cross joins and self joins. It provides the syntax for each join type in both ANSI and non-ANSI format. It also includes examples to retrieve data from multiple tables using different join conditions.

Uploaded by

pavan raut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

ORACLE11AM

The document discusses different types of joins in SQL including equi joins, non-equi joins, outer joins, cross joins and self joins. It provides the syntax for each join type in both ANSI and non-ANSI format. It also includes examples to retrieve data from multiple tables using different join conditions.

Uploaded by

pavan raut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

(09-02-2024)

JOINS:
======
- In RDBMS data can be stored in multiple tables from those multiple
tables if we want to retrieve data then we use a technique is called as "JOINS".
- Joins are used to retrieving the required data from multiple tables
at a time.
- Oracle supports the following two types of joins those are,
1. Non-ansi format joins (old style format) (orcle8i version)
- Equi join
- Non-equi join
- Self join
2. Ansi format joins(new style format) (oracle9i version)
- Inner join
- Outer join
- Left outer join
- Right outer join
- Full outer join
- Cross join
- Natural join

syntax for NON-ANSI JOINS:


========================
SELECT * FROM <TN1>,<TN2> WHERE <JOIN CONDITION>;

syntax for ANSI JOINS:


===================
SELECT * FROM <TN1> <join key> <TN2> ON <JOIN CONDITION>;

EQUI JOIN / INNER JOIN:


=====================
- when we retrieve data from multiple tables based on an " = " operator
is called as "equi / inner " join.

syntax for JOIN CONDITION:


========================
Non-ansi format:
==============
where <tn1>.<common column name> = <tn2>.<common column name>;

Ansi format:
===========
on <tn1>.<common column name> = <tn2>.<common column name>;

Demo_tables:
===========
SQL> SELECT * FROM COURSE;

CID CNAME CFEE


---------- ---------- ----------
101 ORACLE 2500
102 JAVA 3500
103 .NET 5000

SQL> SELECT * FROM STUDENT;

STID SNAME CID


---------- ---------- ----------
1021 SMITH 101
1022 ALLEN 101
1023 WARD 102
1024 MILLER

EX:
waq to retrieve student and the corresponding course details from the tables?
NON-ANSI:
=========
SQL> SELECT * FROM STUDENT,COURSE
2 WHERE STUDENT.CID=COURSE.CID;
(OR)
SQL> SELECT * FROM STUDENT S,COURSE C
2 WHERE S.CID=C.CID;

ANSI:
=====
SQL> SELECT * FROM STUDENT INNER JOIN COURSE
ON STUDENT.CID=COURSE.CID;
(OR)
SQL> SELECT * FROM STUDENT S INNER JOIN COURSE C
ON S.CID=C.CID;

RULE for JOINS:


==============
- a row in a table is comparing with all rows of another table.

Ex:
waq to retrieve student details who are joined in "ORACLE" course?
SQL> SELECT * FROM STUDENT S INNER JOIN COURSE C
2 ON S.CID=C.CID AND CNAME='ORACLE';
(OR)
SQL> SELECT * FROM STUDENT S INNER JOIN COURSE C
2 ON S.CID=C.CID WHERE CNAME='ORACLE';

Ex:
waq to display employees details who are working in "CHICAGO" location?
SQL> SELECT ENAME,LOC FROM EMP E INNER JOIN DEPT D
2 ON E.DEPTNO=D.DEPTNO WHERE LOC='CHICAGO';

Ex:
waq to display sum of salaries of each department names wise?
SQL> SELECT DNAME,SUM(SAL) FROM EMP E INNER JOIN DEPT D
2 ON E.DEPTNO=D.DEPTNO GROUP BY DNAME;

Ex:
waq to display sum of salaries of departments from emp,dept tables
if sum of salary of deperament is more than 10000?
SQL> SELECT DNAME,SUM(SAL) FROM EMP E INNER JOIN DEPT D
2 ON E.DEPTNO=D.DEPTNO GROUP BY DNAME HAVING SUM(SAL)>10000;

12-02-2024:
===========
NON-EQUI JOIN:
==============
- retrieving data from multiple tables based on any operator
condition except an " = " operator is known as "non-equi join".
- In this join we can use the following operators are " < , > , <= , >= , !
= ,
Between,AND,..........etc
DEMO_TABLES:
SQL> SELECT * FROM TEST1;

SNO NAME
---------- ----------
10 SMITH
20 ALLEN

SQL> SELECT * FROM TEST2;

SNO SAL
---------- ----------
10 23000
30 45000

Ex:
NON-ANSI:
SQL> SELECT * FROM TEST1 T1,TEST2 T2 WHERE T1.SNO>T2.SNO;

ANSI:
SQL> SELECT * FROM TEST1 T1 JOIN TEST2 T2 ON T1.SNO>T2.SNO;

EX:
SQL> SELECT * FROM TEST1 T1 JOIN TEST2 T2 ON T1.SNO>=T2.SNO;
SQL> SELECT * FROM TEST1 T1 JOIN TEST2 T2 ON T1.SNO<T2.SNO;
SQL> SELECT * FROM TEST1 T1 JOIN TEST2 T2 ON T1.SNO<=T2.SNO;
SQL> SELECT * FROM TEST1 T1 JOIN TEST2 T2 ON T1.SNO !=T2.SNO;

EX:
waq to display employees whose salary is between low salary and high salary?
SQL> SELECT ENAME,SAL,LOSAL,HISAL FROM EMP JOIN
2 SALGRADE ON SAL BETWEEN LOSAL AND HISAL;
(OR)
SQL> SELECT ENAME,SAL,LOSAL,HISAL FROM EMP JOIN
2 SALGRADE ON (SAL>=LOSAL) AND (SAL<=HISAL);

OUTER JOINS:
=============
- by using equi join we are retrieving matching rows only from the
tables.if we want to retrieve matching and unmatching rows then we should
use "outer joins" techniques.

i) Left outer join:


==============
- retrieving matching data from multiple tables and unmatching data
from the left side of table only.
ANSI:
=====
SQL> SELECT * FROM STUDENT S LEFT OUTER JOIN COURSE C
2 ON S.CID=C.CID;

NON-ANSI:
=========
- when we want to implement outer joins mechanisms in non-ansi
format then we should use a join operator is " (+) ".
SQL> SELECT * FROM STUDENT S , COURSE C WHERE S.CID=C.CID(+);

ii) Right outer join:


==============
- retrieving matching data from multiple tables and unmatching data
from the right side of table only.
ANSI:
=====
SQL> SELECT * FROM STUDENT S RIGHT OUTER JOIN COURSE C
ON S.CID=C.CID;

NON-ANSI:
=========
SQL> SELECT * FROM STUDENT S , COURSE C WHERE S.CID(+)=C.CID;

iii) FULL OUTER JOIN:


===================
- it is a combination of left outer and right outer join.
- by using full outer join we can retrieve matching and unmatching
rows from both sides of tables.
ANSI:
SQL> SELECT * FROM STUDENT S FULL OUTER JOIN COURSE C ON S.CID=C.CID;

NON-ANSI:
SQL> SELECT * FROM STUDENT S,COURSE C WHERE S.CID(+)=C.CID(+);
ERROR at line 2:
ORA-01468: a predicate may reference only one outer-joined table

SOLUTION:
SQL> SELECT * FROM STUDENT S , COURSE C WHERE S.CID=C.CID(+)
2 UNION
3 SELECT * FROM STUDENT S , COURSE C WHERE S.CID(+)=C.CID;

13-02-2024:
==========
CROSS JOIN:
===========
- joining two or more than two tables without any condition.
- in cross join mechanism each row of a table will join with each row
of another table for example a table is having (m) no.of rows and other table
is having (n) no.of rows then the result is (m X n) rows.

EX:
ANSI:
SQL> SELECT * FROM STUDENT CROSS JOIN COURSE;

NON-ANSI:
SQL> SELECT * FROM STUDENT,COURSE;

DEMO_TABLES:
=============
SQL> SELECT * FROM ITEMS1;

SNO INAME PRICE


---------- ---------- ----------
1 PIZZA 180
2 BURGER 85

SQL> SELECT * FROM ITEMS2;

SNO INAME PRICE


---------- ---------- ----------
101 PEPSI 20
102 COCACOLA 25

EX:
SQL> SELECT I1.INAME,I1.PRICE,I2.INAME,I2.PRICE,
I1.PRICE+I2.PRICE AS TOTAL_BILL_AMOUNT
FROM ITEMS1 I1 CROSS JOIN ITEMS2 I2;

NATURAL JOIN:
==============
- it is a similar to equi join for retrieving matching rows from multiple
tables.

Natutal Join VS Equi Join:


======================

Natural Join Equi Join


=========== ========
1. this condition is prepared by 1. this condition is prepared
system by default . by user.

2. to eliminate duplicate columns 2. duplicate columns are not


from the result set. eliminated from the result set.

3. common column name is 3. common column name is


mandatory. optional.

EX:
SQL> SELECT * FROM STUDENT S NATURAL JOIN COURSE C;

SELF JOIN:
=========
- joining a table data by itself is called as "self join".
(or)
- comparing a table data by itself is called as "self join".
- it can be implemented on a single table data only.
- when we use self join we need to create alias names on a table
otherwise self join cannot be implemented.
- once we created alias name on a table internally system is preparing
a virtual table on each alias name wise.
- we can create any no.of alias names on a single table but each alias
name should be different.

DEMO_TABLE:
============
SQL> SELECT * FROM TEST;

ENAME LOC
---------- ----------
SMITH HYD
JONES MUMBAI
MILLER HYD
ADAMS CHENNAI

14-02-2024:
==========
EX:
waq to display employees who are working in the same location the employee
"smith" is also working?
NON-ANSI:
SQL> SELECT T1.ENAME,T1.LOC FROM TEST T1,TEST T2
WHERE T1.LOC=T2.LOC AND T2.ENAME='SMITH';

ANSI:
SQL> SELECT T1.ENAME,T1.LOC FROM TEST T1 JOIN TEST T2
ON T1.LOC=T2.LOC AND T2.ENAME='SMITH';

ENAME LOC
---------- ----------
SMITH HYD
MILLER HYD

EX:
waq to display employees whose salary is same as the employee "SCOTT"
salary?
SQL> SELECT E1.ENAME,E1.SAL FROM EMP E1 JOIN EMP E2
2 ON E1.SAL=E2.SAL AND E2.ENAME='SCOTT';

EX:
waq to display managers and their employees from emp table?
SQL> SELECT M.ENAME AS MANAGER,E.ENAME AS EMPLOYEES
2 FROM EMP E JOIN EMP M ON M.EMPNO=E.MGR;

EX:
waq to display employees who are working under "BLAKE" manager?
SQL> SELECT M.ENAME AS MANAGER,E.ENAME AS EMPLOYEES
2 FROM EMP E JOIN EMP M ON M.EMPNO=E.MGR
3 WHERE M.ENAME='BLAKE';

EX:
waq to display the manager of "BLAKE" employee?
SQL> SELECT E.ENAME AS EMPLOYEE,M.ENAME AS MANAGER
2 FROM EMP E JOIN EMP M ON M.EMPNO=E.MGR
3 AND E.ENAME='BLAKE';

How to join more than two tables:


=============================
syntax for NON-ANSI format:
=========================
SELECT * FROM <TN1>,<TN2>,<TN3>,<TN4>,............................
WHERE <JOIN CONDITION1> AND <JOIN CONDITION2> AND <JOIN CONDITION3>
AND ..................;

syntax for ANSI format:


====================
SELECT * FROM <TN1> <JOIN KEY> <TN2> ON <JOIN CONDITION1>
<JOIN KEY> <TN3> ON <JOIN CONDITION2>
<JOIN KEY> <TN4> ON <JOIN CONDITION3>
........................................................................
........................................................................
<JOIN KEY> <TN n> ON <JOIN CONDITION n-1>;

DEMO_TABLE:
============
SQL> SELECT * FROM REGISTER;

REGNO REGDATE CID


---------- --------- ----------
10001 13-FEB-24 101
10002 14-FEB-24 102
10003 11-FEB-24

Equi join / Inner join:


==================
NON-ANSI:
SQL> SELECT * FROM STUDENT S,COURSE C,REGISTER R
2 WHERE S.CID=C.CID AND C.CID=R.CID;

ANSI:
SQL> SELECT * FROM STUDENT S INNER JOIN COURSE C
2 ON S.CID=C.CID INNER JOIN REGISTER R
3 ON C.CID=R.CID;

You might also like