SlideShare a Scribd company logo
1 of 26
ADVANCED SQL
1
Essentials of Database Management
By: Zandro Apostol
Chapter 7 Copyright © 2014 Pearson Education, Inc.
OBJECTIVES
 Define terms
 Write single and multiple table SQL queries
 Define and use three types of joins
2
Chapter 7 Copyright © 2014 Pearson Education, Inc.
PROCESSING MULTIPLE TABLES
Join–a relational operation that causes two or more
tables with a common domain to be combined into a
single table or view
Equi-join–a join in which the joining condition is
based on equality between values in the common
columns; common columns appear redundantly in
the result table
Natural join–an equi-join in which one of the
duplicate columns is eliminated in the result table
3
The common columns in joined tables are usually the primary key
of the dominant table and the foreign key of the dependent table in
1:M relationships
Chapter 7 Copyright © 2014 Pearson Education, Inc.
PROCESSING MULTIPLE TABLES
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, in which rows must have
matching values in order to appear in the result
table)
Union join–includes all columns from each
table in the join, and an instance for each row of
each table
Self join–Matching rows of a table with other
rows from the same table
4
Chapter 7 Copyright © 2014 Pearson Education, Inc.
5
Figure 7-2
Visualization of different join types with results returned
in shaded area
Chapter 7 Copyright © 2014 Pearson Education, Inc.
THE FOLLOWING SLIDES CREATE TABLES
FOR THIS ENTERPRISE DATA MODEL
6
(from Chapter 1, Figure 1-3)
Chapter 7 Copyright © 2014 Pearson Education, Inc.
7
These tables are used in queries that follow
Figure 7-1 Pine Valley Furniture Company Customer_T and
Order_T tables with pointers from customers to their orders
7
Chapter 7 Copyright © 2014 Pearson Education, Inc.
Chapter 7 Copyright © 2014 Pearson Education, Inc.
EQUI-JOIN EXAMPLE
 For each customer who placed an order, what is the
customer’s name and order number?
8
Customer ID
appears twice in the
result
Chapter 7 Copyright © 2014 Pearson Education, Inc.
EQUI-JOIN EXAMPLE – ALTERNATIVE
SYNTAX
9
INNER JOIN clause is an alternative to WHERE clause, and is
used to match primary and foreign keys.
An INNER join will only return rows from each table that have
matching rows in the other.
This query produces same results as previous equi-join example.
Chapter 7 Copyright © 2014 Pearson Education, Inc.
NATURAL JOIN EXAMPLE
For each customer who placed an order, what is the
customer’s name and order number?
10
Join involves multiple tables in FROM clause
ON clause performs the equality
check for common columns of the
two tables
Note: from Fig. 7-1, you see that only
10 Customers have links with orders
 Only 10 rows will be returned from
this INNER join
Chapter 7 Copyright © 2014 Pearson Education, Inc.
OUTER JOIN EXAMPLE
List the customer name, ID number, and order number
for all customers. Include customer information even
for customers that do have an order.
11
LEFT OUTER JOIN clause
causes customer data to
appear even if there is no
corresponding order data
Unlike INNER join, this
will include customer
rows with no matching
order rows
Chapter 7 Copyright © 2014 Pearson Education, Inc.
12
Outer Join
Results
Unlike
INNER join,
this will
include
customer
rows with no
matching
order rows
12
Chapter 7 Copyright © 2014 Pearson Education, Inc.
Chapter 7 Copyright © 2014 Pearson Education, Inc.
SELF-JOIN EXAMPLE
13
The same table is
used on both sides
of the join;
distinguished using
table aliases
Self-joins are usually used on tables with unary relationships
Chapter 7 Copyright © 2014 Pearson Education, Inc.
14
Figure 7-5 Example of a self-join
Chapter 7 Copyright © 2014 Pearson Education, Inc.
PROCESSING MULTIPLE TABLES
USING SUBQUERIES
 Subquery–placing an inner query (SELECT statement)
inside an outer query
 Options:
 In a condition of the WHERE clause
 As a “table” of the FROM clause
 Within the HAVING clause
 Subqueries can be:
 Noncorrelated–executed once for the entire outer query
 Correlated–executed once for each row returned by the
outer query
15
Chapter 7 Copyright © 2014 Pearson Education, Inc.
SUBQUERY EXAMPLE
 Show all customers who have placed an order
16
Subquery is embedded in parentheses. In
this case it returns a list that will be used
in the WHERE clause of the outer query
The IN operator will test to
see if the CUSTOMER_ID
value of a row is included in
the list returned from the
subquery
Chapter 7 Copyright © 2014 Pearson Education, Inc.
JOIN VS. SUBQUERY
 Some queries could be accomplished by either a join or a subquery
17
Join version
Subquery version
Chapter 7 Copyright © 2014 Pearson Education, Inc.
18
Figure 7-6 Graphical depiction of two ways to
answer a query with different types of joins
Chapter 7 Copyright © 2014 Pearson Education, Inc.
19
Figure 7-6 Graphical depiction of two ways to
answer a query with different types of joins
Chapter 7 Copyright © 2014 Pearson Education, Inc.
CORRELATED VS.
NONCORRELATED SUBQUERIES
Noncorrelated subqueries:
 Do not depend on data from the outer query
 Execute once for the entire outer query
Correlated subqueries:
 Make use of data from the outer query
 Execute once for each row of the outer query
 Can use the EXISTS operator
20
Chapter 7 Copyright © 2014 Pearson Education, Inc.
21
Figure 7-8a Processing a noncorrelated subquery
A noncorrelated subquery processes completely before the outer query begins
21
Chapter 7 Copyright © 2014 Pearson Education, Inc.
Chapter 7 Copyright © 2014 Pearson Education, Inc.
CORRELATED SUBQUERY EXAMPLE
Show all orders that include furniture finished in
natural ash
22
The subquery is testing
for a value that comes
from the outer query
The EXISTS operator will return a
TRUE value if the subquery resulted
in a non-empty set, otherwise it
returns a FALSE
 A correlated subquery always refers to
an attribute from a table referenced in
the outer query
Chapter 7 Copyright © 2014 Pearson Education, Inc.
23
Figure 7-8b
Processing a
correlated
subquery
Subquery refers to outer-
query data, so executes once
for each row of outer query
Note: only the
orders that
involve products
with Natural
Ash will be
included in the
final results
23
Chapter 7 Copyright © 2014 Pearson Education, Inc.
Chapter 7 Copyright © 2014 Pearson Education, Inc.
ANOTHER SUBQUERY EXAMPLE
Show all products whose standard price is higher than
the average price
24
The WHERE clause normally cannot include aggregate functions, but because
the aggregate is performed in the subquery its result can be used in the outer
query’s WHERE clause
One column of the subquery is an
aggregate function that has an alias
name. That alias can then be
referred to in the outer query
Subquery forms the derived
table used in the FROM clause
of the outer query
Chapter 7 Copyright © 2014 Pearson Education, Inc.
UNION QUERIES
 Combine the output (union of multiple queries)
together into a single result table
25
First query
Second query
Combine
Chapter 7 Copyright © 2014 Pearson Education, Inc.
26
Figure 7-9 Combining queries using UNION
Note: with UNION
queries, the
quantity and data
types of the
attributes in the
SELECT clauses
of both queries
must be identical
26
Chapter 7 Copyright © 2014 Pearson Education, Inc.

More Related Content

Similar to AdvanceSQL.ppt

mis4200not type of joins in dbms pdddddpt
mis4200not type of joins in dbms pdddddptmis4200not type of joins in dbms pdddddpt
mis4200not type of joins in dbms pdddddpt
Punit gupta
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 
gratisexam.com-Oracle.BrainDumps.1z0-061.v2016-10-10.by.Laura.75q.pdf
gratisexam.com-Oracle.BrainDumps.1z0-061.v2016-10-10.by.Laura.75q.pdfgratisexam.com-Oracle.BrainDumps.1z0-061.v2016-10-10.by.Laura.75q.pdf
gratisexam.com-Oracle.BrainDumps.1z0-061.v2016-10-10.by.Laura.75q.pdf
NarReg
 
Manual for Troubleshooting Formulas & Functions in Excel
Manual for Troubleshooting Formulas & Functions in ExcelManual for Troubleshooting Formulas & Functions in Excel
Manual for Troubleshooting Formulas & Functions in Excel
Christopher Ward
 
Acc 350 wk 6 quiz 4 chapter 5
Acc 350 wk 6 quiz 4 chapter 5Acc 350 wk 6 quiz 4 chapter 5
Acc 350 wk 6 quiz 4 chapter 5
fascinating_heart
 

Similar to AdvanceSQL.ppt (20)

Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
Join sql
Join sqlJoin sql
Join sql
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
 
Sql join
Sql  joinSql  join
Sql join
 
Intro to t sql – 3rd session
Intro to t sql – 3rd sessionIntro to t sql – 3rd session
Intro to t sql – 3rd session
 
Question 2B
Question 2BQuestion 2B
Question 2B
 
mis4200notes7_2.ppt
mis4200notes7_2.pptmis4200notes7_2.ppt
mis4200notes7_2.ppt
 
mis4200not type of joins in dbms pdddddpt
mis4200not type of joins in dbms pdddddptmis4200not type of joins in dbms pdddddpt
mis4200not type of joins in dbms pdddddpt
 
Merging data (1)
Merging data (1)Merging data (1)
Merging data (1)
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
SUBQUERIES.pptx
SUBQUERIES.pptxSUBQUERIES.pptx
SUBQUERIES.pptx
 
gratisexam.com-Oracle.BrainDumps.1z0-061.v2016-10-10.by.Laura.75q.pdf
gratisexam.com-Oracle.BrainDumps.1z0-061.v2016-10-10.by.Laura.75q.pdfgratisexam.com-Oracle.BrainDumps.1z0-061.v2016-10-10.by.Laura.75q.pdf
gratisexam.com-Oracle.BrainDumps.1z0-061.v2016-10-10.by.Laura.75q.pdf
 
SQL MCQ
SQL MCQSQL MCQ
SQL MCQ
 
E-Book 25 Tips and Tricks MS Excel Functions & Formulaes
E-Book 25 Tips and Tricks MS Excel Functions & FormulaesE-Book 25 Tips and Tricks MS Excel Functions & Formulaes
E-Book 25 Tips and Tricks MS Excel Functions & Formulaes
 
Manual for Troubleshooting Formulas & Functions in Excel
Manual for Troubleshooting Formulas & Functions in ExcelManual for Troubleshooting Formulas & Functions in Excel
Manual for Troubleshooting Formulas & Functions in Excel
 
1 z0 047
1 z0 0471 z0 047
1 z0 047
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2
 
Acc 350 wk 6 quiz 4 chapter 5
Acc 350 wk 6 quiz 4 chapter 5Acc 350 wk 6 quiz 4 chapter 5
Acc 350 wk 6 quiz 4 chapter 5
 
MergeResult_2024_02_09_08_59_11.pptx
MergeResult_2024_02_09_08_59_11.pptxMergeResult_2024_02_09_08_59_11.pptx
MergeResult_2024_02_09_08_59_11.pptx
 

More from XanGwaps

MSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
MSITSytemDesignAndDesignasdsdsdsdsdsds.pptxMSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
MSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
XanGwaps
 
AdvanceDatabaseChapter6Advance Dtabases.pptx
AdvanceDatabaseChapter6Advance Dtabases.pptxAdvanceDatabaseChapter6Advance Dtabases.pptx
AdvanceDatabaseChapter6Advance Dtabases.pptx
XanGwaps
 
virtualization-220403085202_Chapter1.pptx
virtualization-220403085202_Chapter1.pptxvirtualization-220403085202_Chapter1.pptx
virtualization-220403085202_Chapter1.pptx
XanGwaps
 
Object-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxObject-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptx
XanGwaps
 
globodox-presentation-v14.ppsx
globodox-presentation-v14.ppsxglobodox-presentation-v14.ppsx
globodox-presentation-v14.ppsx
XanGwaps
 
CS530-Tuesday01.ppt
CS530-Tuesday01.pptCS530-Tuesday01.ppt
CS530-Tuesday01.ppt
XanGwaps
 
Object-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxObject-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptx
XanGwaps
 
Functions of Operating System.pptx
Functions of Operating System.pptxFunctions of Operating System.pptx
Functions of Operating System.pptx
XanGwaps
 
Presentation (4).pptx
Presentation (4).pptxPresentation (4).pptx
Presentation (4).pptx
XanGwaps
 

More from XanGwaps (20)

MSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
MSITSytemDesignAndDesignasdsdsdsdsdsds.pptxMSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
MSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
 
AdvanceDatabaseChapter6Advance Dtabases.pptx
AdvanceDatabaseChapter6Advance Dtabases.pptxAdvanceDatabaseChapter6Advance Dtabases.pptx
AdvanceDatabaseChapter6Advance Dtabases.pptx
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptx
 
Chapter-OBDD.pptx
Chapter-OBDD.pptxChapter-OBDD.pptx
Chapter-OBDD.pptx
 
virtualization-220403085202_Chapter1.pptx
virtualization-220403085202_Chapter1.pptxvirtualization-220403085202_Chapter1.pptx
virtualization-220403085202_Chapter1.pptx
 
