{"id":3957,"date":"2025-06-07T08:15:40","date_gmt":"2025-06-07T08:15:40","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3957"},"modified":"2025-06-07T09:59:20","modified_gmt":"2025-06-07T09:59:20","slug":"sql-cross-join","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-cross-join\/","title":{"rendered":"SQL CROSS JOIN"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A <strong>CROSS JOIN<\/strong> returns <strong>every possible combination<\/strong> of rows from two tables.<br>It\u2019s called a <strong>Cartesian product<\/strong> \u2013 no <code>ON<\/code> clause is required.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT *\nFROM table1\nCROSS JOIN table2;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example: All Product and Region Combinations<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT\n  p.product_name,\n  r.region_name\nFROM products p\nCROSS JOIN regions r;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 If there are 5 products and 3 regions, you get <strong>5 \u00d7 3 = 15 rows<\/strong> \u2013 every combination.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Use Cases<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Generate all combinations (e.g., size \u00d7 color)<\/li>\n\n\n\n<li>Testing and simulations<\/li>\n\n\n\n<li>Matrix-style reports<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u26a0\ufe0f Caution<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CROSS JOIN can return <strong>very large results<\/strong> if the tables are big. Use it carefully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Key Point<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td><code>CROSS JOIN<\/code><\/td><td>Returns all combinations of rows from both tables<\/td><\/tr><tr><td>Output size<\/td><td>Multiplies row counts (rows_A \u00d7 rows_B)<\/td><\/tr><tr><td>No <code>ON<\/code> clause<\/td><td>Doesn\u2019t need a join condition<\/td><\/tr><tr><td>Use cases<\/td><td>All pairings, simulations, exhaustive comparisons<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Use <code>CROSS JOIN<\/code> when <strong>you need every combination of two sets<\/strong><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A CROSS JOIN returns every possible combination of rows from two tables.It\u2019s called a Cartesian product \u2013 no ON clause is required. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example: All Product and Region Combinations \u2705 If there are 5 products and 3 regions, you get 5 \u00d7 3 = 15 rows \u2013 every combination. \ud83d\udd39 Use Cases [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3957","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}