ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Cross Join in MySql

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='7' AND `tutorial_submenu`='53' AND `tutorial_status`=1 LIMIT 1

Cross Join in MySql

CROSS JOIN in MySQL

A CROSS JOIN in MySQL returns the Cartesian product of two tables, meaning every row from the first table is combined with every row from the second table.


1. Syntax

SELECT * FROM table1 CROSS JOIN table2;

OR

SELECT * FROM table1, table2; -- Implicit CROSS JOIN

  • No ON condition is used.
  • The number of rows in the result = rows in table1 × rows in table2.

2. Example: CROSS JOIN Usage

Tables:

employees

idname
1Alice
2Bob

departments

dept_iddept_name
101IT
102HR


Query: CROSS JOIN employees & departments

SELECT employees.name, departments.dept_nameFROM employeesCROSS JOIN departments;

Output:

+--------+-----------+| name | dept_name |+--------+-----------+| Alice | IT || Alice | HR || Bob | IT || Bob | HR |+--------+-----------+

  • 2 employees × 2 departments = 4 rows.

3. CROSS JOIN vs. INNER JOIN

JOIN TypeDescriptionRequires ON Condition?
CROSS JOINMatches every row from Table A to every row from Table B (Cartesian product).❌ No
INNER JOINMatches rows based on a condition (common values).✅ Yes

INNER JOIN Example (Filtering Results)

SELECT employees.name, departments.dept_nameFROM employeesINNER JOIN departments ON employees.id = departments.dept_id;

✅ Returns only matching rows.


4. When to Use CROSS JOIN

✅ Generating all possible combinations (e.g., test cases).
✅ Creating permutations of data.
✅ When no relationship exists between tables.

Avoid CROSS JOIN on large tables, as it generates huge results.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql