You are on page 1of 22

IT22013

Database Systems

Lesson 05: How to retrieve data from


two or more tables

By
S. Sabraz Nawaz
Senior Lecturer in MIT
FMC, SEUSL
Joins
• You have focused primarily on retrieving data from a single
table so far. In practice, it is highly unlikely that your queries will
reference just one table—most of the time; you will be
required to return data from multiple tables.
• To do this, you use the JOIN operator. In this chapter, we will
focus on the three most commonly used:
o INNER
o LEFT OUTER
o RIGHT OUTER

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 2
Using INNER JOIN
• INNER JOIN is an equality match between two or more tables. For example,
assume you have a table that contains products and another that contains
sales and you want to find only the products that have been sold. Basically,
you are looking for the intersection of the two tables on some value.
• Figure illustrates the intersection.

• The shaded section of figure depicts the rows that will be returned from a
query that would join the Sales and Product tables.

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 3
How to code an inner join
 A join is used to combine columns from two or more tables into a
result set based on the join conditions you specify.
 For an inner join, only those rows that satisfy the join condition are
included in the result set.
 A join condition names a column in each of the two tables involved
in the join and indicates how the two columns should be compared.
 In most cases, you’ll join two tables based on the relationship
between the primary key in one table and a foreign key in the other
table.
 You can also join tables based on relationships not defined in the
database. These are called ad hoc relationships.
 If the columns in a join condition have the same name, you have to
qualify them with the table names. To code a qualified column
name, type the table name, a period, then the column name.

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 4
The explicit syntax for an
inner join
SELECT select_list
FROM table_1
[INNER] JOIN table_2 ON join_condition_1
[[INNER] JOIN table_3 ON join_condition_2]...

Notes
The INNER keyword is optional and is seldom used.

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 5
Vendors and Invoices
tables INNER JOINED
Select Vendors.VendorID, VendorName, InvoiceDate, InvoiceTotal
From Vendors join Invoices
on Vendors.vendorid = Invoices.VendorId

Ex: Write a query to display VendorName, InvoiceID, InvoiceDate,


InvoiceTotal

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 6
When and how to use
correlation names
• Correlation names also called as table alias are
temporary table names assigned in the FROM clause.
You can use them when long table names make
qualified column names long or confusing.
• If you assign a correlation name to a table, you must use
that name to refer to the table within your query. You
can’t use the original table name.
• Although the AS keyword is optional, it’s a good idea to
use it because it makes the FROM clause easier to read.
• You can use a correlation name for any table in a join
without using correlation names for all of the other
tables.
• Use correlation names whenever they simplify or clarify
the query. Avoid using correlation names when they
make a query more confusing or difficult to read.

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 7
The syntax for an inner join
that uses correlation names
SELECT select_list
FROM table_1 [AS] n1
[INNER] JOIN table_2 [AS] n2
ON n1.column_name operator n2.column_name
[[INNER] JOIN table_3 [AS] n3
ON n2.column_name operator n3.column_name]...

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 8
Inner join with
correlation names
Select VendorName, InvoiceNumber, InvoiceTotal, InvoiceDueDate
From Vendors as V JOIN Invoices as I
ON V.VendorID = I.VendorID
ORDER BY InvoiceDueDate DESC

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 9
Inner join with condition
• Write an SQL query that displays VendorName,
InvoiceNumber, InvoiceDueDate for invoices with
Invoicetotal more than 1000 and arrange the result set by
VendorName in ascending order.

Select VendorName, InvoiceNumber, InvoiceDueDate


From Vendors AS V Join Invoices I
On V.VendorID = I.VendorID
Where InvoiceTotal > 1000
Order By VendorName

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 10
Inner joins that join more
than two tables
• You can think of a multi-table join as a series of two-
table joins proceeding from left to right.

• The first two tables are joined to produce an interim


result set or interim table. Then, the interim table is
joined with the next table, and so on.

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 11
Three Tables
SELECT VendorName, InvoiceNumber, InvoiceLineItemAmount
FROM Vendors
JOIN Invoices ON Vendors.VendorID = Invoices.VendorID
JOIN InvoiceLineItems ON Invoices.InvoiceID =InvoiceLineItems.InvoiceID
ORDER BY VendorName

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 12
Outer Join

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 13
Using OUTER JOINs
• There are two basic types of OUTER JOINs:
LEFT and RIGHT.
• They both provide very similar function-
ality, but there is a slight difference that
depends on the order of the tables in the
query.
• Regardless of the type of OUTER JOIN, the
syntax is similar to that of an INNER JOIN.
You replace INNER with either LEFT OUTER A RIGHT OUTER JOIN
or RIGHT OUTER. intersection

What outer joins do


Joins of this type Keep unmatched rows from
Left outer join The first (left) table
Right outer join The second (right) table
Full outer join Both tables
ITM22013 Database Systems, By: S. Sabraz Nawaz
Slide 14
The explicit syntax for an
outer join
SELECT select_list
FROM table_1
{LEFT|RIGHT|FULL} [OUTER] JOIN table_2
ON join_condition_1
[{LEFT|RIGHT|FULL} [OUTER] JOIN table_3
ON join_condition_2]...

How to code an outer join


• An outer join retrieves all rows that satisfy the join condition, plus
unmatched rows in one or both tables.
• In most cases, you use the equal operator to retrieve rows with
matching columns.
• When a row with unmatched columns is retrieved, any columns
from the other table that are included in the result set are given null
values.

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 15
A SELECT statement that
uses a left outer join
SELECT VendorName, InvoiceNumber, InvoiceTotal
FROM Vendors LEFT JOIN Invoices ON Vendors.VendorID = Invoices.VendorID
ORDER BY VendorName

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 16
The Departments table The Employees table

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 17
A left outer join
SELECT DeptName, Departments.DeptNo, LastName
FROM Departments LEFT JOIN Employees
ON Departments.DeptNo = Employees.DeptNo

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 18
A right outer join
SELECT DeptName, Employees.DeptNo, LastName
FROM Departments RIGHT JOIN Employees
ON Departments.DeptNo = Employees.DeptNo

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 19
A full outer join
SELECT DeptName, Departments.DeptNo,
Employees.DeptNo, LastName
FROM Departments FULL JOIN Employees
ON Departments.DeptNo = Employees.DeptNo

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 20
Assignment
1. What is a Cross Join? Using the tables in the
Accounts Payable database, clearly explain with
SQL Script and its corresponding output.

2. Explain with example how the SQL UNION is used.

ITM22013 Database Systems, By: S. Sabraz Nawaz


Slide 21
Reference:
Bryan Syverson & Joel Murach. Murach’s SQL Server 2016
for Developers. Mike Murach & Associates, Inc. (p. 125-157)

ITM22013 Database Systems, By: S. Sabraz Nawaz 22

You might also like