Sub topic 13
Common Join Pitfalls
Assume you know SELECT, WHERE, ORDER BY, primary keys, foreign keys, table aliases, and NULL. This chapter focuses on errors that make a join return too many rows, too few rows, or unclear results.
Duplicate rows from one-to-many relationships
A join returns one result row for every matching pair. If one department has 10 employees, joining that department to employees produces 10 rows for the department. The department row is repeated because each employee is a separate match.
SELECT d.department_name, e.last_name
FROM departments d
JOIN employees e
ON e.department_id = d.department_id
WHERE d.department_id = 60;This is correct when you want one row per employee. It is incorrect if you expect one row per department.
Do not add DISTINCT automatically. First decide why duplicates exist. You might need an aggregate, such as COUNT, or a more selective join condition.
SELECT d.department_name, COUNT(e.employee_id) AS employee_count
FROM departments d
LEFT JOIN employees e
ON e.department_id = d.department_id
GROUP BY d.department_name;The LEFT JOIN keeps departments with no employees. COUNT(e.employee_id) returns zero for those departments because the joined employee value is NULL.
Missing rows caused by `NULL` join keys
A normal equality condition does not match NULL values. In Oracle, NULL = NULL is not true. It evaluates to unknown.
SELECT e.last_name, d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;An employee whose department_id is NULL does not match any department in this inner join. An employee whose department ID has no corresponding department is also excluded.
Use an outer join when you need to find such rows:
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;This shows employees without a matching department. The condition in WHERE tests the null-extended department columns after the join.
| Cause | Typical result | Useful check |
|---|---|---|
| `NULL` join key | No equality match | Test with `IS NULL` |
| Missing parent row | Inner join drops the child | Use a `LEFT JOIN` |
| Non-unique parent key | Child row appears more than once | Check key uniqueness |
Ambiguous and incorrect column references
When two tables contain a column with the same name, qualify the column with its table alias. This avoids ORA-00918: column ambiguously defined and makes the query easier to read.
SELECT e.employee_id,
e.department_id,
d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;An unqualified department_id is ambiguous because both tables provide that column.
A different mistake is using the wrong column in the join. A query can run successfully while pairing unrelated rows. Confirm that the columns represent the same relationship, usually a foreign key and its referenced primary key.
Accidental Cartesian products
A Cartesian product pairs every row from one table with every row from another. It occurs with an explicit CROSS JOIN or when a normal join omits its relationship condition.
SELECT e.last_name, d.department_name
FROM employees e
CROSS JOIN departments d;If there are 100 employees and 10 departments, the result has 1,000 rows. This is useful for deliberately generating combinations, but usually signals a missing condition.
A practical checking sequence
When a join result looks wrong:
- Run each table query separately and note the row counts.
- Check whether the join columns are unique on the side you expect to be unique.
- Qualify every shared column with an alias.
- Inspect rows with
NULLjoin keys. - Add join conditions one at a time and compare row counts.
- Confirm that filters on an outer join are in the intended clause.
Exercise
A department disappears after adding WHERE e.salary > 10000 to a LEFT JOIN. What is the likely cause, and how can you keep the department?
Show answer
The WHERE condition removes rows where the employee columns are NULL. Move the salary condition into the ON clause if the department must remain visible.