SQL Standard

SQL Cross Join

The SQL cross join is one of the simplest joins in SQL. A cross join works by taking every row from the first able and joining it with every row from the second table. It is also known as a cartesian join as it returns a cartesian product of the rows from each table.

We will use a practical example to understand better using a cross join in SQL.

Example Usage

The first table contains customer information as shown

The second contains rental information as shown:

We can perform a cross join on the two tables as shown:

SELECT * FROM rental
CROSS JOIN customer LIMIT 10;

This should return output as shown:

rental_id |      rental_date       | inventory_id | customer_id |      return_date       | staff_id |      last_update       | customer_id | store_id | first_name | last_name |                email                | address_id | activebool | create_date |      last_update
-----------+------------------------+--------------+-------------+------------------------+----------+------------------------+-------------+----------+------------+-----------+-------------------------------------+------------+------------+-------------+------------------------
2 | 2005-05-25 00:54:33+03 |         1525 |         459 | 2005-05-28 21:40:33+03 |        1 | 2020-02-16 05:30:53+03 |           1 |        1 | MARY       | SMITH     | MARY.[email protected].org       |          5 | t          | 2020-02-14  | 2020-02-15 12:57:20+03
2 | 2005-05-25 00:54:33+03 |         1525 |         459 | 2005-05-28 21:40:33+03 |        1 | 2020-02-16 05:30:53+03 |           2 |        1 | PATRICIA   | JOHNSON   | PATRICIA.[email protected].org |          6 | t          | 2020-02-14  | 2020-02-15 12

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list