SQL: Data Manipulation (2) : Database Systems: Sixth Edition By: Conally and Begg
SQL: Data Manipulation (2) : Database Systems: Sixth Edition By: Conally and Begg
(2)
DATABASE SYSTEMS: SIXTH EDITION
BY: CONALLY AND BEGG
Chapter Objective
SQL JOIN
Binary operations
Obtain information from two tables:
Subquery
JOINS
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:
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!!