English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use SQL cross join to retrieve data from two tables.
If no join condition is specified when joining two tables, the database system will merge each row of the first table with each row of the second table. This type of join is called a cross join or Cartesian product. The Venn diagram below illustrates how cross join works.
To make it easy to understand this, let's look at the followingemployeesanddepartmentstable.
+--------+--------------+------------+---------+ | emp_id | emp_name | hire_date | dept_id | +--------+--------------+------------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 4 | | 2 | Tony Montana | 2002-07-15 | 1 | | 3 | Sarah Connor | 2005-10-18 | 5 | | 4 | Rick Deckard | 2007-01-03 | 3 | | 5 | Martin Blank | 2008-06-24 | NULL | +--------+--------------+------------+---------+ | +---------+------------------+ | dept_id | dept_name | +---------+------------------+ | 1 | Administration | | 2 | Customer Service | | 3 | Finance | | 4 | Human Resources | | 5 | Sales | +---------+------------------+ | |
Table: employees | Table: departments |
The number of rows in the cross join is the product of the number of rows in each table. This is a simple example of a cross join operation.
SELECT t1.emp_id, t1.emp_name, t1.hire_date, t2.dept_name FROM employees AS t1 CROSS JOIN departments AS t2;
Tip:Cross join will create a Cartesian product or multiply all rows of one table with all rows of another table. Therefore, for example, if one table has5rows, while the other table has10rows, then the cross join query will produce50 rows, which means5and10product.
After executing the above command, you will get the following result set:
+--------+--------------+------------+------------------+ | emp_id | emp_name | hire_date | dept_name | +--------+--------------+------------+------------------+ | 1 | Ethan Hunt | 2001-05-01 | Administration | | 2 | Tony Montana | 2002-07-15 | Administration | | 3 | Sarah Connor | 2005-10-18 | Administration | | 4 | Rick Deckard | 2007-01-03 | Administration | | 5 | Martin Blank | 2008-06-24 | Administration | | 1 | Ethan Hunt | 2001-05-01 | Customer Service | | 2 | Tony Montana | 2002-07-15 | Customer Service | | 3 | Sarah Connor | 2005-10-18 | Customer Service | | 4 | Rick Deckard | 2007-01-03 | Customer Service | | 5 | Martin Blank | 2008-06-24 | Customer Service | | 1 | Ethan Hunt | 2001-05-01 | Finance | | 2 | Tony Montana | 2002-07-15 | Finance | | 3 | Sarah Connor | 2005-10-18 | Finance | | 4 | Rick Deckard | 2007-01-03 | Finance | | 5 | Martin Blank | 2008-06-24 | Finance | | 1 | Ethan Hunt | 2001-05-01 | Human Resources | | 2 | Tony Montana | 2002-07-15 | Human Resources | | 3 | Sarah Connor | 2005-10-18 | Human Resources | | 4 | Rick Deckard | 2007-01-03 | Human Resources | | 5 | Martin Blank | 2008-06-24 | Human Resources | | 1 | Ethan Hunt | 2001-05-01 | Sales | | 2 | Tony Montana | 2002-07-15 | Sales | | 3 | Sarah Connor | 2005-10-18 | Sales | | 4 | Rick Deckard | 2007-01-03 | Sales | | 5 | Martin Blank | 2008-06-24 | Sales | +--------+--------------+------------+------------------+
As you can see, the function of the cross join is not as described in the previous chapters. Since the query does not specify a join condition, each row in the employees table is merged with each row in the departments table. Therefore, do not use the cross join unless you are sure to use the Cartesian product.