Return the Cartesian product (cross join) of TableA and TableB: every row of one paired with every row of the other, so a table of m rows and a table of n rows give m×n result rows.
Input Table(s)
Table TableA
| Column | Type | Meaning |
|---|
| id | INT | Row id (primary key) |
| name | VARCHAR(50) | A person's name, NOT NULL |
Primary key: id.
Table TableB
| Column | Type | Meaning |
|---|
| id | INT | Row id (primary key) |
| color | VARCHAR(50) | A color name, NOT NULL |
Primary key: id.
The two id columns are unrelated. They are not a join key.
Output Contract
One row for every combination of a TableA row and a TableB row, with these columns in this order:
-
a_id
:
TableA.id
-
a_name
:
TableA.name
-
b_id
:
TableB.id
-
b_color
:
TableB.color
Order by a_id ascending, then b_id ascending. Each (a_id, b_id) pair appears once, so this order is unique.
Example
TableA
TableB
Expected result:
| a_id | a_name | b_id | b_color |
|---|
| 1 | Alice | 1 | Red |
| 1 | Alice | 2 | Blue |
| 1 | Alice | 3 | Green |
| 2 | Bob | 1 | Red |
| 2 | Bob | 2 | Blue |
| 2 | Bob | 3 | Green |
Two rows in TableA and three in TableB give 2 × 3 = 6 pairs. Alice's three rows come first because her a_id is smaller, and within each person the colors follow b_id.
Constraints and Clarifications
-
Each table has between 1 and 100 rows, so the result has at most 10,000 rows.
-
id
values are unique within each table. The same
id
value can appear in both tables.
-
name
and
color
consist of English letters only and are never NULL. Names or colors can repeat across rows; every row still pairs with every row of the other table.