Sub topic 2
Inner joins: ANSI syntax and the comma syntax side by side
Assume you know basic SELECT, WHERE, and ORDER BY, primary and foreign keys, table aliases, and NULL. This chapter uses the Oracle sample HR tables employees and departments.
What an inner join does
An inner join combines rows when a join condition is true. It returns only matching rows. If an employee has a department_id that matches a department, both rows can appear. Employees without a matching department and departments without employees do not appear.
In the sample schema, employees.department_id refers to departments.department_id. The two columns form the join condition.
SELECT e.employee_id,
e.last_name,
d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
ORDER BY e.employee_id;Here, e and d are table aliases. An alias is a short name for a table within one statement. The ON clause states how the rows match.
The result contains columns from both tables:
| EMPLOYEE_ID | LAST_NAME | DEPARTMENT_NAME |
|---|---|---|
| 100 | King | Executive |
| 101 | Kochhar | Executive |
| 102 | De Haan | Executive |
The actual rows depend on the data in your Oracle database.
The comma syntax
Oracle also supports an older form. List both tables in the FROM clause, then put the join condition in the WHERE clause.
SELECT e.employee_id,
e.last_name,
d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id
ORDER BY e.employee_id;For this inner join, the comma syntax returns the same matching rows as the ANSI syntax. ANSI syntax is usually preferred because the relationship between tables is visible in JOIN ... ON, while row filters remain in WHERE.
| ANSI syntax | Comma syntax |
|---|---|
| `JOIN departments d` | `, departments d` |
| `ON e.department_id = d.department_id` | `WHERE e.department_id = d.department_id` |
A join condition is different from a search condition, even though both can be written with SQL predicates. The join condition connects tables. A search condition limits rows after the tables are considered.
Do not omit the join condition
If you list two tables without a condition, Oracle pairs every row from one table with every row from the other. This result is a Cartesian product, also called a cross join.
SELECT e.last_name,
d.department_name
FROM employees e, departments d;If employees has 107 rows and departments has 27 rows, this query can produce 2,889 combinations. Most combinations are unrelated. A missing or incorrect join condition is a common cause of unexpectedly large results.
The ANSI form makes an intentional cross join clearer:
SELECT e.last_name,
d.department_name
FROM employees e
CROSS JOIN departments d;Use a cross join only when every combination is meaningful.
Add filters without confusing the join
You can combine a join condition with a row filter:
SELECT e.last_name,
e.salary,
d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
WHERE e.salary > 10000
ORDER BY e.salary DESC;The ON clause connects each employee to a department. The WHERE clause keeps only employees whose salary is greater than 10,000. Because this is an inner join, moving a simple filter between ON and WHERE often produces the same result. That similarity does not apply generally to outer joins, where the location of a filter can change which unmatched rows survive.
Exercise
Rewrite this comma-syntax query using ANSI join syntax: SELECT e.lastname, d.departmentname FROM employees e, departments d WHERE e.departmentid = d.departmentid AND d.location_id = 1700;
Show answer
SELECT e.lastname, d.departmentname FROM employees e JOIN departments d ON e.departmentid = d.departmentid WHERE d.location_id = 1700;