Ex 5
Ex 5
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.
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