Bcis5420 - Lecture Note - ch3 - Multi Table Queries

You might also like

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

Ch.3.

Multi Table Queries

Chapter 3

Multi Table Queries


Ch.3. Multi Table Queries

Handling Business Data

• Business data are organized and stored by various attributes,


such as business functions/ regions, data confidentiality, etc.
(e.g., employee demographics data by HR department, employee salary
data by accounting)

• Therefore, data analysts often combine multiple datasets from


different databases for their projects
Ch.3. Multi Table Queries

Multi Table Queries

• Matching keys: testing a given condition using


matching columns

• Subquery: retrieving data from one table based on data


from another table(s), using IN (NOT IN), EXISTS
(NOT EXISTS), and ANY

• JOIN: retrieving data from multiple tables at the same


time (c.f., VLOOKUP in MS EXCEL)
Ch.3. Multi Table Queries

Section. 1

Subquery
Ch.3. Multi Table Queries

Subquery

• Query in query
• One query sentence belongs to another sentence
• Each query sentence has its SELECT and FROM operators
• Connecting multiple queries using parentheses

SELECT column_a, column_b FROM table_1


WHERE column_a
IN (SELECT column_a FROM table_2)

Condition statement in WHERE


Ch.3. Multi Table Queries

Subquery with IN
• Retrieve data from one table based on data from another table(s)
• Use IN to find values of rows with matching ID’s or defined values
• Query within query, working as below;

Table: Customers Table: Orders


customer_id name order_id amount customer_id
CID1 A OID1 600 CID1
CID2 B OID2 700 CID10
CID3 C OID3 900 CID2
CID4 D OID4 1000 CID3
CID5 E OID5 150 CID4

customer_id name
SELECT customer_id, name CID1 A
FROM Customers CID2 B
WHERE customer_id CID3 C
IN (SELECT customer_id FROM Orders) CID4 D
Ch.3. Multi Table Queries

Subquery with IN_ Example

• In the SQL sever, identifying customers who have placed at least one
order (similar to the example on the previous slide);

SELECT CustomerID, AccountNumber


FROM AdventureWorks.Sales.Customer
WHERE CustomerID
IN (SELECT CustomerID
FROM AdventureWorks. Sales.SalesOrderHeader)
Ch.3. Multi Table Queries

Subquery with IN + WHERE

• Adding WHERE to the script for IN to add more conditions, which


works as below;

Table: Customers Table: Orders


customer_id name order_id amount customer_id
CID1 A OID1 600 CID1
CID2 B OID2 700 CID10
CID3 C OID3 900 CID2
CID4 D OID4 1000 CID3
CID5 E OID5 150 CID4

SELECT customer_id, name customer_id name


FROM Customers CID2 B
WHERE customer_id CID3 C
IN (SELECT customer_id FROM Orders
WHERE amount > 800)
Ch.3. Multi Table Queries

Subquery with IN + WHERE_ Example

• In the SQL server, identifying individual’s information meeting


the below conditions;

▪ Select all the data of persons from Person.Person that have


phone information and PhoneNumberTypeID is smaller than 3
in Person.PersonPhone

• BusinessEntityID (i.e., matching ID) connects these two tables;

SELECT *
FROM AdventureWorks.Person.Person
WHERE BusinessEntityID
IN (SELECT BusinessEntityID
FROM AdventureWorks.Person.PersonPhone
WHERE PhoneNumberTypeID <3)
Ch.3. Multi Table Queries

Subquery with NOT IN


• Apply NOT IN to find values of rows, using matching ID’s
• Adding WHERE to the script for NOT IN to add more conditions,
which works as below;

Table: Customers Table: Orders


customer_id name order_id amount customer_id
CID1 A OID1 600 CID1
CID2 B OID2 700 CID10
CID3 C OID3 900 CID2
CID4 D OID4 1000 CID3
CID5 E OID5 150 CID4

SELECT customer_id, name


FROM Customers customer_id name
WHERE customer_id CID5 E
NOT IN (SELECT customer_id FROM Orders)
Ch.3. Multi Table Queries

Subquery with NOT IN_ Example

• In the SQL server, identifying customers WITHOUT orders in


Sales.SalesOrderHeader;

SELECT CustomerID, AccountNumber


FROM AdventureWorks.Sales.Customer
WHERE CustomerID
NOT IN (SELECT CustomerID
FROM AdventureWorks.Sales.SalesOrderHeader)
Ch.3. Multi Table Queries

Subquery with NOT IN + WHERE


• Use NOT IN with WHERE clauses

• In the SQL sever, identifying currency rate codes (i.e.,


FromCurrecnyCode and ToCurrencyCode) for not ordered invoices

SELECT CurrencyRateID, FromCurrencyCode, ToCurrencyCode


FROM AdventureWorks.Sales.CurrencyRate
WHERE CurrencyRateID
NOT IN (SELECT CurrencyRateID FROM
AdventureWorks.Sales.SalesOrderHeader
WHERE CurrencyRateID IS NOT NULL)

IS NOT NULL: operator for finding not missing values


(i.e, not empty data point)
Ch.3. Multi Table Queries

Subquery with EXISTS


• Use EXISTS to return the Boolean value TRUE or FALSE
• Use EXISTS to rows with matching ID’s, regardless if the rows have values

Table: Customers Table: Orders


customer_id name order_id amount customer_id
CID1 A OID1 600 CID1
CID2 B OID2 700 CID10
CID3 C OID3 900 CID2
CID4 D OID4 1000 CID3
CID5 E OID5 150 CID4

customer_id name
SELECT customer_id, name CID1 A
FROM Customers CID2 B
WHERE EXISTS (SELECT customer_id FROM Orders CID3 C
WHERE Orders.customer_id = Customers.customer_id)
CID4 D
Ch.3. Multi Table Queries

Subquery with EXISTS_ Example

• In the SQL server, identifying customer information for those with orders

SELECT CustomerID, AccountNumber


FROM AdventureWorks.Sales.Customer AS Cust
WHERE EXISTS (SELECT CustomerID, AccountNumber
FROM AdventureWorks.Sales.SalesOrderHeader AS SOH
WHERE SOH.CustomerID = Cust.CustomerID);

Boolean statement to test whether


CustomerID in the table SOH is equal to
CutomerID in the table Cust
Ch.3. Multi Table Queries

Subquery with NOT EXIST

• Use NOT EXIST to rows without matching ID’s, regardless if the rows
have values

• Display customer information for those without orders;

SELECT CustomerID, AccountNumber


FROM Sales.Customer AS Cust
WHERE NOT EXISTS (SELECT CustomerID, AccountNumber
FROM Sales.SalesOrderHeader AS SOH
WHERE SOH.CustomerID = Cust.CustomerID);
Ch.3. Multi Table Queries

Differences between IN and EXISTS_1


• IN scans all records fetched from the given subquery column, whereas
EXISTS clause evaluates true or false of the given Boolean statement
(thus, EXISTS is used with WHERE)

SELECT CustomerID, AccountNumber


FROM Sales.Customer
WHERE EXISTS (SELECT CustomerID
FROM Sales.SalesOrderHeader);

What is the result of this query,


WITHOUT true/false statement for EXISTS?
Ch.3. Multi Table Queries

Differences between IN and EXISTS_2

• EXISTS can only be used with subqueries, whereas IN can be used


on subqueries and values both (thus, IN is typically used to filter a column
for a certain list of values)

SELECT CustomerID, AccountNumber


FROM AdventureWorks.Sales.Customer
WHERE CustomerID IN (1,2,3)

c.f., equal to WHERE CustomerID = 1


OR CustomerID = 2 OR CustomerID = 3
Ch.3. Multi Table Queries

Subquery with ANY

• Returns TRUE if ANY of the subquery values meet the condition

Table: Customers Table: Orders


customer_id name order_id amount customer_id
CID1 A OID1 600 CID1
CID2 B OID2 700 CID10
CID3 C OID3 900 CID2
CID4 D OID4 1000 CID3
CID5 E OID5 150 CID4

SELECT customer_id, name customer_id name


FROM Customers CID2 B
WHERE customer_id = ANY (SELECT customer_id CID3 C
FROM Orders WHERE amount > 800)
Ch.3. Multi Table Queries

Subquery with ANY_ Example_1

• In the SQL server, identifying products with list price greater than
or equal to average list price of any product subcategory

SELECT Name, ListPrice


FROM AdventureWorks.Production.Product
WHERE ProductSubcategoryID IS NOT NULL
AND ListPrice >= ANY (SELECT AVG (ListPrice) AS AvgListPrice
FROM AdventureWorks.Production.Product
WHERE ProductSubcategoryID IS NOT NULL
GROUP BY ProductSubcategoryID)
Ch.3. Multi Table Queries

Subquery with ANY_ Example_2

• In the SQL server, identifying currency rate ID and their from and
to currency code, where currency rate ID is larger than 10

SELECT CurrencyRateID, FromCurrencyCode, ToCurrencyCode


