Anda di halaman 1dari 37

Module 8:

Querying Multiple
Tables
Vidya Vrat Agarwal. | MCT, MCSD
Overview

 Introduction to Joins
 Using Inner Joins
 Using Outer Joins
 Using Cross Joins
 Joining More Than Two Tables
 Joining a Table to Itself
 Natural Join
 Equi, Non-Equi and Theta Join
 Union
Introduction to Joins

A Join is an Operation that allows to Query Two or More tables


to produce a result set. That Incorporates rows and columns
from each table.

Joins Operation is used on Columns that are common and of


Same Data Type to both tables.

The process of forming pairs of rows by matching the contents


of related columns is called Joining the Tables.
Purpose of Joins
A Join:
 Selects Specific Columns from Multiple Tables
 JOIN keyword specifies that tables are joined and how to
join them
 ON keyword specifies join condition

 Queries Two or More Tables to Produce a Result Set


 Use Primary and Foreign keys as join conditions
 Use columns common to specified tables to join tables
Inner Join

Inner Join combine rows by comparing values in columns


that are common to both tables.
i.e. UnMatched rows are not included in the result set.

1 2
4 4
7 5
8 8
12 13
Using Inner Joins
USE
USE joindb
joindb
SELECT
SELECT buyer_name,
buyer_name, sales.buyer_id,
sales.buyer_id, qty
qty
FROM
FROM buyers
buyers INNER
INNER JOIN
JOIN sales
sales
ON buyers.buyer_id = sales.buyer_id
ON buyers.buyer_id = sales.buyer_id
buyers sales
buyer_name
buyer_name buyer_id
buyer_id buyer_id
buyer_id prod_id
prod_id qty
qty
Adam
AdamBarr
Barr 11 11 22 15
15
Sean
SeanChai
Chai 22 11 33 55
Eva
EvaCorets
Corets 33 44 11 37
37
Erin
ErinO’Melia
O’Melia 44 33 55 11
11
44 22 1003
1003
Result
buyer_name
buyer_name buyer_id
buyer_id qty
qty
Adam
AdamBarr
Barr 11 15
15
Adam
AdamBarr
Barr 11 55
Erin
ErinO’Melia
O’Melia 44 37
37
Eva
EvaCorets
Corets 33 11
11
Erin
ErinO’Melia
O’Melia 44 1003
1003
Outer Join

Outer join retains UnMatched rows in result set.

Following are the 3 Types of Outer Joins :

1. Left Outer Join


2. Right Outer Join
3. Full Outer Join
Left and Right Outer Join

Left Outer Join combine rows from Left Table, plus any
UnMatched rows from the Right Table.

Right Outer Join combine rows from Right Table, plus any
UnMatched rows from the Left Table.

Rows that do not match the Join Condition, display NULL


in result set.

The only difference between a Right and Left Outer Join is,
how the Query is written. i.e., the Position of tables.
Using Outer Joins
USE
USE joindb
joindb
SELECT
SELECT buyer_name,
buyer_name, sales.buyer_id,
sales.buyer_id, qty
qty
FROM
FROM buyers
buyers LEFT
LEFT OUTER
OUTER JOIN
JOIN sales
sales
ON buyers.buyer_id = sales.buyer_id
ON buyers.buyer_id = sales.buyer_id
buyers sales
buyer_name
buyer_name buyer_id
buyer_id buyer_id
buyer_id prod_id
prod_id qty
qty
Adam
AdamBarr
Barr 11 11 22 15
15
Sean
SeanChai
Chai 22 11 33 55
Eva
EvaCorets
Corets 33 44 11 37
37
Erin
ErinO’Melia
O’Melia 44 33 55 11
11
Result 44 22 1003
1003
buyer_name
buyer_name buyer_id
buyer_id qty
qty
Adam
AdamBarr
Barr 11 15
15
Adam
AdamBarr
Barr 11 55
Erin
ErinO’Melia
O’Melia 44 37
37
Eva
EvaCorets
Corets 33 11
11
Erin
ErinO’Melia
O’Melia 44 1003
1003
Sean
SeanChai
Chai NULL
NULL NULL
NULL
Full Outer Join

