You are on page 1of 32

Chapter 8

Advanced SQL- Part I


©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
.
Learning Objectives
 In this chapter, you (also) will learn:
 How to use the advanced SQL JOIN operator syntax
 How to create views
 About the relational set operators UNION, UNION
ALL, INTERSECT, and MINUS

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 2
Joining Tables
Joining Database Tables
 Performed when data are retrieved from more than
one table at a time
 Equality comparison between foreign key and primary
key of related tables
 Tables are joined by listing tables in FROM clause of
SELECT statement
 DBMS creates Cartesian product of every table in the
FROM clause

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
4
Table 8.1 - SQL Join Expression Styles

Ref: https://
www.google.com/url?sa=i&url=https%3A%2F%2Fwww.youtu
be.com%2Fwatch%3Fv%3DUakldqF6L2o&psig=AOvVaw3mpY
MjiItFNdzxgH640S28&ust=1584028891507000&source=imag
es&cd=vfe&ved=0CA0QjhxqFwoTCODDuM3lkugCFQAAAAAd
AAAAABAN

5
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
6
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 7
Joining Tables With an Alias
 Alias identifies source table from which data are
taken
 Any legal table name can be used as alias
 Add alias after table name in FROM clause

Alias

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
8
Recursive Joins
 Recursive query: Table is joined to itself using alias
 Use aliases to differentiate the table from itself

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
9
Recursive Joins (cont.)
SELECT E.EMP_NUM, E.EMP_LNAME, E.EMP_MGR, M.EMP_LNAME
FROM EMP E, EMP M
WHERE E.EMP_MGR = M.EMP_NUM
ORDER BY E.EMP_MGR;

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
10
Natural join
• No need to
specify the
common
attribute
• Will only return
one copy of the
common
attribute
• Not supported
by SQL Server

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 11
JOIN USING

• Specify the common attribute


• Will only return one copy of the common
attribute
• Not supported by SQL Server

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 12
JOIN ON

• Specify the common attribute by table


qualifiers (otherwise error)
• Will allow for joins even when the tables do
not share a common attribute
• Supported by SQL Server

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 13
Recursive Joins with JOIN ON
SELECT E.EMP_NUM, E.EMP_LNAME, E.EMP_MGR, M.EMP_LNAME
FROM EMP E, EMP M
WHERE E.EMP_MGR = M.EMP_NUM
ORDER BY E.EMP_MGR;

SELECT E.EMP_NUM, E.EMP_LNAME, E.EMP_MGR, M.EMP_LNAME


FROM EMP E JOIN EMP M
ON E.EMP_MGR = M.EMP_NUM
ORDER BY E.EMP_MGR;

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 14
Another Example: INNER JOIN
Table: N1 Table: N2
A B B C
---------- ---------- ---------- ----------
John Jack Mary Rob
Martin Mary Sue Sandra
Sara Sue Ben Don

SELECT * FROM N1 [INNER] JOIN N2 ON N1.B = N2.B;

A B B C
---------- ---------- ---------- ----------
Martin Mary Mary Rob
Sara Sue Sue Sandra

(2 row(s) affected)

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 15
LEFT OUTER JOIN
Table: N1 Table: N2
A B B C
---------- ---------- ---------- ----------
John Jack Mary Rob
Martin Mary Sue Sandra
Sara Sue Ben Don

SELECT * FROM N1 LEFT [OUTER] JOIN N2 ON N1.B = N2.B;

A B B C
---------- ---------- ---------- ----------
John Jack NULL NULL
Martin Mary Mary Rob
Sara Sue Sue Sandra

(3 row(s) affected)
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 16
RIGHT OUTER JOIN
Table: N1 Table: N2
A B B C
---------- ---------- ---------- ----------
John Jack Mary Rob
Martin Mary Sue Sandra
Sara Sue Ben Don

SELECT * FROM N1 RIGHT [OUTER] JOIN N2 ON N1.B = N2.B;

A B B C
---------- ---------- ---------- ----------
Martin Mary Mary Rob
Sara Sue Sue Sandra
NULL NULL Ben Don

(3 row(s) affected)
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 17
FULL OUTER JOIN
Table: N1 Table: N2
A B B C
---------- ---------- ---------- ----------
John Jack Mary Rob
Martin Mary Sue Sandra
Sara Sue Ben Don

SELECT * FROM N1 FULL [OUTER] JOIN N2 ON N1.B = N2.B;

A B B C
---------- ---------- ---------- ----------
John Jack NULL NULL
Martin Mary Mary Rob
Sara Sue Sue Sandra
NULL NULL Ben Don

(4 row(s) affected)
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 18
Virtual Tables: Creating a View
 View: Virtual table based on a SELECT query
 Base tables: Tables on which the view is based
 CREATE VIEW statement: Data definition
command that stores the subquery specification in the
data dictionary
 CREATE VIEW command
 CREATE VIEW viewname AS SELECT query

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use. 19
Relational Operators in
SQL
REVIEW
Figure 3.4 - Select

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 21
REVIEW

Implementing ‘select’ in SQL

SELECT * FROM P;

SELECT * FROM P WHERE PRICE < 2.00;

SELECT * FROM P WHERE P_CODE = 311452;

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 22
REVIEW
Figure 3.5 - Project

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 23
REVIEW

Implementing ‘project’ in SQL

SELECT PRICE FROM P;

SELECT P_DESCRIPT, PRICE FROM P;

SELECT P_CODE, PRICE FROM P;

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 24
Implementing ‘Union’ in SQL

#rows in CUSTOMER: 10
#rows in CUSTOMER_2: 7
#rows in UNION: 15

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 25
UNION ALL

#rows in CUSTOMER: 10
#rows in CUSTOMER_2: 7
#rows in UNION ALL: 17

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 26
UNION and UNION ALL
 UNION
 Combines rows from two or more queries without including duplicate rows
 Syntax - query UNION query

 UNION ALL
 Produces a relation that retains duplicate rows
 Can be used to unite more than two queries

 UNION (as well as INTERSECT, and Except (MINUS)) work


properly when relations are union-compatible
 Union-compatible: Number of attributes are the same and their
corresponding data types are alike

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 27
Implementing ‘intersect’ in SQL
 INTERSECT
 Combines rows from two queries, returning only the
rows that appear in both sets
 Syntax - query INTERSECT query

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 28
Implementing ‘difference’ in SQL
 EXCEPT (MINUS)
 Implementation of ‘difference’ set operator
 EXCEPT in MS SQL Server, MINUS in Oracle
 Combines rows from two queries and returns only the rows
that appear in the first set
 Syntax
 query EXCEPT query
 query MINUS query

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 29
Alternatives
 Syntax alternatives
 IN and NOT IN subqueries can be used in place of
INTERSECT or EXCEPT

yields the same results as:

A subquery
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 30
Alternatives (cont.)

yields the same results as:

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 31
Implementing ‘product’ in SQL

T1 T2

SELECT * FROM T1, T2;


or
SELECT * FROM T1 CROSS JOIN T2;

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, 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 or school-approved learning management system for classroom use.
. 32

You might also like