{"id":5685,"date":"2017-02-13T02:59:07","date_gmt":"2017-02-13T09:59:07","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=5685"},"modified":"2023-10-29T02:48:15","modified_gmt":"2023-10-29T09:48:15","slug":"mysql-cross-join","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-cross-join\/","title":{"rendered":"MySQL CROSS JOIN"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the MySQL <code>CROSS JOIN<\/code> clause and how to use it more effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL CROSS JOIN clause<\/h2>\n\n\n\n<p>Suppose you join two tables using the <code>CROSS JOIN<\/code> clause. The result set will include all rows from both tables, where each row is the combination of the row in the first table with the row in the second table.&nbsp;In general, if each table has <code>n<\/code> and <code>m<\/code> rows respectively, the result set will have <code>nxm<\/code> rows.<\/p>\n\n\n\n<p>In other words, the <code>CROSS JOIN<\/code> clause returns a <strong>Cartesian product<\/strong> of rows from the joined tables.<\/p>\n\n\n\n<p>The following illustrates the syntax of the <code>CROSS JOIN<\/code> clause that joins two tables <code>t1<\/code> and <code>t2<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> select_list \n<span class=\"hljs-keyword\">FROM<\/span> t1\n<span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">JOIN<\/span> t2;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that different from the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-inner-join\/\"><code>INNER JOIN<\/code><\/a>,&nbsp;<a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-left-join\/\"><code>LEFT JOIN<\/code><\/a> , and <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-right-join\/\">RIGHT JOIN<\/a><\/code> clauses, the <code>CROSS JOIN<\/code> clause does not have a join predicate. In other words, it does not have the <code>ON<\/code> or <code>USING<\/code> clause.<\/p>\n\n\n\n<p>If you add a <code>WHERE<\/code> clause, in case table <code>t1<\/code> and <code>t2<\/code> has a relationship, the <code>CROSS JOIN<\/code> works like the <code>INNER JOIN<\/code> clause as shown in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> select_list \n<span class=\"hljs-keyword\">FROM<\/span> t1\n<span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">JOIN<\/span> t2\n<span class=\"hljs-keyword\">WHERE<\/span> t1.id = t2.id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">MySQL CROSS JOIN clause examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples to understand how the cross join works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Simple cross join example<\/h3>\n\n\n\n<p>We&#8217;ll use a cross join to create a deck of 52 playing cards.<\/p>\n\n\n\n<p>First, create a table that stores suits:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> suits (\n    suit_id <span class=\"hljs-built_in\">INT<\/span>,\n    suit_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">10<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a table to store ranks:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> ranks (\n    rank_id <span class=\"hljs-built_in\">INT<\/span>,\n    rank_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">5<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, insert data into the suits and ranks table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> suits (suit_id, suit_name) <span class=\"hljs-keyword\">VALUES<\/span>\n    (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'Hearts'<\/span>),\n    (<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'Diamonds'<\/span>),\n    (<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">'Clubs'<\/span>),\n    (<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-string\">'Spades'<\/span>);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> ranks (rank_id, rank_name) <span class=\"hljs-keyword\">VALUES<\/span>\n    (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'Ace'<\/span>),\n    (<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'2'<\/span>),\n    (<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">'3'<\/span>),\n    (<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-string\">'4'<\/span>),\n    (<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'5'<\/span>),\n    (<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-string\">'6'<\/span>),\n    (<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-string\">'7'<\/span>),\n    (<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-string\">'8'<\/span>),\n    (<span class=\"hljs-number\">9<\/span>, <span class=\"hljs-string\">'9'<\/span>),\n    (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-string\">'10'<\/span>),\n    (<span class=\"hljs-number\">11<\/span>, <span class=\"hljs-string\">'Jack'<\/span>),\n    (<span class=\"hljs-number\">12<\/span>, <span class=\"hljs-string\">'Queen'<\/span>),\n    (<span class=\"hljs-number\">13<\/span>, <span class=\"hljs-string\">'King'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, use a cross to combine the suits and ranks to create a deck of 52 playing cards:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n  suit_name, \n  rank_name \n<span class=\"hljs-keyword\">FROM<\/span> \n  suits <span class=\"hljs-keyword\">CROSS<\/span> \n  <span class=\"hljs-keyword\">JOIN<\/span> ranks \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  suit_name, \n  rank_name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+-----------+-----------+\n| suit_name | rank_name |\n+-----------+-----------+\n| Clubs     | 10        |\n| Clubs     | 2         |\n| Clubs     | 3         |\n| Clubs     | 4         |\n| Clubs     | 5         |\n| Clubs     | 6         |\n| Clubs     | 7         |\n| Clubs     | 8         |\n| Clubs     | 9         |\n| Clubs     | Ace       |\n| Clubs     | Jack      |\n| Clubs     | King      |\n| Clubs     | Queen     |\n| Diamonds  | 10        |\n| Diamonds  | 2         |\n| Diamonds  | 3         |\n| Diamonds  | 4         |\n| Diamonds  | 5         |\n| Diamonds  | 6         |\n| Diamonds  | 7         |\n| Diamonds  | 8         |\n| Diamonds  | 9         |\n| Diamonds  | Ace       |\n| Diamonds  | Jack      |\n| Diamonds  | King      |\n| Diamonds  | Queen     |\n| Hearts    | 10        |\n| Hearts    | 2         |\n| Hearts    | 3         |\n| Hearts    | 4         |\n| Hearts    | 5         |\n| Hearts    | 6         |\n| Hearts    | 7         |\n| Hearts    | 8         |\n| Hearts    | 9         |\n| Hearts    | Ace       |\n| Hearts    | Jack      |\n| Hearts    | King      |\n| Hearts    | Queen     |\n| Spades    | 10        |\n| Spades    | 2         |\n| Spades    | 3         |\n| Spades    | 4         |\n| Spades    | 5         |\n| Spades    | 6         |\n| Spades    | 7         |\n| Spades    | 8         |\n| Spades    | 9         |\n| Spades    | Ace       |\n| Spades    | Jack      |\n| Spades    | King      |\n| Spades    | Queen     |\n+-----------+-----------+\n52 rows in set (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this query, we use a cross join to combine each suit from the suits table with each rank from the ranks table, resulting in a Cartesian product that pairs every suit with every rank.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) A complex cross join example<\/h3>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-database\/\">create a new database<\/a> <code>salesdb<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> salesdb;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, switch the current data to the new database <code>salesdb<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">USE<\/span> salesdb;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create new tables<\/a> in the <code>salesdb<\/code> database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span> AUTO_INCREMENT,\n    product_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>),\n    price <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">13<\/span>,<span class=\"hljs-number\">2<\/span> )\n);\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> stores (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span> AUTO_INCREMENT,\n    store_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>)\n);\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> sales (\n    product_id <span class=\"hljs-built_in\">INT<\/span>,\n    store_id <span class=\"hljs-built_in\">INT<\/span>,\n    quantity <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">13<\/span> , <span class=\"hljs-number\">2<\/span> ) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    sales_date <span class=\"hljs-built_in\">DATE<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    PRIMARY <span class=\"hljs-keyword\">KEY<\/span> (product_id , store_id),\n    <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (product_id)\n        <span class=\"hljs-keyword\">REFERENCES<\/span> products (<span class=\"hljs-keyword\">id<\/span>)\n        <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span> <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>,\n    <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (store_id)\n        <span class=\"hljs-keyword\">REFERENCES<\/span> stores (<span class=\"hljs-keyword\">id<\/span>)\n        <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span> <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here are the descriptions of the tables:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The table <code>products<\/code> contains the product master data that includes product id, product name, and sales price.<\/li>\n\n\n\n<li>The table <code>stores<\/code> contains the stores where the products are sold.<\/li>\n\n\n\n<li>The table <code>sales<\/code> contains the products that are sold in a particular store by quantity and date.<\/li>\n<\/ul>\n\n\n\n<p>Fourth, insert data into the three tables. <\/p>\n\n\n\n<p>Suppose that we have three products <code>iPhone<\/code>, <code>iPad<\/code> and <code>Macbook Pro<\/code> which are sold in two stores <code>North<\/code> and <code>South<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(product_name, price)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'iPhone'<\/span>, <span class=\"hljs-number\">699<\/span>),\n      (<span class=\"hljs-string\">'iPad'<\/span>,<span class=\"hljs-number\">599<\/span>),\n      (<span class=\"hljs-string\">'Macbook Pro'<\/span>,<span class=\"hljs-number\">1299<\/span>);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> stores(store_name)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'North'<\/span>),\n      (<span class=\"hljs-string\">'South'<\/span>);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> sales(store_id,product_id,quantity,sales_date)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-string\">'2017-01-02'<\/span>),\n      (<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">15<\/span>,<span class=\"hljs-string\">'2017-01-05'<\/span>),\n      (<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">25<\/span>,<span class=\"hljs-string\">'2017-01-05'<\/span>),\n      (<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">30<\/span>,<span class=\"hljs-string\">'2017-01-02'<\/span>),\n      (<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">35<\/span>,<span class=\"hljs-string\">'2017-01-05'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, the following statement returns the total sales for each product in each store:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    store_name,\n    product_name,\n    <span class=\"hljs-keyword\">SUM<\/span>(quantity * price) <span class=\"hljs-keyword\">AS<\/span> revenue\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    products <span class=\"hljs-keyword\">ON<\/span> products.id = sales.product_id\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    stores <span class=\"hljs-keyword\">ON<\/span> stores.id = sales.store_id\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> store_name , product_name; \n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"262\" height=\"137\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/02\/MySQL-CROSS-JOIN-GROUP-BY-example.png\" alt=\"MySQL CROSS JOIN GROUP BY example\" class=\"wp-image-5690\" title=\"MySQL CROSS JOIN GROUP BY example\"\/><\/figure>\n\n\n\n<p>Now, what if you wish to determine which store had no sales of a particular product? The previously mentioned statement is unable to address this query.<\/p>\n\n\n\n<p>To solve the problem, you can use the <code>CROSS JOIN<\/code> clause.<\/p>\n\n\n\n<p>Sixth, use the <code>CROSS JOIN<\/code> clause to get the combination of all stores and products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    store_name, product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    stores <span class=\"hljs-keyword\">AS<\/span> a\n        <span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    products <span class=\"hljs-keyword\">AS<\/span> b;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"185\" height=\"158\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/02\/MySQL-CROSS-JOIN-stores-and-products.png\" alt=\"MySQL CROSS JOIN stores and products\" class=\"wp-image-5688\" title=\"MySQL CROSS JOIN stores and products\"\/><\/figure>\n\n\n\n<p>Next, join the result of the query above with a query that returns the total of sales by store and product:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    b.store_name,\n    a.product_name,\n    <span class=\"hljs-keyword\">IFNULL<\/span>(c.revenue, <span class=\"hljs-number\">0<\/span>) <span class=\"hljs-keyword\">AS<\/span> revenue\n<span class=\"hljs-keyword\">FROM<\/span>\n    products <span class=\"hljs-keyword\">AS<\/span> a\n        <span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    stores <span class=\"hljs-keyword\">AS<\/span> b\n        <span class=\"hljs-keyword\">LEFT<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    (<span class=\"hljs-keyword\">SELECT<\/span> \n        stores.id <span class=\"hljs-keyword\">AS<\/span> store_id,\n        products.id <span class=\"hljs-keyword\">AS<\/span> product_id,\n        store_name,\n            product_name,\n            <span class=\"hljs-keyword\">ROUND<\/span>(<span class=\"hljs-keyword\">SUM<\/span>(quantity * price), <span class=\"hljs-number\">0<\/span>) <span class=\"hljs-keyword\">AS<\/span> revenue\n    <span class=\"hljs-keyword\">FROM<\/span>\n        sales\n    <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> products <span class=\"hljs-keyword\">ON<\/span> products.id = sales.product_id\n    <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> stores <span class=\"hljs-keyword\">ON<\/span> stores.id = sales.store_id\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> stores.id, products.id, store_name , product_name) <span class=\"hljs-keyword\">AS<\/span> c <span class=\"hljs-keyword\">ON<\/span> c.store_id = b.id\n        <span class=\"hljs-keyword\">AND<\/span> c.product_id= a.id\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> b.store_name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"242\" height=\"152\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/02\/MySQL-CROSS-JOIN-query-example.png\" alt=\"MySQL CROSS JOIN query example\" class=\"wp-image-5689\" title=\"MySQL CROSS JOIN query example\"\/><\/figure>\n\n\n\n<p>Note that the query used the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-ifnull\/\"><code>IFNULL<\/code><\/a> function to return 0 if the revenue is <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-null\/\">NULL<\/a><\/code>&nbsp;(in case the store had no sales).<\/p>\n\n\n\n<p>By using the <code>CROSS JOIN<\/code> clause this way, you can answer a wide range of questions e.g., find the sales revenue by salesman, month even if the salesman has no sales in a particular month.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A cross join combines each row from a table with each row from another table, resulting in a Cartesian product.<\/li>\n\n\n\n<li>Use the <code>CROSS JOIN<\/code> clause to perform a cross join.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5685\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-cross-join\/\"\n\t\t\t\tdata-post-title=\"MySQL CROSS JOIN\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5685\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-cross-join\/\"\n\t\t\t\tdata-post-title=\"MySQL CROSS JOIN\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the MySQL CROSS JOIN clause to create the Cartesian product of rows from the joined tables.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":19,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5685","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL CROSS JOIN<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the MySQL CROSS JOIN clause to create the Cartesian product of rows from the joined tables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL CROSS JOIN\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the MySQL CROSS JOIN clause to create the Cartesian product of rows from the joined tables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-29T09:48:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/02\/MySQL-CROSS-JOIN-GROUP-BY-example.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cross-join\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cross-join\\\/\",\"name\":\"MySQL CROSS JOIN\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cross-join\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cross-join\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/MySQL-CROSS-JOIN-GROUP-BY-example.png\",\"datePublished\":\"2017-02-13T09:59:07+00:00\",\"dateModified\":\"2023-10-29T09:48:15+00:00\",\"description\":\"This tutorial shows you how to use the MySQL CROSS JOIN clause to create the Cartesian product of rows from the joined tables.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cross-join\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cross-join\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cross-join\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/MySQL-CROSS-JOIN-GROUP-BY-example.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/MySQL-CROSS-JOIN-GROUP-BY-example.png\",\"width\":262,\"height\":137,\"caption\":\"MySQL CROSS JOIN GROUP BY example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cross-join\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL CROSS JOIN\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL CROSS JOIN","description":"This tutorial shows you how to use the MySQL CROSS JOIN clause to create the Cartesian product of rows from the joined tables.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/","og_locale":"en_US","og_type":"article","og_title":"MySQL CROSS JOIN","og_description":"This tutorial shows you how to use the MySQL CROSS JOIN clause to create the Cartesian product of rows from the joined tables.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-10-29T09:48:15+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/02\/MySQL-CROSS-JOIN-GROUP-BY-example.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/","url":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/","name":"MySQL CROSS JOIN","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/02\/MySQL-CROSS-JOIN-GROUP-BY-example.png","datePublished":"2017-02-13T09:59:07+00:00","dateModified":"2023-10-29T09:48:15+00:00","description":"This tutorial shows you how to use the MySQL CROSS JOIN clause to create the Cartesian product of rows from the joined tables.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-cross-join\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/02\/MySQL-CROSS-JOIN-GROUP-BY-example.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/02\/MySQL-CROSS-JOIN-GROUP-BY-example.png","width":262,"height":137,"caption":"MySQL CROSS JOIN GROUP BY example"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-cross-join\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL CROSS JOIN"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=5685"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5685\/revisions"}],"predecessor-version":[{"id":11943,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5685\/revisions\/11943"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=5685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}