O'Reilly logo

Sams Teach Yourself MySQL by Chris Newman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cross Joins

You saw in the previous lesson that if no relationship between joined tables is given in the WHERE clause, a Cartesian product or cross join is produced.

The ON keyword is not required to appear after the JOIN keyword, so the same result can be obtained using INNER JOIN and no ON clause.

However, for readability, you can use the CROSS JOIN keyword to show in your query that the Cartesian product is the desired result. The following example reproduces the cross join between sample tables t1 and t2 from the previous lesson:

mysql>  SELECT *
    ->  FROM t1
    ->  CROSS JOIN t2; +----+--------+----+--------+ | id | letter | id | letter | +----+--------+----+--------+ | 1 | A | 1 | X | | 2 | B | 1 | X | | 3 | C | 1 | X | | 1 | A | 2 | Y | | 2 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required