FROM AdventureWorks.Sales.CurrencyRate
WHERE CurrencyRateID = ANY (SELECT CurrencyRateID
FROM AdventureWorks.Sales.CurrencyRate
WHERE CurrencyRateID > 10)
ORDER BY CurrencyRateID

IS it possible to simplify this query?


Ch.3. Multi Table Queries

Subquery in Multiple Levels

• Have more than one level of subqueries


• Often used with OR, AND, and LIKE
• Show individual’s information with Cell or Home phone number (assume
we do not know the type number for cell and home phone number);

SELECT * FROM [Person].[Person]


WHERE [BusinessEntityID] IN (SELECT [BusinessEntityID]
FROM [AdventureWorks].[Person].[PersonPhone]
WHERE [PhoneNumberTypeID] IN (SELECT
[PhoneNumberTypeID]
FROM [AdventureWorks].[Person].[PhoneNumberType]
WHERE [Name] LIKE 'Cell' OR [Name] LIKE 'Home'))
Ch.3. Multi Table Queries

Section. 2

JOIN
Ch.3. Multi Table Queries

JOIN

• A relational operation that causes two or more tables with a common domain to
be combined into a single table
• Specifying the column from each table to be used for the join, using a foreign
key from one table and its associated key in the other table (i.e., matching keys)
• According to approaches, categorized into;
▪ INNER JOIN
▪ OUTER JOIN
Ch.3. Multi Table Queries

INNER JOIN

• Most frequently used JOIN


• A join in which the joining condition is based
on equality statement between the common
columns, eliminating one of the duplicate
columns in the result table
• Only returning rows from each table that have
matching rows in the other
INNER JOIN
Ch.3. Multi Table Queries

INNER JOIN_ How It Works


Table: Customers Table: Orders

CustomerID AccountNumber Gender CustomerLevel CustomerID OrderNumber CurrencyRateID

CT_1 AN_1 1 2 CT_1 OR_1 USD


CT_2 AN_2 0 5 CT_3 OR_2 CAD
CT_3 AN_3 1 4 CT_5 OR_3 USD
CT_4 AN_4 0 1 CT_7 OR_4 CAD
CT_5 AN_5 1 8 CT_9 OR_5 USD

CustomerID AccountNumber Gender CustomerLevel OrderNumber CurrencyRateID

CT_1 AN_1 1 2 OR_1 USD

CT_3 AN_3 1 4 OR_2 CAD

CT_5 AN_5 1 8 OR_3 USD


Ch.3. Multi Table Queries

INNER JOIN_ Example_1

• Show individual’s first and last name along with address ID and address ID type

SELECT
Person.Person.BusinessEntityID, Person.Person.FirstName, Person.Person.LastName,
Person.BusinessEntityAddress.AddressID, Person.BusinessEntityAddress.AddressTypeID
FROM Person.Person INNER JOIN Person.BusinessEntityAddress
ON (Person.Person.BusinessEntityID = Person.BusinessEntityAddress.BusinessEntityID)
Ch.3. Multi Table Queries

INNER JOIN_ Example_2

• Find product and supplier information for any combination of parts supplied by a
company where price of product is more than $10 (DB: AdventureWorks)

SELECT
Purchasing.ProductVendor.ProductID, Purchasing.Vendor.BusinessEntityID,
Purchasing.Vendor.Name
FROM Purchasing.ProductVendor INNER JOIN Purchasing.Vendor
ON (Purchasing.ProductVendor.BusinessEntityID = Purchasing.Vendor.BusinessEntityID)
WHERE Purchasing.ProductVendor.StandardPrice > $10
Ch.3. Multi Table Queries

INNER JOIN_ Example_2 (cont’d)

Assuming ProductID, Name, and StandardPrice exist only one of the two tables,
will the two queries below produce the same result?

SELECT Purchasing.ProductVendor.ProductID, Purchasing.Vendor.BusinessEntityID,


Purchasing.Vendor.Name FROM Purchasing.ProductVendor INNER JOIN Purchasing.Vendor

ON (Purchasing.ProductVendor.BusinessEntityID = Purchasing.Vendor.BusinessEntityID)

WHERE Purchasing.ProductVendor.StandardPrice > $10

SELECT ProductID, Purchasing.Vendor.BusinessEntityID, Name FROM Purchasing.ProductVendor


INNER JOIN Purchasing.Vendor ON (Purchasing.ProductVendor.BusinessEntityID =
Purchasing.Vendor.BusinessEntityID) WHERE StandardPrice > $10

When a column is unique, existing in one of the two


tables, no need to specify tables with columns
Ch.3. Multi Table Queries

INNER JOIN on Two Columns

