SQL CROSS JOIN

Introduction

In this chapter, we will focus on the CROSS JOIN operation in SQL. The CROSS JOIN keyword returns the Cartesian product of the two tables involved in the join. This means it combines each row of the first table with each row of the second table. This chapter will cover the definition, syntax, and provide examples to help you understand how to use the CROSS JOIN effectively.

What is a CROSS JOIN?

The CROSS JOIN keyword returns the Cartesian product of the two tables. This join is useful when you need to combine all rows from one table with all rows from another table, resulting in a combination of every row from both tables.

Syntax for CROSS JOIN

Basic Syntax

SELECT columns
FROM table1
CROSS JOIN table2;
  • columns: The columns you want to retrieve.
  • table1: The first table you want to join.
  • table2: The second table you want to join.

Step-by-Step Example

Sample Tables

First, let's create two sample tables named products and categories.

CREATE TABLE products (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(50)
);

CREATE TABLE categories (
    category_id INT PRIMARY KEY,
    category_name VARCHAR(50)
);

Insert Sample Data

INSERT INTO products (product_id, product_name)
VALUES
(1, 'Laptop'),
(2, 'Smartphone'),
(3, 'Tablet');

INSERT INTO categories (category_id, category_name)
VALUES
(1, 'Electronics'),
(2, 'Accessories'),
(3, 'Home Appliances');

Using CROSS JOIN

To retrieve the Cartesian product of products and categories:

SELECT products.product_name, categories.category_name
FROM products
CROSS JOIN categories;

Output

product_name category_name
Laptop Electronics
Laptop Accessories
Laptop Home Appliances
Smartphone Electronics
Smartphone Accessories
Smartphone Home Appliances
Tablet Electronics
Tablet Accessories
Tablet Home Appliances

In this example, every product is combined with every category, resulting in a total of 9 combinations (3 products * 3 categories).

CROSS JOIN with Filtering

Although CROSS JOIN produces a Cartesian product, you can still apply a WHERE clause to filter the results.

SELECT products.product_name, categories.category_name
FROM products
CROSS JOIN categories
WHERE products.product_id = 1 OR categories.category_id = 2;

Output

product_name category_name
Laptop Electronics
Laptop Accessories
Laptop Home Appliances
Smartphone Accessories
Tablet Accessories

In this example, we filter the results to include only the combinations where the product_id is 1 or the category_id is 2.

Practical Use Case for CROSS JOIN

A practical use case for CROSS JOIN could be generating combinations for a product recommendation engine or a marketing strategy where you need to combine all products with all possible promotional offers.

Example

CREATE TABLE promotions (
    promotion_id INT PRIMARY KEY,
    promotion_description VARCHAR(100)
);

INSERT INTO promotions (promotion_id, promotion_description)
VALUES
(1, '10% Off'),
(2, 'Buy One Get One Free'),
(3, 'Free Shipping');

SELECT products.product_name, promotions.promotion_description
FROM products
CROSS JOIN promotions;

Output

product_name promotion_description
Laptop 10% Off
Laptop Buy One Get One Free
Laptop Free Shipping
Smartphone 10% Off
Smartphone Buy One Get One Free
Smartphone Free Shipping
Tablet 10% Off
Tablet Buy One Get One Free
Tablet Free Shipping

In this example, every product is combined with every promotion, which can be useful for creating marketing strategies.

Conclusion

The CROSS JOIN is used for generating the Cartesian product of two tables, resulting in all possible combinations of rows from the tables. This chapter covered the basic syntax for CROSS JOIN, provided examples to illustrate its use, and demonstrated how to apply filtering to the results. Understanding how to use CROSS JOIN effectively will greatly enhance your ability to generate comprehensive combinations of data from multiple tables in a relational database. In the next chapter, we will explore SELF JOIN.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare