Sub topic 3
NULLs and the NOT IN Trap
This chapter assumes you know joins, GROUP BY, aggregate functions, EXISTS, and the Oracle HR sample schema (EMPLOYEES, DEPARTMENTS).
NULLs and the NOT IN trap
A NULL means unknown. Any comparison with NULL yields unknown, not true. NOT IN is true only when a value differs from every item in the list, so one NULL in the list makes the test fail for every row.
-- Returns no rows: MANAGER_ID contains NULL for the top-level employee.
SELECT last_name
FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees);-- Employees who manage nobody, written safely:
SELECT e.last_name
FROM employees e
WHERE NOT EXISTS (SELECT 1
FROM employees r
WHERE r.manager_id = e.employee_id);Multi-column subqueries
A subquery can return several columns. The outer row must match the columns as a pair.
SELECT last_name, department_id, salary
FROM employees
WHERE (department_id, salary) IN (SELECT department_id, MAX(salary)
FROM employees
GROUP BY department_id);This returns the top earner of each department, including ties.
Subqueries in INSERT, UPDATE and DELETE
Subqueries can filter rows or supply values in DML statements. This raises the salary of employees paid below their department average:
UPDATE employees e
SET e.salary = e.salary * 1.05
WHERE e.salary < (SELECT AVG(i.salary)
FROM employees i
WHERE i.department_id = e.department_id);Naming subqueries with WITH
The WITH clause names an intermediate result so the main query can read it.
WITH dept_avg AS (
SELECT department_id, AVG(salary) AS avg_sal
FROM employees
GROUP BY department_id
)
SELECT e.last_name, e.salary, a.avg_sal
FROM employees e
JOIN dept_avg a ON a.department_id = e.department_id
WHERE e.salary > a.avg_sal;LATERAL and CROSS APPLY
Oracle Database 12c and later let an inline view refer to columns of the table before it. This query returns the two highest-paid employees of each department:
SELECT d.department_name, t.last_name, t.salary
FROM departments d
CROSS APPLY (SELECT e.last_name, e.salary
FROM employees e
WHERE e.department_id = d.department_id
ORDER BY e.salary DESC
FETCH FIRST 2 ROWS ONLY) t;How the optimizer treats subqueries
Oracle often rewrites a subquery into a join internally, a process called unnesting. Do not assume the rewrite happens. Check the plan with EXPLAIN PLAN FOR followed by SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);.
Choosing between subqueries and joins
- Use a join when you need columns from both tables.
- Use EXISTS when you only test whether matching rows exist. It does not multiply rows the way a join can.
- Use NOT EXISTS instead of NOT IN when the subquery column can contain NULL.
- Use WITH when an intermediate result is reused or makes the query easier to read.
- A subquery that returns more than one row where one value is expected raises ORA-01427.
Exercise
Why does NOT IN return no rows when its subquery returns a NULL, and how do you list employees who manage nobody using NOT EXISTS?
Show answer
NOT IN is true only when a value differs from every item in the list. Comparing with NULL yields unknown, so no row qualifies. NOT EXISTS checks only whether a matching row exists, so a NULL in the subquery does not affect it. Use SELECT e.lastname FROM employees e WHERE NOT EXISTS (SELECT 1 FROM employees r WHERE r.managerid = e.employee_id);