Kroenke Dbp16e Chapter 2 Part 2
Kroenke Dbp16e Chapter 2 Part 2
Chapter 2 Part 2
Introduction to Structured
Query Language
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Learning Objectives (1 of 2)
2.1 To understand the use of extracted datasets in business intelligence (BI) systems
2.2 To understand the use of ad-hoc queries in business intelligence (BI) systems
2.3 To understand the history and significance of Structured Query Language (SQL)
2.6 To create SQL queries that use the SQL SELECT, FROM, WHERE, ORDER BY,
GROUP BY, and HAVING clauses
2.7 To create SQL queries that use the SQL DISTINCT, TOP, and TOP PERCENT
keywords
2.8 To create SQL queries that use the SQL comparison operators, including BETWEEN,
LIKE, IN, and IS NULL
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Learning Objectives (2 of 2)
2.9 To create SQL queries that use the SQL logical operators, including AND, OR, and
NOT
2.10 To create SQL queries that use the SQL built-in aggregate functions of SUM,
COUNT, MIN, MAX, and AVG with and without the SQL GROUP BY clause
2.11 To create SQL queries that retrieve data from a single table while restricting the data
based upon data in another table (subquery)
2.12 To create SQL queries that retrieve data from multiple tables using the SQL join and
JOIN ON operations
2.14 To create SQL queries that retrieve data from multiple tables using the SQL OUTER
JOIN operation
2.15 To create SQL queries that retrieve data from multiple tables using SQL set
operators UNION, INTERSECT, and EXCEPT
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Chapter 2 Part 2 Coverage
• This Microsoft PowerPoint Slide Show covers multiple
table queries and chapter objectives 2.12-2.15.
• The material in the Microsoft PowerPoint Slide Show for
Chapter Two Part One on single table queries must by
covered before discussing the material in this Slide Show.
• A review of the Cape Codd Outdoor Sports database is
included for continuity.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Business Intelligence (BI) Systems
• Business Intelligence (BI) systems are information
systems used to support management decisions by
producing information for assessment, analysis, planning,
and control.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-1: Cape Codd Outdoor Sports
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Cape Codd Outdoor Sports Background
• Cape Codd Outdoor Sports is a fictitious company based
on an actual outdoor retail equipment vendor.
• Cape Codd Outdoor Sports:
– Has 15 retail stores in the United States and Canada
– Has an online Internet store
– Has a (postal) mail order department
• All retail sales are recorded in an Oracle Database 19c D
BMS.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-2: Cape Codd Retail Sales Data
Extraction Process
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-3: Components of a Data
Warehouse
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Retail Sales and Catalog Content Data
Extraction
• The Cape Codd marketing department needs an analysis
of:
– In-store sales
– Catalog content
• The entire database is not needed for this, only an
extraction of retail sales data and catalog content.
• The data is extracted by the IS department from the
operational database into a separate, offline database
for use by the marketing department.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-4: Cape Codd Extracted Retail Sales
Data Database Tables and Relationships
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Cape Codd Extracted Retail Sales
Data Format (1 of 2)
Table Column Data Type
StoreNumber Integer
Blank
OrderYear Integer
Blank
OrderTotal Currency
SKU Integer
Blank
Quantity Integer
Blank
Price Currency
Blank
ExtendedPrice Currency
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Cape Codd Extracted Retail Sales
Data Format (2 of 2)
Table Column Data Type
SKU_DATA SKU Integer
Blank
SKU Integer
Blank
CatalogPage Integer
Blank
DateOnWebSite Date
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-6(a): Sample Data in the Cape Codd
Extracted Retail Sales Database
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-6(b): Sample Data in the Cape Codd
Extracted Retail Sales Database
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Cape Codd Database Available Online (1 of 2)
• Versions of the complete Cape Codd database are
available in the downloadable Student Files available at:
http://www.pearsonhighered.com/kroenke/
• These include versions for:
– Microsoft Access 2019
– Microsoft SQL Server 2019
– Oracle Database
– My SQL 8.0
• We recommend you actually run all material in a live
database!
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Cape Codd Database Available Online (2 of 2)
• To complete setting up the Cape Codd database, set the
referenced materials:
– For Microsoft SQL Server 2019:
▪ See Online Chapter 10A
– For Oracle Database:
▪ See Online Chapter 10B
– For My SQL 8.0
▪ See Online Chapter 10C
• Online chapters 10A, 10B, and 10C are available for download
at:
http://www.pearsonhighered.com/kroenke/
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Querying Two or More Tables with SQL
• SQL provides two different techniques for querying data
from multiple tables:
– The SQL subquery
– The SQL join
• Although both work with multiple tables, they are used for
slightly different purposes.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Querying Multiple Tables with SQL
Subqueries One
• We want to know the revenue for Water Sports items, which have SKU
values of 100100, 100200, 101100, and 101200.
• Given that we know the SKU values, we can use this query:
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Querying Multiple Tables with SQL
Subqueries two
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Querying Multiple Tables with SQL
Subqueries three
We can combine the last two queries as shown in SQL-Query-02-53 and 54.
The second query, which is enclosed in parentheses, is called an SQL
subquery. Note how the subquery returns a set of values for use by the top
level query and note the use of the SQL IN keyword.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
The Logic of Subqueries
• An SQL subquery is often described as:
– A nested query
– A query within a query
• Note that SQL queries using subqueries are still
effectively single table queries in the sense that only the
columns of the top level query can be displayed in
the query results!
• Multiple subqueries can be used to process three or even
more tables.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Query Example Using a Query with Two
Subqueries (1 of 2)
/* *** SQL-Query-CH02-56 *** */
SELECT Buyer, Department
FROM S K U_DATA
WHERE S K U IN
(SELECT SKU
FROM ORDER_ITEM
WHERE OrderNumber IN
(SELECT OrderNumber
FROM RETAIL_ORDER
WHERE OrderMonth = 'January’
AND OrderYear = 2021))
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Query Example Using a Query with Two
Subqueries (2 of 2)
/* *** SQL-Query-CH02-57 *** */
SELECT Buyer, Department, COUNT(S K U) AS Number_Of_S K U_Sold
FROM S K U_DATA
WHERE S K U IN
(SELECT SKU
FROM ORDER_ITEM
WHERE OrderNumber IN
(SELECT OrderNumber
FROM RETAIL_ORDER
WHERE OrderMonth = 'January’
AND OrderYear = 2021))
GROUP BY Buyer, Department
ORDER BY Number_Of_SKU_Sold;
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Querying Multiple Tables with JOINS
In an SQL join operation, the SQL JOIN operator is used
to combine parts or all of two or more tables.
– Explicit join ─ the SQL JOIN operator is used as part
of the SQL statement.
– Implicit join ─ the SQL JOIN operator is not used as
part of the SQL statement.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Querying Multiple Tables with an SQL
CROSS JOIN
• An SQL CROSS JOIN combines each row in one table
with every row in another table.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-29: Example of an Implicit
Join (1 of 3)
• By selecting rows matching the primary key values of one table
with the foreign key values of a second table, we produce an S
QL INNER JOIN.
Note that whenever the field name is the same in both tables, you need
to add the table name the field is from to the SQL statement.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-29: Example of an Implicit
Join (3 of 3)
It will be easier to interpret the results of the previous query if we sort the
results using an ORDER BY clause.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Querying Multiple Tables with an SQL
EQUIJOIN
• The process of using an SQL JOIN operation to join two
tables is called joining the two tables.
• When tables are joined using an equal to condition as in the
example above, the join is called an equijoin.
• When people say “join,” 99.99999 percent of the time they
mean equijoin.
• Multiple joins can be used to process three or even more
tables.
• All parts of the SQL SELECT statement syntax can be applied
to the table displaying the results of an SQL query using joins.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Using a Join to Obtain Data from Two or
More Tables (1 of 2)
/* *** SQL-Query-CH02-61 *** */
SELECT Buyer, S K U_DATA.SK U, S K U_Description,
OrderNumber, ExtendedPrice
FROM SK U_DATA, ORDER_ITEM
WHERE S KU_DATA.S K U = ORDER_ITEM.S KU;
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Using a Join to Obtain Data from Two or
More Tables (2 of 2)
To obtain the total revenue from each SKU managed by each
buyer, just add the GROUP BY and ORDER BY clause.
/* *** SQL-Query-CH02-62 *** */
SELECT Buyer, S K U_DATA.SKU, SK U_Description,
SUM(ExtendedPrice) AS BuyerS K URevenue Since these
FROM SK U_DATA, ORDER_ITEM attributes are NOT
WHERE SK U_DATA.S KU = ORDER_ITEM.SK U in a function, they
GROUP BY Buyer, S K U_DATA.SKU, SK U_Description MUST be
ORDER BY BuyerSK URevenue DESC;
mentioned in
GROUP BY
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Comparing Subqueries with Joins
• SQL subqueries and SQL joins both process multiple
tables.
• An SQL subquery can only be used to retrieve data from
the “top table.”
• An SQL join can be used to obtain data from any number
of tables, including the “top table” of the subquery.
• In Chapter 8, we will study the correlated subquery.
That kind of subquery can do work that is not possible
with joins.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Querying Multiple Tables with the SQL
JOIN ON Syntax
• In the SQL JOIN ON syntax:
– The SQL JOIN keyword is placed between the table names in
the SQL FROM clause, where it replaces the comma that
previously separated the two table names.
– The SQL ON keyword now leads into an SQL ON clause,
which includes the statement of matching key values that was
previously in an SQL WHERE clause.
– The SQL WHERE clause is no longer used as part of the
join, which makes it easier to read the actual restrictions on the
rows in the query in the SQL WHERE clause itself.
• The explicit SQL JOIN ON syntax is currently considered as
the proper way to write SQL join operations, and the older
implicit SQL syntax is considered an archaic, older syntax (but
it still works).
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Example of the JOIN ON Query (1 of 3)
This query uses the JOIN ON syntax of the SQL-Query-CH02-60 query.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Example of the JOIN ON Query (2 of 3)
Figure 2-30: Using Primary Key and Foreign Key Values in
the SQL ON Clause in an SQL JOIN.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Example of the JOIN ON Query (3 of 3)
Records can be limited to those for the OrderYear of 2020 as seen below.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Example of the SQL JOIN ON for Three or
More Tables (1 of 2)
/* *** SQL-Query-CH02-67 *** */
SELECT RETAIL_ORDER.OrderNumber, StoreNumber, OrderYear,
ORDER_ITEM.S KU, SKU_Description, Department
FROM RETAIL_ORDER JOIN ORDER_ITEM
ON RETAIL_ORDER.OrderNumber = ORDER_ITEM.OrderNumber
JOIN S K U_DATA
ON ORDER_ITEM.SK U=S KU_DATA.S K U
WHERE OrderYear = 2020
ORDER BY RETAIL_ORDER.OrderNumber, ORDER_ITEM.S K U;
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Example of the SQL JOIN ON for Three or
More Tables (2 of 2)
You can create a table alias for one or more tables as well as naming output columns as
seen below.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
SQL Queries on Recursive Relationships
(1 of 3)
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
SQL Queries on Recursive Relationships
(2 of 3)
In the Cape Codd Outdoor Sports data extract below, the BUYER table
has a recursive relationship: the Supervisor column holds value of the
BuyerName column as data and serves as a foreign key referencing
BuyerName as the associated primary key.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
SQL Queries on Recursive Relationships
(3 of 3)
The solution is to use table aliases to create two aliased versions of the same
single table. Thus, the query below lists each BuyerName and that person’s
Supervisor:
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-32: The Logic of Joins (1 of 4)
(a) The STUDENT and LOCKER Tables Aligned to Show
Row Relationships
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-32: The Logic of Joins (2 of 4)
(b) INNER JOIN of the STUDENT and LOCKER Tables
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-32: The Logic of Joins (3 of 4)
(c) LEFT OUTER JOIN of the STUDENT and LOCKER
Tables
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-32: The Logic of Joins (4 of 4)
(d) RIGHT OUTER JOIN of the STUDENT and LOCKER
Tables
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Example of the SQL LEFT OUTER JOIN
Syntax
/* *** EXAMPLE CODE – DO NOT RUN *** */
/* *** SQL-Query-CH02-75 *** */
SELECT StudentPK, StudentName, LockerFK, LockerPK,
LockerType
FROM STUDENT LEFT OUTER JOIN LOCKER
ON STUDENT.LockerFK = LOCKER.LockerPK
ORDER BY StudentPK;
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Example of the SQL RIGHT OUTER JOIN
Syntax
/* *** EXAMPLE CODE – DO NOT RUN *** */
/* *** SQL-Query-CH02-76 *** */
SELECT StudentPK, StudentName, LockerFK, LockerPK,
LockerType
FROM STUDENT RIGHT OUTER JOIN LOCKER
ON STUDENT.LockerFK = LOCKER.LockerPK
ORDER BY LockerPK;
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Mathematical Set Theory
• Mathematicians use the term set theory to describe
mathematical operations on sets, where a set is defined
as a group of distinct items.
• A relational database table meets the definition of a set,
so it is little wonder that SQL includes a group of set
operators for use with SQL queries.
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Venn Diagrams
• Venn diagrams are the standard method of visualizing sets and their
relationships.
– A set is represented by a labeled circle.
– A subset is a portion of a set that is contained entirely within the
set.
– The union of two sets represents a set that contains all values in
both sets. This is equivalent to an OR logical operation
(A OR B).
– The intersection of two sets represents the area common to both
sets. This is equivalent to an AND logical operation
(A AND B).
– The complement of set B in set A represents everything in set A
that is not in set B. This is equivalent to a logical operation using
NOT (A NOT B).
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-33: Venn Diagrams
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Figure 2-34: SQL Set Operators
Note that in order to use SQL set operators, the table
columns involved in the operations must be the same
number in each SELECT component, and corresponding
columns must have the same or compatible (e.g., CHAR
and VARCHAR) data types!
Operator Meaning
UNION The result is all the row values in one or both tables
INTERSECT The result is all the row values common to both tables
EXCEPT The result is all the row values common to both tables
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
SQL UNION Operator
“What products were available for sale (by either catalog or Web site)
in 2020 and 2021?”
/* *** SQL-Query-CH02-78 *** */
SELECT S KU, S KU_Description, Department
FROM CATALOG_S KU_2020
UNION
SELECT S KU, S KU_Description, Department
FROM CATALOG_S KU_2021;
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
SQL INTERSECT Operator
“What products were available for sale (by either catalog or Web site) in
both 2020 and 2021?” [Neither Microsoft Access 2019 nor MySQL 8.0
support the INTERSECT Operator]
/* *** SQL-Query-CH02-79 *** */
SELECT S KU, S KU_Description, Department
FROM CATALOG_S KU_2020
INTERSECT
SELECT S KU, S KU_Description, Department
FROM CATALOG_S KU_2021;
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
SQL EXCEPT Operator
“What products were available for sale (by either catalog or Web site) in
2020 but not in 2021?” [Oracle Database calls this the SQL MINUS
operator, and neither Microsoft Access 2019 nor MySQL 8.0 support
this operation]
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved
Copyright
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved