CROSS JOIN

CROSS JOIN

Multi-select
Completed
Status
Done

MySQL CROSS JOIN Explained

CROSS JOIN in MySQL creates a combination of all rows from two tables. This is called a Cartesian product. If one table has n rows and another has m rows, the result will have n × m rows.

CROSS JOIN Syntax

sql
CopyEdit
SELECT select_list
FROM table1
CROSS JOIN table2;

This generates every possible pairing of rows from table1 and table2. Note: Unlike other joins like INNER JOIN or LEFT JOIN, the CROSS JOIN does not use an ON condition.

Practical Example Using Products and Stores

Create the Tables

sql
CopyEdit
CREATE TABLE products (
    product_id INT,
    product_name VARCHAR(100)
);

CREATE TABLE stores (
    store_id INT,
    store_name VARCHAR(100)
);

Insert Data into the Tables

sql
CopyEdit
INSERT INTO products (product_id, product_name) VALUES
    (1, 'iPhone'),
    (2, 'iPad'),
    (3, 'Macbook Pro');

INSERT INTO stores (store_id, store_name) VALUES
    (1, 'North Store'),
    (2, 'South Store');

Perform a CROSS JOIN

To combine all products with all stores:

sql
CopyEdit
SELECT
    product_name,
    store_name
FROM
    products
CROSS JOIN
    stores;

Output:

product_name
store_name
iPhone
North Store
iPhone
South Store
iPad
North Store
iPad
South Store
Macbook Pro
North Store
Macbook Pro
South Store

Here, every product is paired with every store, creating a total of 3 × 2 = 6 rows.

Example with Additional Filtering

If you want to make the CROSS JOIN behave like an INNER JOIN, you can add a WHERE clause. For example:

sql
CopyEdit
SELECT
    product_name,
    store_name
FROM
    products
CROSS JOIN
    stores
WHERE
    store_id = product_id;

This filters the rows where store_id matches product_id.

Use Case: Find Unsold Products

You can use a CROSS JOIN to determine which products were not sold in any store.

Combine Stores and Products

sql
CopyEdit
SELECT
    store_name,
    product_name
FROM
    stores
CROSS JOIN
    products;

Check Sales Data

To include sales data and find unsold products, use a LEFT JOIN with the CROSS JOIN:

sql
CopyEdit
SELECT
    p.product_name,
    s.store_name,
    IFNULL(SUM(sales.quantity), 0) AS quantity_sold
FROM
    products p
CROSS JOIN
    stores s
LEFT JOIN
    sales
ON
    p.product_id = sales.product_id
    AND s.store_id = sales.store_id
GROUP BY
    p.product_name,
    s.store_name;

This query ensures even unsold products are displayed, with their quantities set to 0.

Summary of CROSS JOIN

  • CROSS JOIN creates a Cartesian product of two tables.
  • Use it when you need all possible combinations of rows.
  • Add a WHERE clause to filter results or mimic INNER JOIN.
  • Combine with LEFT JOIN to solve advanced queries like identifying unsold items or unused combinations.

You said:

I have still not under stand the purpose of cross join

ChatGPT said:

ChatGPT

Let me explain CROSS JOIN in a simpler way with examples to help you understand its purpose and use.

What is the Purpose of a CROSS JOIN?

CROSS JOIN combines every row from one table with every row from another table. Its primary purpose is to generate all possible combinations of the two tables' rows.

Think of it as creating a "grid" of all combinations where:

  • Table 1 has n rows
  • Table 2 has m rows
  • The result will have n × m rows.

When Would You Use a CROSS JOIN?

  1. Generate All Combinations:
    • You use a CROSS JOIN when you need all possible pairings of two sets of data.
    • Example: Create a deck of playing cards, pair all products with all stores, or combine months with employees to plan schedules.
  2. Find Missing Data:
    • Pair everything (e.g., products and stores) and then check which combinations are missing in another table (e.g., unsold products in a sales table).

Example 1: Generating All Combinations of Products and Stores

Tables

Products Table:

product_id
product_name
1
iPhone
2
iPad
3
Macbook Pro

Stores Table:

store_id
store_name
1
North Store
2
South Store

CROSS JOIN Query

sql
CopyEdit
SELECT
    product_name,
    store_name
FROM
    products
CROSS JOIN
    stores;

Output:

product_name
store_name
iPhone
North Store
iPhone
South Store
iPad
North Store
iPad
South Store
Macbook Pro
North Store
Macbook Pro
South Store

Why Use CROSS JOIN Here?

  • You now have all combinations of products and stores.
  • For instance, if you want to calculate the revenue or check which store sold a particular product, this result can be useful.

Example 2: Checking Unsold Products in Stores

If you want to find out which products were not sold in a particular store, you can start with a CROSS JOIN to generate all possible combinations.

Add Sales Data

Sales Table:

product_id
store_id
quantity
1
1
10
2
1
5

Query to Find Unsold Products

sql
CopyEdit
SELECT
    p.product_name,
    s.store_name,
    IFNULL(SUM(s.quantity), 0) AS total_quantity_sold
FROM
    products p
CROSS JOIN
    stores s
LEFT JOIN
    sales s ON p.product_id = s.product_id AND s.store_id = s.store_id
GROUP BY
    p.product_name, s.store_name;

Output:

product_name
store_name
total_quantity_sold
iPhone
North Store
10
iPad
North Store
5
Macbook Pro
North Store
0
iPhone
South Store
0
iPad
South Store
0
Macbook Pro
South Store
0

Here, the CROSS JOIN ensures that all products and stores are paired. The LEFT JOIN ensures that combinations without sales are also shown (with 0 sales).

Summary of Purpose

The main purposes of CROSS JOIN are:

  1. All Possible Combinations:
    • Use it to pair every row of one table with every row of another.
  2. Problem Solving:
    • Solve questions about combinations that don't exist in other data (e.g., missing sales).
  3. Data Generation:
    • Generate data combinations for testing, simulations, or creating synthetic datasets (like a deck of cards).

Would you like more examples or clarification on a specific use case?