Skip to content

Commit f0b5173

Browse files
committed
feat(sql-joins): add explanation of Cartesian Product in SQL
What - Documented the concept of Cartesian Product (CROSS JOIN) in SQL. - Explained how it occurs when no join condition is provided. - Illustrated with example tables (`Employees`, `Departments`) and resulting rows. - Highlighted row count calculation: (rows in Table A × rows in Table B). - Covered both intentional and unintentional use cases. Why - Helps differentiate between meaningful joins and accidental cross joins. - Clarifies how missing `ON` conditions can lead to unexpected results. - Provides awareness for developers working with SQL joins. Key Points - CROSS JOIN or missing ON condition → Cartesian Product. - Useful for generating all possible combinations (e.g., customers × products). - Avoid unintentional Cartesian Products by using: - INNER JOIN - LEFT JOIN - RIGHT JOIN with explicit `ON` conditions. Real-life Applications - Generating test datasets by pairing rows across tables. - Recommendation engines (all users × all items). - Scheduling systems (all employees × all time slots). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent a0c4fa3 commit f0b5173

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Section28JDBCusingSQLite/JoinsInSQL/Cartesian Product in SQL Joins.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ A Cartesian Product occurs in SQL when two tables are combined without specifyin
44

55
It results in every row from the first table being paired with every row from the second table.
66

7-
How It Happens
7+
How It Happens:
88
- If you use a CROSS JOIN explicitly or an incorrect JOIN without an ON condition, you get a Cartesian Product.
99
- The total number of rows in the result set = (Rows in Table A) × (Rows in Table B).
1010

11-
Example Scenario
11+
Example Scenario:
1212

1313
Tables:
1414
1. Employees Table:
@@ -44,15 +44,14 @@ If no join condition is used, each employee is combined with all departments:
4444
| 2 | Bob | IT |
4545
| 2 | Bob | Sales |
4646
+--------+------+-----------+
47-
```
47+
4848

4949
Here, 2 employees × 3 departments = 6 rows.
5050

5151
When Cartesian Products Are Useful:
5252
- When you intentionally want all possible combinations (e.g., pairing all customers with all products).
5353
- If using CROSS JOIN explicitly.
5454

55-
How to Avoid Unwanted Cartesian Products
56-
55+
How to Avoid Unwanted Cartesian Products:
5756
- Always specify a JOIN condition with ON to ensure meaningful relationships between tables.
58-
- Use INNER JOIN, LEFT JOIN, or RIGHT JOIN when needed.
57+
- Use INNER JOIN, LEFT JOIN, or RIGHT JOIN when needed.

0 commit comments

Comments
 (0)