Oracle
 sql >> Database >  >> RDS >> Oracle

Come trovare LEFT OUTER JOIN o RIGHT OUTER JOIN con ORACLE JOIN (+)

Si prega di chiarire come trovare correttamente il join esterno destro e sinistro con un esempio

Proverò a mostrare la differenza tra la sintassi di Oracle outer join e la Sintassi ANSI/ISO .

UNIONE ESTERNA SINISTRA -

SELECT e.last_name,
  d.department_name
FROM employees e,
  departments d
WHERE e.department_id = d.department_id(+);

SELECT e.last_name,
  d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id);

UNIONE ESTERNA DESTRA -

SELECT e.last_name,
  d.department_name
FROM employees e,
  departments d
WHERE e.department_id(+) = d.department_id;

SELECT e.last_name,
  d.department_name
FROM employees e
RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id);

UNIONE ESTERNA COMPLETA -

Prima del supporto nativo di hash full outerjoin in 11gR1, Oracle convertiva internamente il FULL OUTER JOIN nel modo seguente:

SELECT e.last_name,
  d.department_name
FROM employees e,
  departments d
WHERE e.department_id = d.department_id(+)
UNION ALL
SELECT NULL,
  d.department_name
FROM departments d
WHERE NOT EXISTS
  (SELECT 1 FROM employees e WHERE e.department_id = d.department_id
  );

SELECT e.last_name,
  d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON (e.department_id = d.department_id);

Dai un'occhiata a questo.