SlideShare a Scribd company logo
1 of 42
A Guide to SQL, Ninth Edition
Chapter Five
Multiple-Table Queries
Objectives
• Use joins to retrieve data from more than one
table
• Use the IN and EXISTS operators to query
multiple tables
• Use a subquery with a subquery
• Use an alias
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
2
Objectives (continued)
• Join a table to itself
• Perform set operations (union, intersection, and
difference)
• Use the ALL and ANY operators in a query
• Perform special operations (inner join, outer join,
and product)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
3
Querying Multiple Tables
• Retrieve data from two or more tables
– Join tables
– Use the same commands as for single-table
queries
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
4
Joining Two Tables
• SELECT clause
– List all columns to display
• FROM clause
– List all tables involved in query
• WHERE clause
– Restrict to rows that have common values in
matching columns
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
5
Joining Two Tables (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 6
Figure 5-1: Joining two tables with a single SQL command
Joining Two Tables (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 7
Figure 5-2: Restricting the rows in a join
Joining Two Tables (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 8
Figure 5-3: Joining the ORDER_LINE and ITEM tables
Comparing JOINS, IN, and EXISTS
• Can join tables using:
– WHERE clause
– IN operator with a subquery
– EXISTS operator with a subquery
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
9
Comparing JOINS, IN, and EXISTS
(continued)
• WHERE clause
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
10
Figure 5-4: Restricting the rows when joining the ORDER_LINE and ITEM tables
Using the IN Operator
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 11
Figure 5-5: Using the IN operator instead of a join to query two tables
Using the EXISTS Operator
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
12
Figure 5-7 Using the EXISTS operator to select order information
Using the EXISTS Operator (continued)
• Correlated subquery
– Subquery involves a table listed in outer query
• In Figure 5-7, the ORDERS table, listed in
FROM clause of outer query used in subquery
• Must qualify ORDER_NUM column in subquery
as ORDERS.ORDER_NUM
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
13
Using a Subquery within a
Subquery
• Nested subquery is a subquery within a
subquery
• Evaluate from innermost query to outermost
• More than one approach to formulating queries
• Many DMBSs have optimizers that analyze
queries for efficiency
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
14
Using a Subquery within a
Subquery (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 15
Figure 5-8: Nested subqueries (a subquery within a
subquery)
Using a Subquery within a
Subquery (continued)
• Order of evaluating query
– Innermost subquery first producing a temporary
table
– Next (intermediate) subquery producing a second
temporary table
– Outer query last producing final result
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
16
A Comprehensive Example
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 17
Figure 5-10 Comprehensive example
Using an Alias
• An alternate name for a table
• Use in FROM clause
• Type name of table, press Spacebar, and then
type name of alias
• Allows for simplicity
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
18
Using an Alias (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 19
Figure 5-11: Using aliases in a query
Joining a Table to Itself
• Called a self-join
• Use a different alias for same table
• Use to compare records within one table
• Treat one table as two separate tables by using
alias
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
20
Joining a Table to Itself (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 21
Figure 5-12: Using aliases for a self-join
Using a Self-Join on a Primary
Key
• Can create a self-join that involves primary key
of table
• List table twice in FROM clause with aliases
– Same as previous self-join example
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
22
Using a Self-Join on a Primary Key
(continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
23
Figure 5-15: List of employees and their managers
Joining Several Tables
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
24
• Condition relates columns for each pair of tables
Figure 5-16: Joining four tables in a query
Joining Several Tables
(continued)
• In SELECT clause, list all columns to display
• Qualify any column names if needed
• In FROM clause, list all tables
• Include tables used in the WHERE clause, even
if they are not in the SELECT clause
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
25
Joining Several Tables
(continued)
• Take one pair of related tables at a time
• Indicate in WHERE clause the condition that
relates tables
• Join conditions with AND operator
• Include any additional conditions in WHERE
clause
• Connect them with AND operator
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
26
Set Operations
• Union
– The union of two tables is a table containing
every row that is in either the first table, the
second table, or both tables
– Use UNION operator
– Tables must be union compatible; that is, the
same number of columns and corresponding
columns have identical data types and lengths
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
27
Set Operations (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 28
Figure 5-20: Using the UNION operator
Set Operations (continued)
• Intersection
– Intersection of two tables is a table containing all
rows that are in both tables
– Uses the INTERSECT operator
– Not supported by Microsoft Access
• Use an alternate approach
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
29
Set Operations (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 30
Figure 5-21: Using the INTERSECT operator
Set Operations (continued)
• Difference
– Difference of two tables is a table containing set
of all rows that are in first table but not in second
table
– Uses the MINUS operator
– Not supported by SQL Server and Microsoft
Access
• Use an alternate approach
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
31
Set Operations (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 32
Figure 5-23: Using the MINUS operator
ALL and ANY
• ALL operator
– Condition is true only if it satisfies all values
• ANY operator
– Condition is true only if it satisfies any value
• Precede subquery with appropriate operator
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
33
ALL and ANY (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
34
Figure 5-25: SELECT command that uses the ALL operator
ALL and ANY (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 35
Figure 5-27: SELECT command with an ANY operator
Special Operations
• Self-join
• Inner join
• Outer join
• Product
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
36
Inner Join
• Compares the tables in FROM clause and lists
only those rows that satisfy condition in WHERE
clause
• INNER JOIN command
– Update to SQL standard 1992
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
37
Inner Join (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 38
Figure 5-29: Query that uses and INNER JOIN clause
Outer Joins
• Left outer join: all rows from the table on the left
(listed first in the query) will be included; matching
rows only from the table on the right will be included
• Right outer join: all rows from the table on the right
will be included; matching rows only from the table
on the left will be included
• Full outer join: all rows from both tables will be
included regardless of matches
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
39
Product
• The product (Cartesian product) of two tables is
the combination of all rows in the first table and
all rows in the second table
• Omit the WHERE clause to form a product
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
40
Summary
• Join tables with WHERE clause
• Join tables with IN operator
• Join tables with EXISTS operator
• A subquery can contain another
subquery
• Use an alias to simplify SQL
command as well to create self-join
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
41
Summary (continued)
• UNION, INTERSECT, MINUS
• ALL and ANY operators
• Inner join
• Outer joins
– Left, right, full
• Cartesian product
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
42

More Related Content

Similar to Sql9e ppt ch05

Access 2016 module 2 ppt presentation
Access 2016 module 2 ppt presentationAccess 2016 module 2 ppt presentation
Access 2016 module 2 ppt presentationdgdotson
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentationdgdotson
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationdgdotson
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentationdgdotson
 
Access 2016 module 3 ppt presentation
Access 2016 module 3 ppt presentationAccess 2016 module 3 ppt presentation
Access 2016 module 3 ppt presentationdgdotson
 
New Perspectives on Microsoft Access 2016Module 4Creating.docx
New Perspectives on Microsoft Access 2016Module 4Creating.docxNew Perspectives on Microsoft Access 2016Module 4Creating.docx
New Perspectives on Microsoft Access 2016Module 4Creating.docxgibbonshay
 
Access 2016 module 1 ppt presentation
Access 2016 module 1 ppt presentationAccess 2016 module 1 ppt presentation
Access 2016 module 1 ppt presentationdgdotson
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Steve Guinan
 
Data Structures and CAATTs for Data Extraction
Data Structures and CAATTs for Data ExtractionData Structures and CAATTs for Data Extraction
Data Structures and CAATTs for Data Extraction204025
 
Power point 2016 module 3 ppt presentation
Power point 2016 module 3 ppt presentationPower point 2016 module 3 ppt presentation
Power point 2016 module 3 ppt presentationdgdotson
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05Terry Yoast
 
Management chapter 6 organization structure.pptx
Management chapter 6 organization structure.pptxManagement chapter 6 organization structure.pptx
Management chapter 6 organization structure.pptxMuhammadAdeel321
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
Power point 2016 module 1 ppt presentation
Power point 2016 module 1 ppt presentationPower point 2016 module 1 ppt presentation
Power point 2016 module 1 ppt presentationdgdotson
 
Chapt7.ppt
Chapt7.pptChapt7.ppt
Chapt7.pptImXaib
 
Power point 2016 module 4 ppt presentation
Power point 2016 module 4 ppt presentationPower point 2016 module 4 ppt presentation
Power point 2016 module 4 ppt presentationdgdotson
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
Shelly cashman microsoft access 2016 module 2 querying a dat
Shelly cashman microsoft access 2016 module 2  querying a datShelly cashman microsoft access 2016 module 2  querying a dat
Shelly cashman microsoft access 2016 module 2 querying a datmayank272369
 
Hitt_ppt_12e_ch03_SM.pptx
Hitt_ppt_12e_ch03_SM.pptxHitt_ppt_12e_ch03_SM.pptx
Hitt_ppt_12e_ch03_SM.pptxmubasherakram1
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structuremshellman
 

Similar to Sql9e ppt ch05 (20)

Access 2016 module 2 ppt presentation
Access 2016 module 2 ppt presentationAccess 2016 module 2 ppt presentation
Access 2016 module 2 ppt presentation
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentation
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentation
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentation
 
Access 2016 module 3 ppt presentation
Access 2016 module 3 ppt presentationAccess 2016 module 3 ppt presentation
Access 2016 module 3 ppt presentation
 
New Perspectives on Microsoft Access 2016Module 4Creating.docx
New Perspectives on Microsoft Access 2016Module 4Creating.docxNew Perspectives on Microsoft Access 2016Module 4Creating.docx
New Perspectives on Microsoft Access 2016Module 4Creating.docx
 
Access 2016 module 1 ppt presentation
Access 2016 module 1 ppt presentationAccess 2016 module 1 ppt presentation
Access 2016 module 1 ppt presentation
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7
 
Data Structures and CAATTs for Data Extraction
Data Structures and CAATTs for Data ExtractionData Structures and CAATTs for Data Extraction
Data Structures and CAATTs for Data Extraction
 
Power point 2016 module 3 ppt presentation
Power point 2016 module 3 ppt presentationPower point 2016 module 3 ppt presentation
Power point 2016 module 3 ppt presentation
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05
 
Management chapter 6 organization structure.pptx
Management chapter 6 organization structure.pptxManagement chapter 6 organization structure.pptx
Management chapter 6 organization structure.pptx
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
Power point 2016 module 1 ppt presentation
Power point 2016 module 1 ppt presentationPower point 2016 module 1 ppt presentation
Power point 2016 module 1 ppt presentation
 
Chapt7.ppt
Chapt7.pptChapt7.ppt
Chapt7.ppt
 
Power point 2016 module 4 ppt presentation
Power point 2016 module 4 ppt presentationPower point 2016 module 4 ppt presentation
Power point 2016 module 4 ppt presentation
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
Shelly cashman microsoft access 2016 module 2 querying a dat
Shelly cashman microsoft access 2016 module 2  querying a datShelly cashman microsoft access 2016 module 2  querying a dat
Shelly cashman microsoft access 2016 module 2 querying a dat
 
Hitt_ppt_12e_ch03_SM.pptx
Hitt_ppt_12e_ch03_SM.pptxHitt_ppt_12e_ch03_SM.pptx
Hitt_ppt_12e_ch03_SM.pptx
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
 

More from Dr. Ahmed Al Zaidy

Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingDr. Ahmed Al Zaidy
 
Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web formsDr. Ahmed Al Zaidy
 
Chapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsChapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsDr. Ahmed Al Zaidy
 
Chapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesChapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesDr. Ahmed Al Zaidy
 
Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsDr. Ahmed Al Zaidy
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptDr. Ahmed Al Zaidy
 
Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaDr. Ahmed Al Zaidy
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web formDr. Ahmed Al Zaidy
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsDr. Ahmed Al Zaidy
 
Chapter 5 Designing for the mobile web
Chapter 5 Designing for the mobile webChapter 5 Designing for the mobile web
Chapter 5 Designing for the mobile webDr. Ahmed Al Zaidy
 
Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSDr. Ahmed Al Zaidy
 
Chapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutChapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutDr. Ahmed Al Zaidy
 
Chapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSChapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSDr. Ahmed Al Zaidy
 
Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Dr. Ahmed Al Zaidy
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2Dr. Ahmed Al Zaidy
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityDr. Ahmed Al Zaidy
 

More from Dr. Ahmed Al Zaidy (20)

Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based Programming
 
Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web forms
 
Chapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsChapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheets
 
Chapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesChapter 11 Working with Events and Styles
Chapter 11 Working with Events and Styles
 
Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statements
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScript
 
Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimedia
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web form
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and Columns
 
Chapter 5 Designing for the mobile web
Chapter 5 Designing for the mobile webChapter 5 Designing for the mobile web
Chapter 5 Designing for the mobile web
 
Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSS
 
Chapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutChapter 3 Designing a Page Layout
Chapter 3 Designing a Page Layout
 
Chapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSChapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSS
 
Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5
 
Integer overflows
Integer overflowsInteger overflows
Integer overflows
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2
 
Fundamental of testing
Fundamental of testingFundamental of testing
Fundamental of testing
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data Security
 

Recently uploaded

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 GraphThiyagu K
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
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.pdfQucHHunhnh
 
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 ModeThiyagu K
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

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
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
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
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

Sql9e ppt ch05

  • 1. A Guide to SQL, Ninth Edition Chapter Five Multiple-Table Queries
  • 2. Objectives • Use joins to retrieve data from more than one table • Use the IN and EXISTS operators to query multiple tables • Use a subquery with a subquery • Use an alias ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 2
  • 3. Objectives (continued) • Join a table to itself • Perform set operations (union, intersection, and difference) • Use the ALL and ANY operators in a query • Perform special operations (inner join, outer join, and product) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 3
  • 4. Querying Multiple Tables • Retrieve data from two or more tables – Join tables – Use the same commands as for single-table queries ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 4
  • 5. Joining Two Tables • SELECT clause – List all columns to display • FROM clause – List all tables involved in query • WHERE clause – Restrict to rows that have common values in matching columns ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 5
  • 6. Joining Two Tables (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 6 Figure 5-1: Joining two tables with a single SQL command
  • 7. Joining Two Tables (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 7 Figure 5-2: Restricting the rows in a join
  • 8. Joining Two Tables (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 8 Figure 5-3: Joining the ORDER_LINE and ITEM tables
  • 9. Comparing JOINS, IN, and EXISTS • Can join tables using: – WHERE clause – IN operator with a subquery – EXISTS operator with a subquery ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 9
  • 10. Comparing JOINS, IN, and EXISTS (continued) • WHERE clause ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 10 Figure 5-4: Restricting the rows when joining the ORDER_LINE and ITEM tables
  • 11. Using the IN Operator ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 11 Figure 5-5: Using the IN operator instead of a join to query two tables
  • 12. Using the EXISTS Operator ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 12 Figure 5-7 Using the EXISTS operator to select order information
  • 13. Using the EXISTS Operator (continued) • Correlated subquery – Subquery involves a table listed in outer query • In Figure 5-7, the ORDERS table, listed in FROM clause of outer query used in subquery • Must qualify ORDER_NUM column in subquery as ORDERS.ORDER_NUM ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 13
  • 14. Using a Subquery within a Subquery • Nested subquery is a subquery within a subquery • Evaluate from innermost query to outermost • More than one approach to formulating queries • Many DMBSs have optimizers that analyze queries for efficiency ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 14
  • 15. Using a Subquery within a Subquery (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 15 Figure 5-8: Nested subqueries (a subquery within a subquery)
  • 16. Using a Subquery within a Subquery (continued) • Order of evaluating query – Innermost subquery first producing a temporary table – Next (intermediate) subquery producing a second temporary table – Outer query last producing final result ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 16
  • 17. A Comprehensive Example ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 17 Figure 5-10 Comprehensive example
  • 18. Using an Alias • An alternate name for a table • Use in FROM clause • Type name of table, press Spacebar, and then type name of alias • Allows for simplicity ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 18
  • 19. Using an Alias (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 19 Figure 5-11: Using aliases in a query
  • 20. Joining a Table to Itself • Called a self-join • Use a different alias for same table • Use to compare records within one table • Treat one table as two separate tables by using alias ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 20
  • 21. Joining a Table to Itself (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 21 Figure 5-12: Using aliases for a self-join
  • 22. Using a Self-Join on a Primary Key • Can create a self-join that involves primary key of table • List table twice in FROM clause with aliases – Same as previous self-join example ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 22
  • 23. Using a Self-Join on a Primary Key (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 23 Figure 5-15: List of employees and their managers
  • 24. Joining Several Tables ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 24 • Condition relates columns for each pair of tables Figure 5-16: Joining four tables in a query
  • 25. Joining Several Tables (continued) • In SELECT clause, list all columns to display • Qualify any column names if needed • In FROM clause, list all tables • Include tables used in the WHERE clause, even if they are not in the SELECT clause ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 25
  • 26. Joining Several Tables (continued) • Take one pair of related tables at a time • Indicate in WHERE clause the condition that relates tables • Join conditions with AND operator • Include any additional conditions in WHERE clause • Connect them with AND operator ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 26
  • 27. Set Operations • Union – The union of two tables is a table containing every row that is in either the first table, the second table, or both tables – Use UNION operator – Tables must be union compatible; that is, the same number of columns and corresponding columns have identical data types and lengths ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 27
  • 28. Set Operations (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 28 Figure 5-20: Using the UNION operator
  • 29. Set Operations (continued) • Intersection – Intersection of two tables is a table containing all rows that are in both tables – Uses the INTERSECT operator – Not supported by Microsoft Access • Use an alternate approach ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 29
  • 30. Set Operations (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 30 Figure 5-21: Using the INTERSECT operator
  • 31. Set Operations (continued) • Difference – Difference of two tables is a table containing set of all rows that are in first table but not in second table – Uses the MINUS operator – Not supported by SQL Server and Microsoft Access • Use an alternate approach ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 31
  • 32. Set Operations (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 32 Figure 5-23: Using the MINUS operator
  • 33. ALL and ANY • ALL operator – Condition is true only if it satisfies all values • ANY operator – Condition is true only if it satisfies any value • Precede subquery with appropriate operator ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 33
  • 34. ALL and ANY (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 34 Figure 5-25: SELECT command that uses the ALL operator
  • 35. ALL and ANY (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 35 Figure 5-27: SELECT command with an ANY operator
  • 36. Special Operations • Self-join • Inner join • Outer join • Product ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 36
  • 37. Inner Join • Compares the tables in FROM clause and lists only those rows that satisfy condition in WHERE clause • INNER JOIN command – Update to SQL standard 1992 ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 37
  • 38. Inner Join (continued) ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 38 Figure 5-29: Query that uses and INNER JOIN clause
  • 39. Outer Joins • Left outer join: all rows from the table on the left (listed first in the query) will be included; matching rows only from the table on the right will be included • Right outer join: all rows from the table on the right will be included; matching rows only from the table on the left will be included • Full outer join: all rows from both tables will be included regardless of matches ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 39
  • 40. Product • The product (Cartesian product) of two tables is the combination of all rows in the first table and all rows in the second table • Omit the WHERE clause to form a product ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 40
  • 41. Summary • Join tables with WHERE clause • Join tables with IN operator • Join tables with EXISTS operator • A subquery can contain another subquery • Use an alias to simplify SQL command as well to create self-join ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 41
  • 42. Summary (continued) • UNION, INTERSECT, MINUS • ALL and ANY operators • Inner join • Outer joins – Left, right, full • Cartesian product ©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 42