0% found this document useful (0 votes)
22 views12 pages

Chapter 12 5 Self Joins

A self-join allows a table to be joined to itself. It requires the table to have two aliases so that columns can be qualified by alias when joining conditions are specified. A common use is to list employees and their managers by joining the employee table to itself using the employee ID and manager ID columns.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views12 pages

Chapter 12 5 Self Joins

A self-join allows a table to be joined to itself. It requires the table to have two aliases so that columns can be qualified by alias when joining conditions are specified. A common use is to list employees and their managers by joining the employee table to itself using the employee ID and manager ID columns.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Self-Join

Self-Joins
• Table is joined to itself
• Table has two roles in the FROM clause
• Tables is assigned two aliases
– Table alias is used to identify which table role is meant
when specifying a column name in any of the other
clauses
• Requires the use of a column qualifier

2
Self-Joins
• List employees and their managers

3
Self-Joins
• manager_id in worker table is equal to employee_id
in manager table

4
SELF-JOIN

5
EMPLOYEES Table
EMPLOYEE_ID|FIRST_NAME|LAST_NAME|MANAGER_ID|
-----------|----------|---------|----------|
101|Lauren |Alexander| NULL|
102|Lisa |James | NULL|
103|Dave |Bernard | NULL|
104|Steve |Carr | 101|
105|Marg |Horner | 103|
106|Scott |Long | 102|
107|Jim |Best | 101|
108|Sue |McDonald | 105|
109|Trish |Albert | 105|
110|Terry |Maxwell | 103|
111|Dave |Nisbet | 104|
112|Anne |Richie | 102|

6
7
Self-Joins
• List all employees, EMPLOYEE_ID|FIRST_NAME|LAST_NAME|MANAGER_ID|
-----------|----------|---------|----------|
including those that do 101|Lauren |Alexander| NULL|
102|Lisa |James | NULL|
not have a manager 103|Dave |Bernard | NULL|
104|Steve |Carr | 101|
105|Marg |Horner | 103|
106|Scott |Long | 102|
107|Jim |Best | 101|
108|Sue |McDonald | 105|
109|Trish |Albert | 105|
110|Terry |Maxwell | 103|
111|Dave |Nisbet | 104|
112|Anne |Richie | 102|

8
9
Self-Join – Manager Example
• List managers and the employees that report to each
manager

10
SELF-JOIN – Manager example

11
12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy