You are on page 1of 5

Laboratory Exercise 5

SQL Join Clauses


Objectives:

At the end of the exercise, the students should be able to:

1. use different SQL join clauses.

Materials:
  PC with installed Microsoft Access 2007 (or later)

 
Flash drive

Procedures:

In this laboratory exercise, we will create and use the tblCustomers and tblItems_Ordered tables in
OrderDB database.

1. Open Microsoft Access, Create OrderDB

2. Create the tblCustomers (disregard the number on the left)

Table 1 tblCustomers
Table 2 tblItems_Ordered

3. Open a New Query editor to combine the two tables using the INNER JOIN as follows:

SELECT tblCustomers.CustomerID, tblCustomers.FirstName,


tblCustomers.LastName, tblItems_Ordered.Item, tblItems_Ordered.Quantity
FROM tblCustomers
INNER JOIN tblItems_Ordered
ON tblCustomers.CustomerID =
tblItems_Ordered.CustomerID

4. The SQL above would produce the following result. Name the query as “Inner Join”.

Table 3 INNER JOIN query result


Observe what happened when two tables joined using the INNER JOIN clause.

1. Write your observation on a sheet of yellow paper


2. Considering the two tables above (tblCustomers and tblItems_Ordered), what do you
think will happen to the information on the first and second table if combined?
3. What will happen to the customers who doesn’t have items ordered?
4. What will happen to the items which wasn’t ordered?

5. Consider the two tables above, now, join the tables using LEFT JOIN. Follow the SQL below, Name
the query as “LEFT JOIN”

SELECT tblCustomers.CustomerID, tblCustomers.FirstName,


tblCustomers.LastName, tblItems_Ordered.Item, tblItems_Ordered.Quantity
FROM tblCustomers
LEFT JOIN tblItems_Ordered
ON tblCustomers.CustomerID =
tblItems_Ordered.CustomerID

6. The query above would result the following:

Table 4 LEFT JOIN query result

5. Considering the two tables above (tblCustomers and tblItems_Ordered), what do


you think will happen to the information on the first table if combined to the second
table?
6. What will happen to the customers who doesn’t have items ordered?
7. What will happen to the items which wasn’t ordered?
7. This time use the RIGHT JOIN in combining two tables above. Copy the SQL below, Name the
query as “RIGHT JOIN”

SELECT tblCustomers.CustomerID, tblCustomers.FirstName,


tblCustomers.LastName, tblItems_Ordered.Item, tblItems_Ordered.Quantity
FROM tblCustomers
RIGHT JOIN tblItems_Ordered
ON tblCustomers.CustomerID =
tblItems_Ordered.CustomerID

8. See results below:

Table 5 RIGHT JOIN query result

8. Considering the two tables above (tblCustomers and tblItems_Ordered), what do


you think will happen to the information on the second table if combined to the first
table?
9. What will happen to the customers who doesn’t have items ordered?
10. What will happen to the items which wasn’t ordered?
Exercise

Use tblCustomers and tblItems_ordered tables in OrderDB database. Write in your Yellow Pad.

1. Create a SQL command that will display the CustomerID, LastName, Item, Quantity, and Price using
INNER JOIN clause ORDER BY LastName.

2. Create a SQL command that will display the CustomerID, LastName, Item, Quantity, and Price using
INNER JOIN clause where CustomerID = 10449 ORDER BY LastName.

3. Create a SQL command that will display the CustomerID, LastName, Item, Quantity, and Price using
LEFT JOIN clause where Price > 25 ORDER BY CustomerID.

 Challenge Exercise : FULL JOIN is not supported by MS ACCESS. FULL JOIN allows
you to combine the LEFT and RIGHT JOINS (allowing NULL values on both sides)

 Figure out an approach that will display the CustomerID, LastName, Item, Quantity, and
Price using – similar to a full join - where Item or Quantity is null ORDER BY
CustomerID.

You might also like