Part 5 - Joining Tables - Inner Join

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Database Concepts and Skills for Big

Data
Le Duy Dung (Andrew)
SQL – Part 5
Joining Tables – Inner Join
S Q L Art Course Database Example

• Using SQL to recreate the given list


S Q L Art Course Database Example

• Using SQL to recreate the given list

Source: Figure 1-15: : Database Concepts (Part I – Chapter 1)


Why use Joins?
• Breaking data into multiple tables enables more efficient storage,
easier manipulation, and greater scalability.

• Questions: If data is stored in multiple tables, how can we retrieve that.


data with a single SELECT statement?
Ø The answer is to use a join, which is a mechanism used to associate, or join,
tables within a SELECT statement.
Ø You can join multiple tables so that a single set of output is returned and the
join associates the correct rows in each table on the fly.
Creating a Join
• You must specify all the tables to be included and how they are
related to each other
<Example>
SELECT ItemDescription, ItemCost, CompanyName
FROM ITEM, VENDOR
WHERE ITEM.VendorID = VENDOR.VendorID;
SELECT ItemDescription, ItemCost, CompanyName

Creating a Join FROM ITEM,VENDOR


WHERE ITEM.VendorID = VENDOR.VendorID;

• The SELECT statement starts by specifying the columns


ItemDescription, ItemCost from one table ITEM and the column
CompanyName from another table VENDOR.

• The FROM clause has two tables, that are being joined in this SELECT
statement.
SELECT ItemDescription, ItemCost, CompanyName

Creating a Join FROM ITEM,VENDOR


WHERE ITEM.VendorID = VENDOR.VendorID;

• The tables are correctly joined with a WHERE clause that instructs
the DBMS to match VendorID in the ITEM table with VendorID in the
VENDOR table.

• Note that the columns are specified as: ITEM.VendorID and


VENDOR.VendorID. This fully qualified column name is required as the
DBMS can not tell which VendorID columns you are referring to.
The importance of WHERE clause
• When you join two tables, you are actually pairing every row in the
first table with every row in second table.

• The WHERE clause acts as a filter to include only the rows that match
the join condition.

• Without the WHERE clause, every row in the first table will be paired
with every row of second table. (Try this yourself!)
Inner Joins
• The example join earlier is called an equijoin- a join based on the
testing of equality between two tables. This kind of join also called an
inner join.
• In fact, you may use a slightly different syntax for these joins,
specifying the type of join explicitly.
<Example>
SELECT ItemDescription, ItemCost, CompanyName
FROM ITEM INNER JOIN VENDOR
ON Item.VendorID = Vendor.VendorID;
Joining multiple tables
• SQL imposes no limit to the number of tables that may be joined in
a SELECT statement.
• The basic rules for creating the join remain the same. First, list all the
tables, and then define the relationship between each.
<Example>
SELECT SaleID, Customer.FirstName AS CustFirstName ,
Employee .FirstName AS EmpFirstName , Total
FROM SALE, CUSTOMER, EMPLOYEE
WHERE SALE.CustomerID = CUSTOMER.CustomerID
AND SALE.EmployeeID = EMPLOYEE.EmployeeID
AND Total > 1000;
Joining multiple tables
• Remember subqueries?
<Example>
SELECT * FROM ITEM
WHERE ItemID IN (SELECT ItemID
FROM SALE_ITEM
WHERE SaleID IN (SELECT SaleID
FROM SALE
WHERE EmployeeID = 2));

Can you use join SQL quer y to get the same results?
Creating advanced joins
• Using table aliases
SELECT ItemDescription, ItemCost, VendorID
FROM ITEM as I, SALE as S, SALE_ITEM as SI
WHERE S.EmployeeID = 2 AND S.SaleID = SI.SaleID AND SI.ItemID = I.ItemID;

• To shorten the SQL syntax


• To enable multiple uses of the same table within a single SELECT
statement
Creating advanced joins
• Self Join
Let’s imagine you want to send an email to all customers who are from the same city
as the customer with the phone number is 206-524-6877.

Using subqueries
SELECT CustomerID, LastName, FirstName, City
FROM CUSTOMER
WHERE City in (SELECT City FROM CUSTOMER
WHERE Phone = ‘206-524-6877’);
Creating advanced joins
• Self Join
Let’s imagine you want to send an email to all customers who are from the same city
as the customer with the phone number is 206-524-6877.

Using subqueries
SELECT CustomerID, LastName, FirstName, City
FROM CUSTOMER
WHERE City in (SELECT City FROM CUSTOMER
WHERE Phone = ‘206-524-6877’);
Creating advanced joins
• Self Join
Let’s imagine you want to send an email to all customers who are from the same city
as the customer with the phone number is 206-524-6877.

Using self join:


SELECT c1.CustomerID, c1.LastName, c1.FirstName, c1.City
FROM CUSTOMER as c1, CUSTOMER as c2
WHERE c1.City = c2.City AND c2.Phone = ‘206-524-6877’;
QnA!

You might also like