Chapter 8 - JOINS and SET Operations
Chapter 8 - JOINS and SET Operations
outline
Introduction
Joining Tables
Performing SET Operations on Relations
8.1ntroduction
• You must have encountered some sutuation where the simple queries could not work up
to your expectation.
• For instance , you might have wanted to extract columns from more than one table or to
group records coming from multiple tables (relations) or to find common records of two
tables.
• Two important concept of SQL -
In a join-query , more than one table are listed in the FROM clause
of SELECT query.
Thus we can say that a join is used when an SQL query requires data
from more than on table on database
Qualified Names
If we say deptno field from joining tables empl and dept , it will lead
to an ambiguity that “ deptno field from which table ? “
To avoid such ambiguity , the qualified field names are used.
Sometimes you need to extract information from more than two tables.In
this case you need to join more rhan two tables.
This can be achieved by specifying all the required table names in FROM
clause and by providing all join conditions using AND operator.
OUTPUT
8.2.6 Equi - join
The join, in which columns are compared for equality, is called
Equi - Join.
An equal sign ( = ) is used as comparison operator in the where
clause to refer equality.
Recall the output of example 1.
It works as follows :
-> To create Cartesian product ofif two tables (say empl and dept ) write query as:
no join-condition is specified with a
SELECT * JOIN clause, cartesian-product will be output.
To create equijoin on some common field (say deptno) of two tables (empl and dept
here ), write query as :
SELECT * Join condition is specified with ON clause
FROM empl e
JOIN dept d
ON (e.deptno = d.deptno);
Cross Join : MySQL provide a NATURAL JOIN clause for you that , one of the
duplicate columns get removed from thr output.
SELECT *
FROM <table 1>
NATURAL JOIN <table2> ;
SELECT *
FROM empl
NATURAL JOIN dept ;
Filtering Output : We can further filter the join output, by using the WHERE
clause.
For instance, following query will first create a natural join from tables empl
and dept and then extract only those records where sal is more than 2000.
mysql > SELECT FROM empl e
-> NATURAL JOIN dept d
-> WHERE sal > 2000 ;
Similarly , the following query will first create equi-join from tables empl and
dept and then filter those records where sal is < 2000
mysql > SELECT * FROM empl
-> JOIN dept ON (empl.deptno = dept.deptno)
-> WHERE sal < 2000 ;
USING vs ON Clause : We can also use the USING clause in place of ON clause
but with USING clause we just have to specify the name of join-field.That is
above query can also be reframes as:
SELECT *
FROM empl
JOIN dept
USING (deptno);
Difference between ON and USING sub clauses of JOIN clause of SELECT
SQL joins tend to combine columns from two or more tables (i.e. width
wise) and SQL SET Operation tend to combine rows from two or more
tables (i.e. length wise)
There are three SET Operations.
1. UNION
2. INTERSECT
3. MINUS
8.3.1 UNION
Multiple queries can be combined into one by forming a union of
them.
2. The ORDER BY clause can occur onlyat the end of the UNION statement .It
can’t be used within the individual queries that make the UNION statement.
3. The GROUP BY and HAVING clauses are allowd only within indiviual queries
.These clauses cannot be used to affect the final result set.
8.3.1B UNION vs. UNION ALL
Both UNION and UNION ALL give the combine rows of two tables, but UNION
removes the duplicate entries while UNION ALL retains the duplicate entries.
8.3.2 MINUS Operator
• The MINUS operator is used to get the unique rows from the table1, i.e. the
rows which are in table1 but not in table2.
• MINUS is also known as EXCEPT.
• We need to be careful with the order of SELECT statements.table1 MINUS
table2 is not the same as table2 MINUS table1.