Professional Documents
Culture Documents
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
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
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.
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
Table1 Table2
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
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.?
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.