Documente Academic
Documente Profesional
Documente Cultură
SELECT parent.parent_key,
parent.data_1,
child.child_key,
child.parent_key
FROM parent LEFT OUTER JOIN child ON parent.parent_key = child.parent_key
AND parent.data_1 = 'x'
ORDER BY parent.parent_key,
child.child_key;
In this case the result set is exactly the same as it was before:
parent. parent. child. child.
parent_key data_1 child_key parent_key
========== ======= ========= ==========
1 x 4 1 -- parent and child
1 x 5 1 -- parent and child
1 x 6 1 -- parent and child
2 x NULL NULL -- parent with no children
3 y NULL NULL -- parent with no children
The fact that a row with parent.data_1 = 'y' is included even though the ON con-
dition specified only rows with 'x' were to be included often comes as a surprise.
Its the way an OUTER JOIN works, and its the way its supposed to work, but
it is often not exactly what you want.
Tip: Be very careful what you code in the ON condition of an OUTER JOIN. A
good rule of thumb is to only code conditions that affect how rows from both
tables are joined, not conditions affecting only one or the other table. If you want
to eliminate rows in one or the other table before the OUTER JOIN is applied,
use a derived table or a view.