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.

Return the Cartesian product (cross join) of `tablea` (TableA) and `tableb` (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. Table `tablea`: - `id` INTEGER: row id (primary key) - `name` VARCHAR(50) NOT NULL: a person's name Table `tableb`: - `id` INTEGER: row id (primary key) - `color` VARCHAR(50) NOT NULL: a color name The two `id` columns are unrelated. They are not a join key. Return 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: with `tablea` rows (1, Alice) and (2, Bob) and `tableb` rows (1, Red), (2, Blue) and (3, Green), the result has 2 × 3 = 6 rows: (1, Alice, 1, Red), (1, Alice, 2, Blue), (1, Alice, 3, Green), (2, Bob, 1, Red), (2, Bob, 2, Blue), (2, Bob, 3, Green). Constraints: - 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.

Tables

tablea(id INTEGER, name VARCHAR(50))

tableb(id INTEGER, color VARCHAR(50))

Hints

  1. Both tables have an `id` column. Decide how the query will tell them apart in the select list and in the ordering.
  2. Check your row count: m rows in `tablea` and n rows in `tableb` should give exactly m × n rows, even when no `id` value appears in both tables.
AMDData ScientisteasyData Manipulation (SQL/Python)

Cross Join of TableA and TableB

Return the Cartesian product (cross join) of tablea (TableA) and tableb (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.

Table tablea:

  • id INTEGER: row id (primary key)
  • name VARCHAR(50) NOT NULL: a person's name

Table tableb:

  • id INTEGER: row id (primary key)
  • color VARCHAR(50) NOT NULL: a color name

The two id columns are unrelated. They are not a join key.

Return 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: with tablea rows (1, Alice) and (2, Bob) and tableb rows (1, Red), (2, Blue) and (3, Green), the result has 2 × 3 = 6 rows: (1, Alice, 1, Red), (1, Alice, 2, Blue), (1, Alice, 3, Green), (2, Bob, 1, Red), (2, Bob, 2, Blue), (2, Bob, 3, Green).

Constraints:

  • 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.

Expected Output

Tables

tableatableb
tablea
ColumnType
idINTEGER
nameVARCHAR(50)
tableb
ColumnType
idINTEGER
colorVARCHAR(50)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions