2. OVERVIEW
In this subject, you will learn how to retrieve data from multiple
tables using one SQL statement. You will see how tables can be
joined together and how similar results are obtained using
different approaches, including the joints and sub queries.
It is important that you understand how to query multiple tables
for generating appropriate reports in the creation of an
information system.
3. After studying our lesson, you should be able to:
• Learn the capabilities of SQL, in querying multiple tables by
joining these tables.
• Connect Database to Visual Basic
• Create a simple Database System
4. JOINING MORE THAN ONE TABLES
You already learned from your previous database course how to generate
reports from a single table. This time we will explore the capabilities of
SQL, in querying multiple tables by joining these tables.
Joining can be performed by combining two or more tables together by
finding rows that match the values in the common columns. These
common columns in joined tables are usually the primary key of the
parent table and the foreign key of the child table in a one-to-many
relationships.
4
6. JOIN typically combines rows with equal values for the specified columns.
Usually, one table contains a primary key, which is a column or columns
that uniquely identify rows in the table (the cat_id column in the cat table).
The other table has a column or columns that refer to the primary key
columns in the first table (the cat_id column in the toy table). Such
columns are foreign keys.
The JOIN condition is the equality between the primary key columns in one
table and columns referring to them in the other table.
7. JOIN returns all rows that match the ON condition. JOIN is also
called INNER JOIN.
It is when you want to return only records having pair on both sides.
Example syntax:
SELECT *
FROM Toy
JOIN Cat
ON TOY.CAT_ID = CAT.CAT_ID;
7
9. There is also another, older syntax, but it isn't recommended. List joined
tables in the FROM clause and place the conditions in the WHERE clause.
SELECT *
FROM Toy, Cat
WHERE Toy.Cat_ID = Cat.Cat_ID;
9
10. JOIN CONDITIONS
10
The JOIN condition doesn't have to be an equality – it can be any
condition you want. JOIN doesn't interpret the JOIN condition; it
only checks if the rows satisfy the given condition. To refer to a
column in the JOIN query, you have to use the full column
name: first the table name, then a dot (.) and the column name:
ON Cat.Cat_ID = Toy.Cat_ID
11. JOIN CONDITIONS
11
The JOIN condition doesn't have to be an equality – it can be any condition you want.
JOIN doesn't interpret the JOIN condition; it only checks if the rows satisfy the given
condition. To refer to a column in the JOIN query, you have to use the full column
name: first the table name, then a dot (.) and the column name:
ON Cat.Cat_ID = Toy.Cat_ID
You can omit the table name and use just the column name if the name of the column
is unique within all columns in the joined tables.
12. NATURAL JOIN
12
If the tables have columns with the same name, you can use NATURAL JOIN instead
of JOIN.
SELECT *
FROM Toy
NATURAL JOIN Cat;
The common column appears only once in the result table.
Note: NATURAL JOIN is rarely used in real life.
13. LEFT JOIN
13
LEFT JOIN returns all rows from the left table with matching rows from the right table.
Rows without a match are filled with NULLs. LEFT JOIN is also called LEFT OUTER
JOIN.
When you need all records from the left table no matter if they have pair in the right
table or not.
SELECT *
FROM Toy
LEFT JOIN Cat
ON Toy.Cat_ID=Cat.Cat_ID;
15. 15
RIGHT JOIN
RIGHT JOIN returns all rows from the right table with matching rows from the left table.
Rows without a match are filled with NULLs. RIGHT JOIN is also called RIGHT OUTER
JOIN.
SELECT *
FROM Toy
RIGHT JOIN Cat
ON Toy.Cat_ID= Cat.Cat_ID;
16. 16
FULL JOIN (FULL OUTER JOIN)
FULL JOIN returns all rows from the left table and all rows from the right table. It fills
the non-matching rows with NULLs. FULL JOIN is also called FULL OUTER JOIN.
SELECT *
FROM Toy
FULL JOIN Cat
ON Toy.Cat_ID = Cat.Cat_ID;
Note: Full Outer Join in MySQL
• MySQL doesn’t have FULL OUTER JOIN
• Simulate using LEFT and RIGHT JOINs
17. 17
FULL JOIN (FULL OUTER JOIN)
Example Syntax:
SELECT *
FROM Toy
LEFT JOIN Cat
ON Toy.Cat_ID= Cat.Cat_ID
UNION SELECT *
FROM Toy
RIGHT JOIN Cat
ON Toy.Cat_ID= Cat.Cat_ID
18. 18
CROSS JOIN
CROSS JOIN returns all possible combinations of rows from the left and right tables.
Example Syntax:
SELECT *
FROM Toy
CROSS JOIN Cat;
Other syntax:
SELECT *
FROM Toy, Cat;
20. COLUMN AND TABLE ALIASES
20
Aliases give a temporary name to a table or a column in a table.
Mom_ID Owner_ID
Owner As o
Owner_ID Owner_Name
1 John Smith
2 Danielle Davis
21. 21
A column alias renames a column in the result. A table alias renames a table within the
query. If you define a table alias, you must use it instead of the table name everywhere in
the query. The AS keyword is optional in defining aliases.
Example Syntax:
SELECT o.owner_name, c.cat_name
FROM cat AS c
JOIN owner AS o
ON c.owner_id = o.owner_id;
22. 22
SELF JOIN
You can join a table to itself, for example, to show a parent-child relationship.
Mom_ID Owner_ID Mom_ID Owner_ID
23. 23
Each occurrence of the table must be given a different alias. Each column reference must
be preceded with an appropriate table alias.
Example Syntax:
SELECT child.cat_name AS child_name, mom.cat_name AS mom_name
FROM cat AS child
JOIN cat AS mom
ON child.mom_id = mom.cat_id;
24. 24
MULTIPLE JOINS
To perform joining multiple tables, you need to understand the SQL JOIN
concepts clearly. You need to understand how different joins such as INNER
JOIN, LEFT JOIN, RIGHT JOIN, CROSS JOIN, and FULL JOIN work.
Moreover, understanding table relationships such as one-to-one, one-to-many,
and many-to-many as well as primary key and foreign key will help to understand
this topic. So, make sure that you have a good understanding of these points.
25. 25
MULTIPLE JOINS
You can join more than two tables together. First, two tables are joined, then the third
table is joined to the result of the previous joining.
Owner As o
Owner_ID Owner_Name
1 John Smith
2 Danielle Davis
26. 26
JOIN & JOIN
Example Syntax:
SELECT t.toy_name, c.cat_name, o.owner_name
FROM toy t
JOIN cat c
ON t.cat_id = c.cat_id
JOIN owner o
ON c.owner_id = o.owner_id;
27. 27
JOIN & LEFT JOIN
Example Syntax:
SELECT t.toy_name, c.cat_name, o.owner_name
FROM toy t
JOIN cat c
ON t.cat_id = c.cat_id
LEFT JOIN owner o
ON c.owner_id = o.owner_id;
28. 28
LEFT JOIN & LEFT JOIN
Example Syntax:
SELECT t.toy_name, c.cat_name, o.owner_name
FROM toy t
LEFT JOIN cat c
ON t.cat_id = c.cat_id
LEFT JOIN owner o
ON c.owner_id = o.owner_id;
30. 30
OrderID PK Order_Name CustomerID
FK
10308 Keyboard 1001
10309 Mouse 1002
10310 Mouse Null
10311 Monitor 1004
Table: Orders
CustomerID CustomerName Address
1001 Jose Bautista Taguig City
1002 Allan Williams Pasay City
1003 Anna Marie Ruiz Mandaluyong City
Table: Customers
Use the following Join Statements:
1. Natural Join
2. Left Join
3. Right Join
4. Full Join
5. Cross Join
31. 31
OrderID PK OrderItem FK CustomerID SupplierID FK OrderDate
10308 Mouse 1001 1014 2023-08-20
10309 Keyboard 1002 1037 2023-08-23
10310 Monitor 1003 1022 2023-08-29
Table: Orders
CustomerID PK CustomerName Address
1001 Jose Bautista Taguig City
1002 Allan Williams Pasay City
1003 Anna Marie Ruiz Mandaluyong City
Table: Customers
SupplierID PK SupplierName Address
1014 Electronico PPC
1037 Du Ek Sam Narra
1022 Teslinque PPC
Table: Suppliers
Use the following Join
Statements
1. Join & Join
2. Join & Left Join
3. Left join & Left Join
32. REFERENCES
32
• Hoffer, J. Rames, V., Topi, H. (2013) Modern Database Management, 11th Ed
New Jersey Pearson Education, Inc
• Price, J. (2008) Oracle Database l1g SQL: Master SOL and PL QL in the
Oracle Database. New York: McGraw-Hill.
• AMA Online University MIT 412. Advanced Database Systems