• Sometimes a single column is NOT enough to uniquely identify each row


• Therefore, multiple columns (two or more than two) are necessary for JOIN

Neither Date nor ProductID can uniquely identify each row


but the combination of the two can

Date ProductID Price Date ProductID AvailQty

2022-10-11 A 20 2022-10-11 A 25
2022-10-11 B 50 2022-10-11 B 40
2022-10-11 C 40 2022-10-11 C 55
2022-10-12 A 25 2022-10-11 D 60
2022-10-12 B 45 2022-10-11 E 75
Ch.3. Multi Table Queries

INNER JOIN on Two Columns

• Show order id, order detail id, product id, special offer id, and modified date

SELECT
sod.SalesOrderID, sod.SalesOrderDetailID, so.ProductID, so.SpecialOfferID, so.ModifiedDate
FROM Sales.SalesOrderDetail AS sod INNER JOIN Sales.SpecialOfferProduct AS so
ON so.ProductID = sod.ProductID AND so.SpecialOfferID = sod.SpecialOfferID
Ch.3. Multi Table Queries

OUTER JOIN

• A join in which rows that do not have matching


values in common columns are nonetheless
included in the result table
• As opposed to inner join, OUTER JOIN
allows row without matching values in outer
table to appear in the result
(Left) OUTER JOIN
• Hence, sometimes resulting in missing values
(i.e., nulls)
Ch.3. Multi Table Queries

LEFT OUTER JOIN_ How It Works


Table: Customers Table: Orders

CustomerID AccountNumber Gender CustomerLevel CustomerID OrderNumber CurrencyRateID

CT_1 AN_1 1 2 CT_1 OR_1 USD


CT_2 AN_2 0 5 CT_3 OR_2 CAD
CT_3 AN_3 1 4 CT_5 OR_3 USD
CT_4 AN_4 0 1 CT_7 OR_4 CAD
CT_5 AN_5 1 8 CT_9 OR_5 USD

CustomerID AccountNumber Gender CustomerLevel OrderNumber CurrencyRateID

CT_1 AN_1 1 2 OR_1 USD


CT_2 AN_2 0 5 null null
CT_3 AN_3 1 4 OR_2 CAD
CT_4 AN_4 0 1 null null
CT_5 AN_5 1 8 OR_3 USD
Ch.3. Multi Table Queries

LEFT OUTER JOIN

• Show the customers (1, 2, 3, 4, 11028,11029) and their sales orders


using LEFT JOIN

SELECT c.CustomerID, s.SalesOrderID, s.OrderDate FROM Sales.Customer AS c


LEFT OUTER JOIN Sales.SalesOrderHeader AS s ON
c.CustomerID = s.CustomerID WHERE c.CustomerID IN (1, 2, 3, 4, 11028,11029);

Sales.Customer Sales.SalesOrderHeader
Ch.3. Multi Table Queries

RIGHT OUTER JOIN_ How It Works

CustomerID AccountNumber Gender CustomerLevel CustomerID OrderNumber CurrencyRateID

CT_1 AN_1 1 2 CT_1 OR_1 USD


CT_2 AN_2 0 5 CT_3 OR_2 CAD
CT_3 AN_3 1 4 CT_5 OR_3 USD
CT_4 AN_4 0 1 CT_7 OR_4 CAD
CT_5 AN_5 1 8 CT_9 OR_5 USD

CustomerID OrderNumber CurrencyRateID AccountNumber Gender CustomerLevel

CT_1 OR_1 USD AN_1 1 2


CT_3 OR_2 CAD AN_3 1 4
CT_5 OR_3 USD AN_5 1 8
CT_7 OR_4 CAD null null null
CT_9 OR_5 USD null null null
Ch.3. Multi Table Queries

RIGHT OUTER JOIN

• Show the customers (1, 2, 3, 4, 11028,11029) and their sales orders


using RIGHT JOIN

SELECT c.CustomerID, s.SalesOrderID, s.OrderDate


FROM Sales.SalesOrderHeader AS s RIGHT OUTER JOIN Sales.Customer AS c ON
c.CustomerID = s.CustomerID;

Sales.Customer Sales.SalesOrderHeader
Ch.3. Multi Table Queries

References

• The major contents of this note are reproduced from the textbook of BCIS 5420;
Topi et al. Modern Database Management. 13 th edition. Pearson's, 2019

• Unless having a specific reference source, the photos and icons used in this
material are from the following sources providing copyright free images:
imagesource.com, iconfinder.com, and pexels.com

• The diagrams used are from the textbook publisher’s materials

You might also like