P. 1
Database Fundamentals

Database Fundamentals

|Views: 72|Likes:
Published by mmmaheshwari
DB2 database
DB2 database

More info:

Published by: mmmaheshwari on Mar 02, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

04/07/2015

pdf

text

original

An outer join is a specialized form of join used in SQL statements. In an outer joins, the
first table specified in an SQL statement in the FROM clause is referred as the LEFT table
and the remaining table is referred as the RIGHT table. An outer join is of the following
three types:

Left outer join
Right outer join
Full outer join

Figure 5.1 shows a diagram depicting the three outer join types.

Figure 5.1 - The different outer join types

Chapter 5 – Introduction to SQL 127

In the next sections, we describe each of these types in more detail. For a better
understanding of each case, examples are provided using the tables shown in Figure 5.2.

Figure 5.2 - Input tables to use in outer-join examples

5.4.2.1 Left outer join

In a left outer join, the result set is a union of the results of an equi-join, including any non-
matching rows from the LEFT table. For example, the following statement would return the
rows shown in Figure 5.3.

SELECT *

FROM STUDENT

LEFT OUTER JOIN ENROLLMENT

ON STUDENT.ENROLLMENT_NO = ENROLLMENT_NO

Figure 5.3 - Output of a left outer join

5.4.2.2 Right outer join

In a right outer join, the result set is the union of results of an equi-join, including any non-
matching rows from the RIGHT table. For example, the following statement would return
the rows shown in Figure 5.4.

SELECT *

FROM STUDENT

RIGHT OUTER JOIN ENROLLMENT

Database Fundamentals

128

ON STUDENT.ENROLLMENT_NO = ENROLLMENT_NO

Figure 5.4 - Output of a right outer join

5.4.2.3 Full outer join

In a full outer join, the result set is the union of results of an equi- join, including any non-
matching rows of the LEFT and the RIGHT table. For example, the following statement
would return the rows shown in Figure 5.5.

SELECT *

FROM STUDENT

FULL OUTER JOIN ENROLLMENT

ON STUDENT.ENROLLMENT_NO = ENROLLMENT_NO

Figure 5.5 - Output of a full outer join

Different outer joins return different data sets; therefore, these should be used explicitly as
per the business requirements. For example, if we need a list of students who have
enrolled in any subject as well as those who have not yet enrolled, then probably what we
need is a left outer join.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->