0% found this document useful (0 votes)
10 views

Ex 5

The document describes how to create relationships between databases using join operations. It defines inner, outer, and different types of joins. It then creates two sample tables - Dept and Emp2 - and demonstrates various join queries between the tables including inner joins, outer joins, self joins, and cross joins. The aim of creating and relating the databases was achieved using join operations.

Uploaded by

Sidharth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Ex 5

The document describes how to create relationships between databases using join operations. It defines inner, outer, and different types of joins. It then creates two sample tables - Dept and Emp2 - and demonstrates various join queries between the tables including inner joins, outer joins, self joins, and cross joins. The aim of creating and relating the databases was achieved using join operations.

Uploaded by

Sidharth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex. No: 5 Creating relationship between the databases.

AIM
To create databases and implement the relationship between databases using join
operation.
DESCRIPTION:
JOIN OPERATIONS
✔ INNER JOIN/ NATURAL JOIN/ JOIN: It is a binary operation that allows us to combine
certain selections and a Cartesian product into one operation.
✔ OUTER JOIN: It is an extension of join operation to deal with missing information.

✔ Left Outer Join: It takes tuples in the left relation that did not match with any tuple in the
right relation, pads the tuples with null values for all other attributes from the right
relation and adds them to the result of the natural join.
✔ Right Outer Join: It takes tuples in the right relation that did not match with any tuple in
the left relation, pads the tuples with null values for all other attributes from the left
relation and adds them to the result of the natural join.
✔ Full Outer Join: It combines tuples from both the left and the right relation and pads the
tuples with null values for the missing attributes and hem to the result of the natural join.
CREATING TABLES FOR DOING JOIN AND NESTED QUERY OPERATIONS
Creating Dept table:
SQL> create table dept(dno number(10),dname varchar(10),loc varchar(10)); Table created.
SQL> insert into dept values(10,'inventory','hyd');
1 row created.
SQL> insert into dept values(20,'finance','bglr');
1 row created.
SQL> insert into dept values(30,'HR','mumbai');
1 row created.
SQL> select * from dept;
DNO DNAME LOC
---------- ---------- ----------
10 inventory hyd
20 finance bglr
30 HR mumbai
Creating emp2 table:
SQL> create table emp2(eno number(10),ename varchar(10),job varchar(10),Mgr
number(10),dno number(10));
Table created.
SQL> insert into emp2 values(111,'saketh','analyst',444,10);
1 row created.
SQL> insert into emp2 values(222,'sandeep','clerk',333,20);
1 row created.
SQL> insert into emp2 values(333,'jagan','manager',111,10);
1 row created.
xSQL> insert into emp2 values(444,'madhu','engineer',222,40);

29
1 row created.

SQL> select * from emp2;


ENO ENAME JOB MGR DNO
---------- ---------- ---------- ---------- ---------- ----------
111 saketh analyst 444 10
222 sandeep clerk 333 20
333 jagan manager 111 10
444 madhu engineer 222 40
1. Equijoin:
A join which contains an equal to ‘=’ operator in this joins condition
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno;
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ------------------- -------------
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
333 jagan manager inventory hyd
Using Clause:
SQL> select eno,ename,job,dname,loc from emp2 e join dept d using(dno);
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ---------- --------------------
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
333 jagan manager inventory hyd
On Clause:
SQL> select eno,ename,job,dname,loc from emp2 e join dept d on(e.dno=d.dno);
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ---------- ------------------
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
333 jagan manager inventory hyd
2. Non-Equijoin:
A join which contains an operator other than equal to ‘=’ in the join condition.
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno>d.dno;
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ---------- -----------------
222 sandeep clerk inventory hyd
444 madhu engineer inventory hyd
444 madhu engineer finance bglr
444 madhu engineer HR Mumbai
3. Self Join:
Joining the table itself is called self join.
SQL> select e1.eno,e2.ename,e1.job,e2.dno from emp2 e1,emp2 e2 where e1.eno=e2 mgr;
ENO ENAME JOB DNO
---------- ---------- ---------- ---------- --------
444 saketh engineer 10
333 sandeep manager 20
111 jagan analyst 10

30
222 madhu clerk 40
4. Natural Join:
It compares all the common columns.
SQL> select eno,ename,job,dname,loc from emp2 natural join dept;
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ---------- --------------
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
333 jagan manager inventory hyd
5. Cross Join:
This will give the cross product.
SQL> select eno,ename,job,dname,loc from emp2 cross join dept;
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ---------- ------------------
111 saketh analyst inventory hyd
222 sandeep clerk inventory hyd
333 jagan manager inventory hyd
444 madhu engineer inventory hyd
111 saketh analyst finance bglr
222 sandeep clerk finance bglr
333 jagan manager finance bglr
444 madhu engineer finance bglr
111 saketh analyst HR mumbai
222 sandeep clerk HR mumbai
333 jagan manager HR mumbai
444 madhu engineer HR mumbai
12 rows selected.
6. Outer Join:
It gives the non matching records along with matching records.
6.1 Left Outer Join:
This will display the all matching records and the records which are in left hand side table
those that are in right hand side table.
SQL> select eno,ename,job,dname,loc from emp2 e left outer join dept d on(e.dno= d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno(+);
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ---------- -------------------
333 jagan manager inventory hyd
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
444 madhu Engineer
6.2 Right Outer Join:
This will display the all matching records and the records which are in right hand side
table those that are not in left hand side table.
SQL> select eno,ename,job,dname,loc from emp2 e right outer join dept d on(e.dno =d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno(+)=d.dno;
ENO ENAME JOB DNAME LOC

31
---------- ---------- ---------- ---------- --------------------------------
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
333 jagan manager inventory hyd
HR mumbai
6.3 Full Outer Join:
This will display the all matching records and the non matching records from both tables.
SQL> select eno,ename,job,dname,loc from emp2 e full outer join dept d on(e.dno= d.dno);
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ---------- -----------------
333 jagan manager inventory hyd
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
444 madhu engineer HR Mumbai

RESULT
Thus the relationship between databases has been implemented using join operation.

32

You might also like