collating information
☛ 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 process of forming rows from two or more tables by comparing
the contents of related columns is called joining tables
☛the resulting table, containing data from both of the original tables,
is called a join between tables
☛ guidelines for joins
☛ with joins, you can combine the columns from multiple tables
☛ with joins, as many tables as desired may be joined together
☛ in a join, the tables to be joined are specified in the from clause,
separated by commas
☛ 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
☛ the columns with same name in multiple tables must be uniquely
identified by specifying [Link]
☛ tablename.* is the shortcut to specify all the columns in the table
☛ 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
the other table
☛useful in finding out all possible combination of columns from
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,[Link],dname,loc from emp,dept
where [Link] = [Link](+)
select empno,sal,[Link],dname,loc
from emp left outer join dept
on ([Link] = [Link]);
example of right outer join
select empno,ename,sal,[Link],dname,loc from emp,dept
where [Link](+) = [Link]
select empno,sal,[Link],dname,loc
from emp right outer join dept
on ([Link] = [Link]);
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.
select [Link] || 'works for' || [Link]
from emp e1, emp e2
where [Link] = [Link]
non-equi join
☛ non-equi join :
☛ the joins which use comparison operators other than `=’
(equals) in defining their criteria are called as non-equi
joins
select empno,sal,grade from emp,salgrade
where [Link] between [Link] and [Link]
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.