SQL: Data Manipulation
(2)
DATABASE SYSTEMS: SIXTH EDITION
BY: CONALLY AND BEGG
Chapter Objective
SQL JOIN
Binary operations
Obtain information from two tables:
Subquery
JOINS
Combination of Cartesian product + Selection criteria
A Join operation pairs two tuples from different relations, if and only if a given join condition is
satisfied
Similar to Cartesian product except the fact that in Cartesian product, we get all the possible
combinations of relations while in join only those combinations can be formed that satisfies
some matching conditions
Joins Example Foreign
Primary Key
◦ EMPLOYEE Key DEPARTMENT
Emp_Id Emp_Name Address Dep_Id Dep_Name Emp_Id
1 Subhan Multan D1 HR 1
2 Sikandar Sargodha D2 IT 2
3 Sultan Faisalabad D3 Marketing 4
4 Salar Karachi D4 Finance 5
5 Ahmad Lahore D5 HR 3
Find emp_name from Employee who is working in HR Department
CLASSIFICATION OF JOINS
CROSS JOIN (Cartesian Product)
The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the
number of rows in the second table
Syntax
SELECT *
FROM table1
CROSS JOIN table2;
The number of rows in a CROSS JOIN is the product of the number of rows in the contributing tables.
3x3=9
1,000 x 1,000 = 1,000,000
100,000 x 100,000 = 10,000,000,000
A CROSS JOINis rarely the desired result of a query
CROSS
JOIN
EXAMPL
E
SQL JOINS
The SQL join operation combines information from two tables by forming pairs of related rows
from the two tables.
The row pairs that make up the joined table are those where the matching columns in each of
the two tables have the same value.
Steps followed by join operations
Cartesian Product
Certain Selection Criteria
Remove Duplicates
JOIN Statement
More than one table name in the FROM clause
WHERE clause to specify the join column(s)
Alias
Possible to use an alias for a table named in the FROM clause
An alias can be used to qualify a column name whenever there is ambiguity regarding the
source of the column name
EXAMPLE:
SELECT fName, lName, City
FROM Branch as b, Staff as s
WHERE b.branchNo=s.branchNo
JOIN EXAMPLE
Matching Attributes:
Branch.branch No (Primary Key)
Staff.branchNo (Foreign Key)
Parent Child Relationship
Example: List names of employees who work
in any branch along with its city.
SELECT fName, lName, City
FROM Branch as b,Staff as s
WHERE b.branchNo=s.branchNo
INNER JOIN
INNER JOIN/EQUI JOIN
Simple JOIN Example: (Equivalent to EquiJoin in Relational Algebra)
EXAMPLE: List the names of all clients who have viewed a property, along with any comments
supplied.
SELECT c.clientNo, fName, IName, SELECT c.clientNo, fName, IName,
propertyNo, comment propertyNo, comment
FROM Client c, Viewing v FROM Client JOIN
WHERE c.clientNo = v.clientNo; Viewing ON c.clietNo= v.clientNo
Output
NATURAL JOIN
The SQL NATURAL JOIN is a type of INNER JOIN and is structured in such a way that, columns
with the same name of associated tables will appear once only
Natural Join: Guidelines
- The associated tables have one or more pairs of identically named columns.
- The columns must be the same data type.
- Don’t use ON clause in a natural join.
Syntax
SELECT *
FROM table1
NATURAL JOIN table2;
NATURAL
JOIN
EXAMPLE
Inner Join vs Natural Join
Compare Inner Joins And Outer Joins
The following table is a comparison of inner and outer join syntax and limitations:
Key Point Inner Join Outer Join
Table Limit 256 2
Join Behavior Returns matching rows Returns matching and
only nonmatching rows
Join Options Matching rows only LEFT, FULL, RIGHT
Syntax changes Multiple tables in the ON clause that
FROM clause specifies join criteria
WHERE clause that
specifies join criteria
17
Self Join
A self join is a join in which a table is joined with itself (which is also called Unary relationships)
To join a table itself means that each row of the table is combined with itself and with every
other row of the table.
SYNTAX: The syntax of the command for joining a table to itself is almost same as that for
joining two different tables.
To distinguish the column names from one another, aliases for the actual the table name are
used, since both the tables have the same name
SYNTAX: SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;
SORTING A JOIN
Ordered the output of JOIN using ORDER BY , to make results more readable.
Major Sort Key vs Minor Sort Key
EXAMPLE: For each branch office, list the staff numbers and names of staff who manage properties and the
properties that they manage
SELECT s.branchNo, s.staffNo, fName, IName, propertyNo
FROM Staff s, PropertyForRent p
WHERE s.staffNo = p.staffNo
ORDER BY s.branchNo, s.staffNo, propertyNo;
Result:
Three-table join
EXAMPLE: For each branch, list the staff numbers and names of staff who manage properties,
including the city in which the branch is located and the properties that the staff manage.
SELECT b.branchNo, b.city, s.staffNo, fName, IName, propertyNo
FROM Branch b, Staff s, PropertyForRent p
WHERE b.branchNo = s.branchNo AND s.staffNo = p.staffNo
ORDER BY b.branchNo, s.staffNo, propertyNo;
RESULT:
EXAMPLES OF
INNER JOIN
INNER JOIN: EXAMPLE
SELECT b.*, p.* SELECT b.*, p.*
FROM Branch1 b, PropertyForRent1 p FROM Branch1 b INNER JOIN PropertyForRent1 p
WHERE b.bCity = p.pCity ON b.bCity = p.pCity
SUBQUERY VS JOIN
SUBQUERY JOIN JOIN
SELECT staffNo, fName, lName, SELECT staffNo, fName, IName, SELECT staffNo, fName, lName,
position position position
FROM Staff s FROM Staff s, Branch b FROM staff s INNER JOIN Branch b ON
WHERE EXISTS (SELECT * WHERE s.branchNo = b.branchNo s.branch=b.branch
FROM Branch b AND city = ‘London’; WHERE city= ‘London’;
WHERE s.branchNo =
b.branchNo AND city = 'London')
OUTER JOIN
OUTER JOIN
The SQL OUTER JOIN returns all rows from both the participating tables which satisfy the join
condition along with rows which do not satisfy the join condition.
The SQL OUTER JOIN operator (+) is used only on one side of the join condition only.
The subtypes of SQL OUTER JOIN
LEFT OUTER JOIN or LEFT JOIN
RIGHT OUTER JOIN or RIGHT JOIN
FULL OUTER JOIN
Syntax: Select *
FROM table1, table2
WHERE conditions [+];
LEFT OUTER JOIN
All the tuples of left table is displayed irrespective of whether it satisfies the matching
conditions.
Thus, in the left all the tuples have been displayed but, in the right, only those are present that
satisfy the matching conditions.
Syntax: SELECT *
FROM table1
LEFT [ OUTER ] JOIN table2
ON table1.column_name=table2.column_name;
LEFT OUTER JOIN
EXAMPLE: List all branch offices and any properties that are in the same city.
SELECT b.*, p.*
FROM Branch1 b LEFT JOIN PropertyForRent1 p ON b.bCity = p.pCity;
INPUT
OUTPUT
RIGHT OUTER JOIN
All the tuples of right table are displayed irrespective of whether it satisfies the matching
conditions or not.
Thus, in the right, all the tuples have been displayed but, in the left, only those are present that
satisfy the matching conditions
Syntax: SELECT *
FROM table1
RIGHT [ OUTER ] JOIN table2
ON table1.column_name=table2.column_name;
RIGHT OUTER JOIN
EXAMPLE: List all properties and any branch offices that are in the same city.
SELECT b.*, p.*
FROM Branch1 b RIGHT JOIN PropertyForRent1 p ON b.bCity = p.pCity;
INPUT
OUTPUT
FULL OUTER JOIN
Tuples from both the relations takes part irrespective of whether it has the matching or non-
matching conditions
SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
FULL OUTER JOIN
FULL OUTER JOIN
List the branch offices and properties that are in the same city along with any unmatched
branches or properties.
SELECT b.*, p.*
FROM Branch1 b FULL JOIN PropertyForRent1 p ON b.bCity = p.pCity;
INPUT
OUTPUT
SQL
JOINS
QUESTIONS!!