A Full Outer Join statement retrieves all records from both


sides of the relationship.
For any record on either side that has no matching record
NULL is displayed.
Cross Join

A Cross Join produces the combination of all rows in the


joined tables.
When this occurs, every possible relationship between the
two table is executed.
The Output of such join is called a Cartesian Product.
Cross Join can be successfully used for generating large
amounts of sample data for testing database
performance.
Using Cross Joins
USE
USE joindb
joindb
SELECT
SELECT buyer_name,
buyer_name, qty
qty
FROM
FROM buyers
buyers CROSS
CROSS JOIN
JOIN sales
sales
buyers sales Result
buyer_id
buyer_id buyer_name
buyer_name buyer_id
buyer_id prod_id
prod_id qty
qty buyer_name
buyer_name qty
qty
11 Adam
AdamBarr
Barr 11 22 15
15 Adam
AdamBarr
Barr 15
15
22 Sean
SeanChai
Chai 11 33 55 Adam
AdamBarr
Barr 55
33 Eva
EvaCorets
Corets 44 11 37
37 Adam
AdamBarr
Barr 37
37
44 Erin
ErinO’Melia
O’Melia 33 55 11
11 Adam
AdamBarr
Barr 11
11
44 22 1003
1003 Adam
AdamBarr
Barr 1003
1003
Sean
SeanChai
Chai 15
15
Sean
SeanChai
Chai 55
USE
USE joindb Sean
joindb SeanChai
Chai 37
37
SELECT
SELECT buyer_name,
buyer_name, qty
qty Sean
FROM buyers, sales SeanChai
Chai 11
11
FROM buyers, sales Sean
SeanChai
Chai 1003
1003
Eva
EvaCorets
Corets 15
15
Eva
EvaCorets
Corets 55
...... ......
Joining More Than Two Tables
SELECT
SELECT buyer_name,
buyer_name, prod_name,
prod_name, qty
qty
FROM buyers JOIN sales
FROM buyers JOIN sales
ON
ON buyers.buyer_id
buyers.buyer_id == sales.buyer_id
sales.buyer_id
JOIN produce
JOIN produce
ON
ON sales.prod_id
sales.prod_id == produce.prod_id
produce.prod_id

buyers sales produce


buyer_id
buyer_id buyer_name
buyer_name buyer_id
buyer_id prod_id
prod_id qty
qty prod_id
prod_id prod_name
prod_name
11 Adam
AdamBarr
Barr 11 22 15
15 11 Apples
Apples
22 Sean
SeanChai
Chai 11 33 55 22 Pears
Pears
33 Eva
EvaCorets
Corets 43
3 11 37
37 33 Oranges
Oranges
44 Erin
ErinO’Melia
O’Melia 44
3 55 11
11 44 Bananas
Bananas
22
4 22 1003
1003 55 Peaches
Peaches
Result
buyer_name
buyer_name prod_name
prod_name qty
qty
Erin
ErinO’Melia
O’Melia Apples
Apples 37
37
Adam
AdamBarr
Barr Pears
Pears 15
15
Erin
ErinO’Melia
O’Melia Pears
Pears 1003
1003
Adam
AdamBarr
Barr Oranges
Oranges 55
Eva
EvaCorets
Corets Peaches
Peaches 11
11
Self Join – Joining a Table to Itself

If a Table is joined to itself, is called as Self join.


It compares values with a column of a single table.
The following could be the Situations for Self Joins :

 Find out the employees who have same Joining date.


 Find the largest or smallest value available.