Java ConstructorsPPT.pptx
Java ConstructorsPPT.pptxJava ConstructorsPPT.pptx
Java ConstructorsPPT.pptx
 
Object-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxObject-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptx
 
mod5_cabling-LANsWANs-2.ppt
mod5_cabling-LANsWANs-2.pptmod5_cabling-LANsWANs-2.ppt
mod5_cabling-LANsWANs-2.ppt
 
globodox-presentation-v14.ppsx
globodox-presentation-v14.ppsxglobodox-presentation-v14.ppsx
globodox-presentation-v14.ppsx
 
chapter-2.ppt
chapter-2.pptchapter-2.ppt
chapter-2.ppt
 
Requirements Analysis.pptx
Requirements Analysis.pptxRequirements Analysis.pptx
Requirements Analysis.pptx
 
Java Constructors.pptx
Java Constructors.pptxJava Constructors.pptx
Java Constructors.pptx
 
Java Constructors.pptx
Java Constructors.pptxJava Constructors.pptx
Java Constructors.pptx
 
CS530-Tuesday01.ppt
CS530-Tuesday01.pptCS530-Tuesday01.ppt
CS530-Tuesday01.ppt
 
Object-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxObject-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptx
 
Functions of Operating System.pptx
Functions of Operating System.pptxFunctions of Operating System.pptx
Functions of Operating System.pptx
 
HCI 1 Module 2.pptx
HCI 1 Module 2.pptxHCI 1 Module 2.pptx
HCI 1 Module 2.pptx
 
The virtual box.pptx
The virtual box.pptxThe virtual box.pptx
The virtual box.pptx
 
Presentation (10).pptx
Presentation (10).pptxPresentation (10).pptx
Presentation (10).pptx
 
Presentation (4).pptx
Presentation (4).pptxPresentation (4).pptx
Presentation (4).pptx
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 

AdvanceSQL.ppt

  • 1. ADVANCED SQL 1 Essentials of Database Management By: Zandro Apostol
  • 2. Chapter 7 Copyright © 2014 Pearson Education, Inc. OBJECTIVES  Define terms  Write single and multiple table SQL queries  Define and use three types of joins 2
  • 3. Chapter 7 Copyright © 2014 Pearson Education, Inc. PROCESSING MULTIPLE TABLES Join–a relational operation that causes two or more tables with a common domain to be combined into a single table or view Equi-join–a join in which the joining condition is based on equality between values in the common columns; common columns appear redundantly in the result table Natural join–an equi-join in which one of the duplicate columns is eliminated in the result table 3 The common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table in 1:M relationships
  • 4. Chapter 7 Copyright © 2014 Pearson Education, Inc. PROCESSING MULTIPLE TABLES 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, in which rows must have matching values in order to appear in the result table) Union join–includes all columns from each table in the join, and an instance for each row of each table Self join–Matching rows of a table with other rows from the same table 4
  • 5. Chapter 7 Copyright © 2014 Pearson Education, Inc. 5 Figure 7-2 Visualization of different join types with results returned in shaded area
  • 6. Chapter 7 Copyright © 2014 Pearson Education, Inc. THE FOLLOWING SLIDES CREATE TABLES FOR THIS ENTERPRISE DATA MODEL 6 (from Chapter 1, Figure 1-3)
  • 7. Chapter 7 Copyright © 2014 Pearson Education, Inc. 7 These tables are used in queries that follow Figure 7-1 Pine Valley Furniture Company Customer_T and Order_T tables with pointers from customers to their orders 7 Chapter 7 Copyright © 2014 Pearson Education, Inc.
  • 8. Chapter 7 Copyright © 2014 Pearson Education, Inc. EQUI-JOIN EXAMPLE  For each customer who placed an order, what is the customer’s name and order number? 8 Customer ID appears twice in the result
  • 9. Chapter 7 Copyright © 2014 Pearson Education, Inc. EQUI-JOIN EXAMPLE – ALTERNATIVE SYNTAX 9 INNER JOIN clause is an alternative to WHERE clause, and is used to match primary and foreign keys. An INNER join will only return rows from each table that have matching rows in the other. This query produces same results as previous equi-join example.
  • 10. Chapter 7 Copyright © 2014 Pearson Education, Inc. NATURAL JOIN EXAMPLE For each customer who placed an order, what is the customer’s name and order number? 10 Join involves multiple tables in FROM clause ON clause performs the equality check for common columns of the two tables Note: from Fig. 7-1, you see that only 10 Customers have links with orders  Only 10 rows will be returned from this INNER join
  • 11. Chapter 7 Copyright © 2014 Pearson Education, Inc. OUTER JOIN EXAMPLE List the customer name, ID number, and order number for all customers. Include customer information even for customers that do have an order. 11 LEFT OUTER JOIN clause causes customer data to appear even if there is no corresponding order data Unlike INNER join, this will include customer rows with no matching order rows
  • 12. Chapter 7 Copyright © 2014 Pearson Education, Inc. 12 Outer Join Results Unlike INNER join, this will include customer rows with no matching order rows 12 Chapter 7 Copyright © 2014 Pearson Education, Inc.
  • 13. Chapter 7 Copyright © 2014 Pearson Education, Inc. SELF-JOIN EXAMPLE 13 The same table is used on both sides of the join; distinguished using table aliases Self-joins are usually used on tables with unary relationships
  • 14. Chapter 7 Copyright © 2014 Pearson Education, Inc. 14 Figure 7-5 Example of a self-join
  • 15. Chapter 7 Copyright © 2014 Pearson Education, Inc. PROCESSING MULTIPLE TABLES USING SUBQUERIES  Subquery–placing an inner query (SELECT statement) inside an outer query  Options:  In a condition of the WHERE clause  As a “table” of the FROM clause  Within the HAVING clause  Subqueries can be:  Noncorrelated–executed once for the entire outer query  Correlated–executed once for each row returned by the outer query 15
  • 16. Chapter 7 Copyright © 2014 Pearson Education, Inc. SUBQUERY EXAMPLE  Show all customers who have placed an order 16 Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery
  • 17. Chapter 7 Copyright © 2014 Pearson Education, Inc. JOIN VS. SUBQUERY  Some queries could be accomplished by either a join or a subquery 17 Join version Subquery version
  • 18. Chapter 7 Copyright © 2014 Pearson Education, Inc. 18 Figure 7-6 Graphical depiction of two ways to answer a query with different types of joins
  • 19. Chapter 7 Copyright © 2014 Pearson Education, Inc. 19 Figure 7-6 Graphical depiction of two ways to answer a query with different types of joins
  • 20. Chapter 7 Copyright © 2014 Pearson Education, Inc. CORRELATED VS. NONCORRELATED SUBQUERIES Noncorrelated subqueries:  Do not depend on data from the outer query  Execute once for the entire outer query Correlated subqueries:  Make use of data from the outer query  Execute once for each row of the outer query  Can use the EXISTS operator 20
  • 21. Chapter 7 Copyright © 2014 Pearson Education, Inc. 21 Figure 7-8a Processing a noncorrelated subquery A noncorrelated subquery processes completely before the outer query begins 21 Chapter 7 Copyright © 2014 Pearson Education, Inc.
  • 22. Chapter 7 Copyright © 2014 Pearson Education, Inc. CORRELATED SUBQUERY EXAMPLE Show all orders that include furniture finished in natural ash 22 The subquery is testing for a value that comes from the outer query The EXISTS operator will return a TRUE value if the subquery resulted in a non-empty set, otherwise it returns a FALSE  A correlated subquery always refers to an attribute from a table referenced in the outer query
  • 23. Chapter 7 Copyright © 2014 Pearson Education, Inc. 23 Figure 7-8b Processing a correlated subquery Subquery refers to outer- query data, so executes once for each row of outer query Note: only the orders that involve products with Natural Ash will be included in the final results 23 Chapter 7 Copyright © 2014 Pearson Education, Inc.
  • 24. Chapter 7 Copyright © 2014 Pearson Education, Inc. ANOTHER SUBQUERY EXAMPLE Show all products whose standard price is higher than the average price 24 The WHERE clause normally cannot include aggregate functions, but because the aggregate is performed in the subquery its result can be used in the outer query’s WHERE clause One column of the subquery is an aggregate function that has an alias name. That alias can then be referred to in the outer query Subquery forms the derived table used in the FROM clause of the outer query
  • 25. Chapter 7 Copyright © 2014 Pearson Education, Inc. UNION QUERIES  Combine the output (union of multiple queries) together into a single result table 25 First query Second query Combine
  • 26. Chapter 7 Copyright © 2014 Pearson Education, Inc. 26 Figure 7-9 Combining queries using UNION Note: with UNION queries, the quantity and data types of the attributes in the SELECT clauses of both queries must be identical 26 Chapter 7 Copyright © 2014 Pearson Education, Inc.