Sub topic 10
USING and NATURAL JOIN
Assume you already know basic SELECT, WHERE, and ORDER BY clauses, primary and foreign keys, table aliases, and the meaning of NULL. This chapter shows two shorthand forms for joins in Oracle: USING and NATURAL JOIN. Both can make simple equijoins shorter, but both require careful column naming.
USING: name the shared join column
Use USING when two tables have a join column with the same name. The column must be an equality key, such as department_id. Oracle displays that shared column once instead of displaying one copy from each table.
SELECT department_id,
d.department_name,
e.last_name
FROM employees e
JOIN departments d
USING (department_id)
ORDER BY department_id, e.last_name;This query is equivalent to the following ANSI join:
SELECT e.department_id,
d.department_name,
e.last_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
ORDER BY e.department_id, e.last_name;The important difference is how you refer to the shared column. A column named in USING must not be qualified with a table alias. Therefore, use departmentid, not e.departmentid or d.department_id, in the SELECT list, WHERE clause, or ORDER BY clause.
Columns that are not listed in USING can still use aliases:
SELECT department_id,
d.department_name,
e.last_name
FROM employees e
LEFT JOIN departments d
USING (department_id)
WHERE e.salary > 10000;USING can also be used with an outer join. The join type still controls which unmatched rows remain.
| Join form | Matching rule | Common-column output |
|---|---|---|
| JOIN ... USING (department_id) | Equal department_id values | One department_id column |
| JOIN ... ON ... | Condition written explicitly | Columns may appear twice |
| NATURAL JOIN | All same-named columns | One column for each shared name |
NATURAL JOIN: automatic matching by name
A NATURAL JOIN tells Oracle to join every column that has the same name in both tables. It also returns each shared column only once.
SELECT department_id,
department_name,
last_name
FROM employees
NATURAL JOIN departments
ORDER BY department_id, last_name;If employees and departments share only departmentid, Oracle treats this as an equality join on departmentid. However, the query does not say that directly. If both tables later gain another column with the same name, NATURAL JOIN may silently add that column to the join condition.
For example, suppose both tables acquire a column named regionid. The NATURAL JOIN then requires both departmentid and regionid to match. Rows that previously matched may disappear if their regionid values differ or if either value is NULL.
A NATURAL JOIN is therefore sensitive to schema changes. It can also fail when same-named columns have incompatible data types. Use it only when all same-named columns are deliberately part of the relationship and the table design is stable.
Choosing the form
Prefer JOIN with ON when the relationship uses differently named columns, several conditions, ranges, or expressions. Prefer USING when one or more same-named equality columns clearly define the relationship. Treat NATURAL JOIN as specialized shorthand rather than a default style.
Remember that NULL never equals NULL in a normal join condition. If department_id is NULL on both sides, those rows do not match with USING or NATURAL JOIN.
Exercise
Rewrite this query with USING: SELECT e.lastname, d.departmentname FROM employees e JOIN departments d ON e.departmentid = d.departmentid;
Show answer
SELECT e.lastname, d.departmentname FROM employees e JOIN departments d USING (department_id);