Joining a Table to Itself
USE
USE joindb
joindb
SELECT
SELECT a.buyer_id
a.buyer_id ASAS buyer1,
buyer1, a.prod_id,
a.prod_id,
b.buyer_id AS buyer2
b.buyer_id AS buyer2
FROM
FROM sales aa JOIN
sales JOIN sales
sales bb
ON
ON a.prod_id
a.prod_id == b.prod_id
b.prod_id
WHERE
WHERE a.buyer_id
a.buyer_id >> b.buyer_id
b.buyer_id
sales a sales b
buyer_id
buyer_id prod_id
prod_id qty
qty buyer_id
buyer_id prod_id
prod_id qty
qty
11 22 15
15 11 22 15
15
11 33 55 11 33 55
44 11 37
37 44 11 37
37
33 55 11
11 33 55 11
11
44 22 1003
1003 44 22 1003
1003
Result
buyer1
buyer1 prod_id
prod_id buyer2
buyer2
44 22 11
Natural Join

A join that restricts the Redundant Column data from the


result set is known as Natural Join.
If one of these columns is eliminated, then join is called
the Natural Join.
Select c.Course_name,s.Student_ID,s.Student_name
From Course c,Student s, Registration r
Where s.student_id Like '%[125]' And
s.student_id=r.Student_ID
Student_Id of Registration table is not used because it is
common to both the tables ( student and Registration).
Equi, Non-Equi and Theta Joins

If the Comparison Operator is Equality(=) then the join is called


an Equi Join.
If the Comparison Operator is not the equal sign then it is a
Non-Equi Join.

SELECT p.pub_name, p.state, a.au_lname, a.au_fname, a.state

FROM publishers p INNER JOIN authors a ON a.state > p.state

A join based on any other comparison operator


( < ,>,<=,>=,<>) is a Theta ( θ ) Join.
Union
The UNION operator allows you to combine the results
of two or more set of rows into a single result set. …

Table1 Table2

ColumnA ColumnB ColumnC ColumnD


------- --- ------- ---
abc 1 ghi 3
def 2 jkl 4
ghi 3 mno 5

Whereas, a Join combines two sets of columns into


a single result set.
SELECT * FROM Table1
UNION [ALL]
SELECT * FROM Table2
Result Set :
ColumnA ColumnB
------- --------
abc 1
def 2
ghi 3
Jkl 4
Mno 5
Check Your Understanding.
Q.1. ______________ is a query in which data is retrieved
from more than one table.

1. Union
2. Join
3. Subquery
4. None
Q.2. The Column names in the select list need not
to be qualified with their table names if there
are no columns with the same name in both
tables.

1. True
2. False
Q.3. When the comparison operator of a join is equality,
hat join is called ?

1. Equality Join
2. Equi join
3. Natural join
4. Outer join
Q.4. What is the result of the following query .?
Select emp.*,dept.*
From emp,dept
Where emp.deptid=dept.deptid

1. All columns from emp table


2. All columns from dept table
3. All columns from emp and dept tables.
4. None of the above.
Q.5. What is Natural Join.?
Q.6. The following is an example of a natural join since
the column Deptid of the Department table is omitted
from the select list.?

Select E.empno,E.name,E.deptid,D.name,D.manager
From employee E, department D
Where E.deptid= D.deptid

1. True
2. False
Q.7. What is Self Join .?
Q.8. The following is an example of a self join.?

Select distinct E1.name,E1.dt_join


From Employee E1, Employee E2
Where E1.dt_join=e2.dt_join

1. True
2. False
Q.9. ___________ is a table that contains all the
possible combinations of rows.?

1. Result set
2. Combination set
3. Combination Product
4. Cartesian Product
Q.10. The following query will result in a cartesian
product.?

Select Employee.*,Salary.*
From Employee, Salary

1. True
2. False
Q.11. What is Outer Join and what are the types.?
Q.12. What is the difference between Inner, outer and
cross joins .?
Q.13.What is Union Operator.?
Q.14. For Union compatibility the two tables must contain
the same number of columns.?
1. True
2. False
Q.15. What is the difference between Join and Union .?
Overview

 Introduction to Joins
 Using Inner Joins
 Using Outer Joins
 Using Cross Joins
 Joining More Than Two Tables
 Joining a Table to Itself
 Natural Join
 Equi, Non-Equi and Theta Join
 Union
The Journey of
Thousand miles,
begin with one Step

Thank You.

Anda mungkin juga menyukai