JOIN CLAUSES
NATURAL JOIN
• A natural join is based on all columns in two
tables that have the same name and selects
rows from the two tables that have equal
values in all matched columns.
• when using a natural join, it is possible to join
the tables without having to explicitly specify
the columns in the corresponding table.
• the names and data types of both columns must be the
same.
SELECT first_name, last_name, job_id, job_title
FROM employees NATURAL JOIN jobs WHERE
department_id > 80;
• the natural join column does not have to appear
in the SELECT clause.
SELECT department_name, city
FROM departments NATURAL JOIN locations;
CROSS JOIN
• The ANSI/ISO SQL: 1999 SQL CROSS JOIN joins each
row in one table to every row in the other table.
• The result set represents all possible row
combinations from the two tables.
• If you CROSS JOIN a table with 20 rows with a table
with 100 rows, the query will return 2000 rows.
SELECT last_name, department_name
FROM employees CROSS JOIN departments;
USING Clause
• In a natural join, if the tables have columns with the
same names but different data types, the join causes
an error.
• To avoid this situation, the join clause can be
modified with a USING clause.
• The USING clause specifies the columns that should
be used for the join.
• The columns referenced in the USING clause should
not have a qualifier (table name or alias) anywhere in
the SQL statement.
SELECT first_name, last_name, department_id,
department_name
FROM employees JOIN departments USING
(department_id);
Using Where
SELECT first_name, last_name, department_id,
department_name
FROM employees JOIN departments USING
(department_id)
WHERE last_name = 'Higgins';
ON Clause
• What if the columns to be joined have different
names, or if the join uses non-equality comparison
operators such as <, >, or BETWEEN ?
• We can't use USING, so instead we use an ON clause.
• This allows a greater variety of join conditions to be
specified.
• The ON clause also allows us to use WHERE to restrict
rows from one or both tables.
• A join ON clause is required when the
common columns have different names in the
two tables.
SELECT last_name, job_title
FROM employees e JOIN jobs j
ON (e.job_id = j.job_id);
ON CLAUSE with WHERE CLAUSE
SELECT last_name, job_title
FROM employees e JOIN jobs j
ON (e.job_id = j.job_id)
WHERE last_name LIKE 'H%';
ON Clause with non-equality operator
SELECT last_name, salary, grade_level, lowest_sal,
highest_sal
FROM employees JOIN job_grades
ON (salary BETWEEN lowest_sal AND highest_sal);
Joining Three Tables
SELECT last_name, department_name AS
"Department", city
FROM employees JOIN departments USING
(department_id)
JOIN locations USING (location_id);
INNER And OUTER Joins
• In ANSI-99 SQL, a join of two or more tables that
returns only the matched rows is called an inner
join.
• When a join returns the unmatched rows as well
as the matched rows, it is called an outer join.
• Outer join syntax uses the terms "left, full, and
right".
SELECT e.last_name, d.department_id, d.department_name
FROM employees e
LEFT OUTER JOIN departments d ON (e.department_id =
d.department_id);
Employees left outer join
departments => ambil tabel
sebelah kiri/sebelum join
Left
SELECT e.last_name, d.department_id, d.department_name
FROM employees e
RIGHT OUTER JOIN departments d ON (e.department_id =
d.department_id);
Employees right outer join
departments => ambil tabel
sebelah kanan/setelah join
FULL OUTER Join
• It is possible to create a join condition to retrieve
all matching rows and all unmatched rows from
both tables.
• Using a full outer join solves this problem.
• The result set of a full outer join includes all rows
from a left outer join and all rows from a right
outer join combined together without duplication.
SELECT e.last_name, d.department_id, d.department_name
FROM employees e
FULL OUTER JOIN departments d ON (e.department_id =
d.department_id);
Self-Joins
• In data modeling, it was
sometimes necessary to show
an entity with a relationship
to itself.
• For example, an employee can
also be a manager.
• We showed this using the
recursive or “pig's ear”
relationship.
• A self-join is use to join a table to itself as if it
was two tables.
• To join a table to itself, the table is given two
names or aliases. This will make the database
"think" that there are two tables.
SELECT worker.last_name || ' works for ' ||
manager.last_name
AS "Works for"
FROM employees worker JOIN employees manager
ON (worker.manager_id = manager.employee_id);
• Choose alias names that relate to the data's
association with that table.
SELECT worker.last_name, worker.manager_id,
manager.last_name
AS "Manager name"
FROM employees worker JOIN employees manager
ON (worker.manager_id = manager.employee_id);
Hierarchical Queries
• With hierarchical queries,
we can also see who that
manager works for, and so
on.
• With this type of query, we
can build an Organization
Chart showing the structure
of a company or a
department.
• Using hierarchical queries, you can retrieve data based on a
natural hierarchical relationship between rows in a table.
• A relational database does not store records in a
hierarchical way.
• However, where a hierarchical relationship exists between
the rows of a single table, a process called tree walking
enables the hierarchy to be constructed.
• A hierarchical query is a method of reporting the branches
of a tree in a specific order.
• Hierarchical queries have their own new
keywords: START WITH, CONNECT BY PRIOR, and LEVEL.
• START WITH identifies which row to use as the Root for
the tree it is constructing,
• CONNECT BY PRIOR explains how to do the inter-row
joins, and
• LEVEL specifies how many branches deep the tree will
traverse.
SELECT employee_id, last_name, job_id, manager_id
FROM employees
START WITH employee_id = 100
CONNECT BY PRIOR employee_id = manager_id
SELECT last_name ||' reports to ' || PRIOR last_name
AS "Walk Top Down"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id;
• LEVEL is a pseudo-column used with
hierarchical queries, and it counts the number
of steps it has taken from the root of the tree.
SELECT LEVEL, last_name || ' reports to ' || PRIOR last_name
AS "Walk Top Down"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id;
SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_')
AS "Org Chart"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id;
SELECT LPAD(last_name, LENGTH(last_name)+ (LEVEL*2)-2,'_') AS "Org_Chart"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id;
SELECT LPAD(last_name, LENGTH(last_name) + (LEVEL*2)-2, '_') AS “ORG_CHART”
FROM employees
START WITH last_name = 'Grant'
CONNECT BY employee_id = PRIOR manager_id
Oracle Equijoin and Cartesian Product
• Basic Format Join Syntax :
SELECT [Link], [Link]
FROM table1, table2
WHERE table1.column1 = table2.column2;
EQUIJOIN
• An equijion is equavalent to ANSI:
– NATURAL JOIN
– JOIN USING
– JOIN ON (when the join condition uses "=")
• What? The SELECT clause specifies the column
names to display.
• Where? The FROM clause specifies the tables that
the database must access, separated by commas.
• How? The WHERE clause specifies how the tables
are to be joined.
• An Equijoin uses the equals operator to specify the
join condition.
CARTESIAN PRODUCT JOIN
• If two tables in a join query have no join condition
specified in the WHERE clause or the join condition is
invalid, the Oracle Server returns the Cartesian product of
the two tables.
• This is a combination of each row of one table with each
row of the other.
• A Cartesian product is equivalent to an ANSI CROSS JOIN.
• To avoid a Cartesian product, always include a valid join
condition in a WHERE clause.
SELECT employees.last_name, departments.department_name
FROM employees, departments;
SELECT employees.last_name, employees.job_id, jobs.job_title
FROM employees, jobs
WHERE employees.job_id = jobs.job_id
AND employees.department_id = 80;
SELECT last_name, e.job_id, job_title
FROM employees e, jobs j
WHERE e.job_id = j.job_id
AND department_id = 80;
• When column names are not duplicated between two tables,
you do not need to add the table name or alias to the column
name.
• We need to join three tables: employees,
departments and locations.
SELECT last_name, city
FROM employees e, departments d, locations l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id;
Nonequijoin
• Example:
– Suppose we want to know the grade_level
for each employee's salary.
– The job_grades table does not have a
common column with the employees table.
– Using a nonequijoin allows us to join the
two tables.
• Although comparison conditions such as <=
and >= can be used, BETWEEN...AND is a more
effective way to execute a nonequijoin.
• A nonequijoin is equivalent to an ANSI JOIN
ON (where the condition used is something
other than equals).
SELECT last_name, salary, grade_level, lowest_sal, highest_sal
FROM employees, job_grades
WHERE (salary BETWEEN lowest_sal AND highest_sal);
• An outer join is used to see rows that have a
corresponding value in another table plus those
rows in one of the tables that have no matching
value in the other table.
• To indicate which table may have missing data using
Oracle Join Syntax, add a plus sign (+) after the
table's column name in the WHERE clause of the
query.
SELECT e.last_name, d.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id = = LEFT OUTER JOIN
d.department_id(+);
SELECT e.last_name, d.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = = RIGHT OUTER JOIN
d.department_id;
• It is not possible to have the equivalent of a FULL OUTER JOIN by adding a (+) sign
to both columns in the join condition.
SELECT e.last_name, d.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id(+);