Collection Contents Index Self-joins and correlation names How joins are processed pdf/chap6.pdf

User's Guide
   PART 1. Working with Databases
     CHAPTER 6. Joins: Retrieving Data from Several Tables       

Cross joins


As for other types of joins, each row in a cross join is a combination of one column from the first table and one column from the second table. Unlike other joins, a cross join contains no restrictions. All possible combinations of rows are present.

Each row of the first table appears exactly once with each row of the second table. Hence, the number of rows in the join is the product of the number of rows in the individual tables.

Inner and outer modifiers do not apply to cross joins 

Except in the presence of additional restrictions, all rows of both tables always appear in the result. Thus, the keywords INNER, LEFT OUTER and RIGHT OUTER are not applicable to cross joins.

The query

SELECT *
FROM table1 CROSS JOIN table2

has a result set as follows:

Top of page  Self-joins and cross joins

The following self-join produces a list of pairs of employees. Each employee names appears in combination with every employee name.

SELECT a.emp_fname, a.emp_lname,
      b.emp_fname, b.emp_lname
FROM employee AS a CROSS JOIN employee AS b

emp_fname

emp_lname

Emp_fname

emp_lname

Fran

Whitney

Fran

Whitney

Matthew

Cobb

Fran

Whitney

Philip

Chin

Fran

Whitney

Julie

Jordan

Fran

Whitney

Robert

Breault

Fran

Whitney

Melissa

Espinoza

Fran

Whitney

Jeannette

Bertrand

Fran

Whitney

Since the employee table has 75 rows, this join contains 75 x 75 = 5625 rows. It includes, however, rows which that list each employee with themselves. For example, it contains the row

emp_fname

emp_lname

emp_fname

emp_lname

Fran

Whitney

Fran

Whitney

To remove that list the same employee's name twice, use the following command.

SELECT a.emp_fname, a.emp_lname,
      b.emp_fname, b.emp_lname
FROM employee AS a CROSS JOIN employee AS b
WHERE a.emp_lname <> b.emp_lname
   OR a.emp_fname <> b.emp_fname

Without these rows, the join contains 75 x 74 = 5550 rows.

This join contains rows that pair each employee with every other employee, but because each pair of names can appear in two possible orders, each pair appears twice. For example, the result of the above join contains the following two rows.

emp_fname

emp_lname

emp_fname

emp_lname

Matthew

Cobb

Fran

Whitney

Fran

Whitney

Matthew

Cobb

If the order of the names is not important, you can produce a list of the (75 x 74)/2! = 2775 unique pairs.

SELECT a.emp_fname, a.emp_lname,
      b.emp_fname, b.emp_lname
FROM employee AS a CROSS JOIN employee AS b
WHERE a.emp_lname < b.emp_lname
   OR (a.emp_lname = b.emp_lname
   AND a.emp_fname < b.emp_fname)

This statement eliminates duplicate lines by selecting only those rows in which the name of employee a is alphabetically less than that of employee b.

For Info     For more information, see Self-joins and correlation names.

Top of page  

Collection Contents Index Self-joins and correlation names How joins are processed pdf/chap6.pdf