0% found this document useful (0 votes)
17 views1 page

Right Outer Join: Chapter 3: Selecting

The document discusses different types of outer joins in SQL including left outer joins and right outer joins. A left outer join returns all rows from the left table and matching rows from the right table or null if no match is found. A right outer join is the reverse, returning all rows from the right table and matching rows from the left or null. The document provides examples of each type of join.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Right Outer Join: Chapter 3: Selecting

The document discusses different types of outer joins in SQL including left outer joins and right outer joins. A left outer join returns all rows from the left table and matching rows from the right table or null if no match is found. A right outer join is the reverse, returning all rows from the right table and matching rows from the left or null. The document provides examples of each type of join.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Chapter 3: Selecting 85

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
In a LEFT OUTER JOIN operation, the left-hand table is called the preserved
table because every row is represented at least once in the result set. The
right-hand table is called the null-supplying table because NULL may be used
for its column values in some of the rows in the result of the join. The word
LEFT in LEFT OUTER JOIN means that the left-hand table is the preserved
table. In the example above, only two new rows were added, corresponding to
the parent rows with no corresponding child rows. The orphan child row, the
one with no corresponding parent row, still doesnt appear in the final result
because only one table can be the preserved table in a LEFT OUTER JOIN.
The LEFT OUTER JOIN operator is very commonly used to gather
required and optional data from different tables when you want to make sure all
the required data is included, even when no corresponding optional data exists.
Or to put it another way, show me all the parent and child data, including all
the childless parents.

3.4.4 RIGHT OUTER JOIN


The RIGHT OUTER JOIN operator exactly reverses the roles of the two tables
in a LEFT OUTER JOIN. For example, the following statement defines the
child table as the preserved table because it is on the right side of the RIGHT
OUTER JOIN, and the parent table is the null-supplying table because it is on
the other side:
SELECT parent.parent_key,
parent.data_1,
child.child_key,
child.parent_key
FROM parent RIGHT OUTER JOIN child ON parent.parent_key = child.parent_key
ORDER BY parent.parent_key,
child.child_key;
Now the orphan child row is included in the final result set, with parent.par-
ent_key and data_1 set to NULL, but the parent rows with no corresponding
children are missing:
parent. parent. child. child.
parent_key data_1 child_key parent_key
========== ======= ========= ==========
NULL NULL 7 NULL -- orphan
1 x 4 1 -- parent and child
1 x 5 1 -- parent and child
1 x 6 1 -- parent and child
Note that every RIGHT OUTER JOIN can be transformed into a LEFT OUTER
JOIN, which performs exactly the same function, by simply switching the table
names. For example, the following LEFT OUTER JOIN returns the same rows
as the RIGHT OUTER JOIN above:

You might also like