Table joins

5.4 Table joins

A simple SQL select statement is one that selects one or more columns from any single table. The next level of complexity is added when a select statement has two or more tables as source tables. This leads to multiple possibilities of how the result set will be generated.

There are two types of table joins in SQL statements:

1. Inner join

2. Outer join These are explained in more detail in the following sections.

5.4.1 Inner joins

An inner join is the most common form of join used in SQL statements. It can be classified into:

 Equi-join  Natural join  Cross join

5.4.1.1 Equi-join

This type of join happens when two tables are joined based on the equality of specified columns; for example:

SELECT * FROM student, enrollment WHERE student.enrollment_no=enrollment.enrollment_no

OR SELECT * FROM student INNER JOIN enrollment ON student.enrollment_no=enrollment.enrollment_no

5.4.1.2 Natural join

A natural join is an improved version of an equi-join where the joining column does not require specification. The system automatically selects the column with same name in the tables and applies the equality operation on it. A natural join will remove all duplicate attributes. Below is an example.

SELECT * FROM STUDENT NATURAL JOIN ENROLLMENT

Database Fundamentals 126 Natural joins bring more doubt and ambiguity than the ease it provides. For example, there

can be problems when tables to be joined have more than one column with the same name, or when the tables do not have same name for the joining column. Most commercial databases do not support natural joins.

5.4.1.3 Cross join

A cross join is simply a Cartesian product of the tables to be joined. For example:

SELECT * FROM STUDENT, ENROLLMENT

5.4.2 Outer joins

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.