Crossing into CROSS JOIN

Joining Data in SQL

Maham Faisal Khan

Senior Content Developer, DataCamp

CROSS JOIN diagram

CROSS JOIN creates all possible combinations of two tables.

A diagram showing two tables, table1 and table2, with arrows pointing to the records that match on the id column in both tables. Records that are not of interest to cross join have been grayed out. The result set after cross join is shown on the right hand side of the diagram.

Joining Data in SQL

CROSS JOIN syntax

SELECT id1, id2
FROM table1
CROSS JOIN table2;
Joining Data in SQL

Pairing prime ministers with presidents

SELECT prime_minister, president
FROM prime_ministers AS p1
CROSS JOIN presidents AS p2

WHERE p1.continent IN ('Asia') AND p2.continent IN ('South America');
|--------------------|-------------------------|
| prime_minister     | president               |
|--------------------|-------------------------|
| Shehbaz Sharif     | Luis Lacalle Pou        |
| Narendra Modi      | Luis Lacalle Pou        |
| Hassanal Bolkiah   | Luis Lacalle Pou        |
| Haitham bin Tarik  | Luis Lacalle Pou        |
| Shehbaz Sharif     | Gabriel Boric           |
| Narendra Modi      | Gabriel Boric           |
| Hassanal Bolkiah   | Gabriel Boric           |
| Haitham bin Tarik  | Gabriel Boric           |
|--------------------|-------------------------|
Joining Data in SQL

Let's practice!

Joining Data in SQL

Preparing Video For Download...