Cross join two tables and return every row pairing in a fixed order

Quick Overview

Write a SQL query that returns the Cartesian product of two tables, pairing every row of one with every row of the other and sorting by both ids. It tests cross join syntax, aliasing two tables that share a column name, and producing a deterministic row order.

Cross join two tables and return every row pairing in a fixed order

Company: AMD

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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 \times 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` | id | name | |---|---| | 1 | Alice | | 2 | Bob | `TableB` | id | color | |---|---| | 1 | Red | | 2 | Blue | | 3 | Green | 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. ```hint Two columns named id Both tables have an `id` column. Decide how the query will tell them apart in the select list and in the ordering. ```

Overview: Write a SQL query that returns the Cartesian product of two tables, pairing every row of one with every row of the other and sorting by both ids. It tests cross join syntax, aliasing two tables that share a column name, and producing a deterministic row order.

AMD logo
AMD
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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 mm rows and a table of nn rows give m×nm \times n result rows.

Input Table(s)

Table TableA

ColumnTypeMeaning
idINTRow id (primary key)
nameVARCHAR(50)A person's name, NOT NULL

Primary key: id.

Table TableB

ColumnTypeMeaning
idINTRow id (primary key)
colorVARCHAR(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

idname
1Alice
2Bob

TableB

idcolor
1Red
2Blue
3Green

Expected result:

a_ida_nameb_idb_color
1Alice1Red
1Alice2Blue
1Alice3Green
2Bob1Red
2Bob2Blue
2Bob3Green

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.