Bird
0
0

Given two tables:

medium📝 query result Q13 of 15
PostgreSQL - Joins in PostgreSQL
Given two tables:
Table A:
id | name
1 | Alice
2 | Bob

Table B:
code
X
Y

What is the result of:
SELECT A.name, B.code FROM A CROSS JOIN B;
A[('Alice', 'X'), ('Bob', 'Y')]
B[('Alice', 'X'), ('Alice', 'Y'), ('Bob', 'X'), ('Bob', 'Y')]
C[('Alice', 'X'), ('Bob', 'X'), ('Alice', 'Y'), ('Bob', 'Y')]
D[('Alice', 'X'), ('Bob', 'X')]
Step-by-Step Solution
Solution:
  1. Step 1: Understand CROSS JOIN output

    Each row in A pairs with every row in B, so 2 rows in A x 2 rows in B = 4 rows total.
  2. Step 2: List all combinations

    Pairs are (Alice, X), (Alice, Y), (Bob, X), (Bob, Y) in that order.
  3. Final Answer:

    [('Alice', 'X'), ('Alice', 'Y'), ('Bob', 'X'), ('Bob', 'Y')] -> Option B
  4. Quick Check:

    2 x 2 = 4 rows [OK]
Quick Trick: Multiply row counts of both tables for CROSS JOIN result size [OK]
Common Mistakes:
  • Assuming CROSS JOIN filters rows
  • Mixing order of pairs incorrectly
  • Counting only matching rows instead of all combinations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes