Professional Documents
Culture Documents
©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
©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
©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;
©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
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
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
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
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
SELECT * 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.
. 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
©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
©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
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.)
©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
©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