Joins
Joins
Dr M AOUDE
A TYPICAL JOIN OPERATION
• Following figure displays the employee and
department tables.
• The figure illustrates the concept of a JOIN
operation by connecting columns within each table
with a line.
• One line connects the employee table's
emp_dpt_number column with the department
table's dpt_no column.
• A second line connects the employee table's
emp_ssn column to the department table's
dep_mgrssn column.
Dr M AOUDE
A TYPICAL JOIN OPERATION
Dr M AOUDE
JOINS
Dr M AOUDE
JOINS
Dr M AOUDE
JOINS
• A large organization can have dozens or even
hundreds of departments. Thus, the numbers
displayed in the department column shown above
may not be very meaningful.
• Suppose you want the department names instead
of the department numbers to be listed.
• The department names are mentioned in the
“Department” table.
• Hence we need to join the “Employee” and the
“Department” tables to get the required results
Dr M AOUDE
JOINS
SELECT emp_last_name "Last Name",
emp_first_name "First Name",
dpt_name "Department Name"
FROM employee, department
WHERE employee.emp_dpt_number = department.dpt_no;
Last Name First Name Department Name
------------- ------------- ----------------------
Bordoloi Bijoy Headquarters
Joyner Suzanne Admin and Records
Zhu Waiman Production
more rows will be displayed . . .
Dr M AOUDE
JOINS
• Following Table shows two tables simply named Table_1 and Table_2.
• Each table has a single column named Col_1. Each table also has three rows with
simple alphabetic values stored in the Col_1 column.
Table_1 Table_2
COL_1 COL_1
a a
b b
c c
Dr M AOUDE
JOINS
SELECT *
FROM table_1, table_2;
COL_1 COL_1
-------- ---------
a a
b a
c a
a b
b b
c b
a c
b c
c c
Dr M AOUDE
JOINS
• The first row of the “table_1” table was joined with every
row in the “table_2” table.
• A Cartesian product may not be useful and could be
misleading.
• Always include a WHERE clause in your JOIN statements.
SELECT *
FROM table_1, table_2;
WHERE table_1.col_1 = table_2.col_1;
col_1 col_1
-------- --------
a a
b b
c c
Dr M AOUDE
JOIN OPERATION RULES
JOINS and the SELECT Clause
Dr M AOUDE
Example
SELECT dpt_name "Department Name",
emp_last_name "Last Name",
emp_first_name "First Name"
FROM employee e, department d
WHERE e.emp_dpt_number = d.dpt_no AND
e.emp_dpt_number = 7;
Dr M AOUDE
JOINS and the WHERE Clause
Dr M AOUDE
QUALIFYING COLUMN NAMES
Dr M AOUDE
QUALIFYING COLUMN NAMES
• This error message tells you that you have included a
column name somewhere in the query that exists in more
than one table listed in the FROM clause.
• Here the error is in the WHERE clause; however, it is also
possible to make a similar error in the SELECT clause.
• The SELECT statement shown below fails to qualify the
col_1 name in the SELECT clause, and Oracle again
produces the ORA-00918 error message.
SELECT col_1
FROM table_1, table_2
WHERE table_1.col_1 = table_2.col_1;
ERROR at line 1:
ORA-00918: column ambiguously defined
Dr M AOUDE
QUALIFYING COLUMN NAMES
• An ambiguous column name is qualified by using the
DOT (.) connector to connect the table name and
column name.
• Sometimes it is easier to qualify column names by
using table alias names.
• Often, a single letter is used as an identifier to reduce
keystroke requirements .
SELECT dpt_name "Department Name",
emp_last_name "Last Name",
emp_first_name "First Name"
FROM employee e, department d
WHERE e.emp_dpt_number = d.dpt_no AND e.emp_dpt_number = 7;
Dr M AOUDE
QUALIFYING COLUMN NAMES
• The use of the letters "e" and "d" is completely
arbitrary; "t1" and "t2" or any other unique aliases
could have been used.
• The important points to learn are:
• The alias must follow a table name.
• Use a space to separate a table name and its alias.
• The alias must be unique within the SELECT statement.
• If the column names are not identical you are not
required to qualify them, although you still might
want to for documentation purposes
Dr M AOUDE
Joining More Than Two Tables
Dr M AOUDE
Joining More Than Two Tables
• The example shown in following figure joins three tables
to produce a result table based on two different
relationships.
Dr M AOUDE
Joining More Than Two Tables
• The SELECT statement to join the tables depicted
in the figure is shown here.
SELECT emp_last_name "Last Name",
emp_first_name "First Name",
1.10*emp_salary "Raised Salary", p.pro_name "Project"
FROM employee e, assignment a, project p
WHERE e.emp_ssn = a.work_emp_ssn AND
a.work_pro_number = p.pro_number AND
p.pro_name = 'Inventory';
Dr M AOUDE
Joining Tables by Using Two Columns
Dr M AOUDE
Joining Tables by Using Two Columns
Dr M AOUDE
OUTER JOIN Operations
Dr M AOUDE
OUTER JOIN Operations
Dr M AOUDE
OUTER JOINS and NULL values
SELECT emp_last_name "Last Name", emp_first_name "First
Name"
FROM employee e, dependent d
WHERE e.emp_ssn = d.dep_emp_ssn(+) AND
d.dep_name IS NULL;
Dr M AOUDE
SELF-JOIN Operations
• A SELF JOIN operation is used to produce a result table when the
relationship of interest exists among rows that are stored within a single
table.
Dr M AOUDE
SELF-JOIN Operations
SELECT e1.emp_last_name || ', ' || e1.emp_first_name
"Supervisor",
e2.emp_last_name || ', ' || e2.emp_first_name "Employee"
FROM employee e1, employee e2
WHERE e1.emp_ssn = e2.emp_superssn;
Supervisor Employee
---------------------------- -------------------
Bordoloi, Bijoy Joyner, Suzanne
Bordoloi, Bijoy Zhu, Waiman
Joyner, Suzanne Markis, Marcia
Joyner, Suzanne Amin, Hyder
Zhu, Waiman Bock, Douglas
Zhu, Waiman Joshi, Dinesh
Zhu, Waiman Prescott, Sherri
Dr M AOUDE