Professional Documents
Culture Documents
☛ objectives :
☛list the ways of collating information
☛explain what is join
☛list & explain types of joins
☛construct joins
☛list & explain set operators
collating information
☛ joins
☛equi-join
☛cartesian join
☛outer join
☛non-equi join
☛self join
☛ set operators
☛union
☛intersection
☛minus
☛ subqueries
☛nested subquery
☛co-related subquery
joins
☛request for data from two or more tables in the database
☛what are joins?
☛ the where clause specifies how to join or merge the tables together
as well as the search criteria if any
☛ the search criteria can refer to any column of any table joined
☛ the columns from either tables may be named in the select clause
☛ you can use table aliases while referring to long table names or
tables in self join
joins
☛ types of joins
☛cartesian join
☛equi-join
☛non-equi join
☛outer join
☛self join
cartesian join
☛ cartesian join :
☛if the join clause is omitted i.e. where clause is not specified, a
cartesian join is performed
☛a cartesian product matches every row of the table to every row of
different tables
☛ example :
☛ select empno, ename, dname, loc from emp, dept ;
equi join
☛ equi join :
☛when two tables are joined together using equality of values in one
or more columns, the join is called an equi-join
☛the comparison operator in the join condition is `=’ (i.e. equal to)
☛ examples :
1. list employee number, name, his department and the department
name.
2. list employee name, his department name and the department
location.
outer join
☛ outer join :
☛ if there are any values in one table that do not have corresponding
value(s) in the other, those rows will not be selected in an equi-
join
☛ such rows can be forcefully selected by using the outer join
☛ outer join uses (+) sign
☛ the corresponding columns (from other table) for that row will have
nulls
outer join
☛ outer joins :
☛ left outer join
☛ right outer join
☛ examples :
1. list details of all employees working in each department.
2. list the department information even if no employee belongs to
that department.
example of left outer join
select empno,ename,sal,dept.deptno,dname,loc from emp,dept
where emp.deptno = dept.deptno(+)
select empno,sal,emp.deptno,dname,loc
from emp left outer join dept
on (emp.deptno = dept.deptno);
example of right outer join
select empno,ename,sal,dept.deptno,dname,loc from emp,dept
where emp.deptno(+) = dept.deptno
select empno,sal,emp.deptno,dname,loc
from emp right outer join dept
on (emp.deptno = dept.deptno);
self join
☛ self-join
☛ used to match and retrieve rows that have matching values in
different columns of the same table
☛ a table can be joined to itself as if it were two different tables
☛ to distinguish the column names from one another, the table name
alias is used
☛ example :
☛ list employee number, name, job, his manager’s name, manager’s
job.
set operators
☛ set operators combine results of two or more
queries into one result
☛ data types of corresponding columns must be the
same
☛ operators :
☛union
☛intersect
☛minus
union
☛ returns distinct rows of first query plus distinct rows of
second query
☛ multiple queries can be merged together and their results
combined, using union operator
☛ the number of columns in the query must be same and of
the same data type
☛ example :
☛ display different designations in department 20 and 30.
intersect
☛ returns the common rows from all queries
☛ example :
☛ list the jobs common to department 20 and 30.
minus
☛ returns the rows unique to the first query
☛ example :
☛ list the jobs unique to department 20 and not any other
department.