Database Basics
Column x of table R has four rows with values 1, 1, 2, 3, and column x of table S has four rows with values 1, 1, 2, 4. How many rows does SELECT * FROM R INNER JOIN S ON R.x = S.x return?
- A2 rows
- B3 rows
- C5 rows
Show answer and explanation
Answer: C. 5 rows
In a join, each combination of rows that satisfies the condition becomes one result row. For x = 1, every combination of R's two rows and S's two rows satisfies the condition, giving 2 × 2 = 4 rows; for x = 2 there is 1 × 1 = 1 row. R's 3 and S's 4 have no partner, so they do not appear in the inner join. The total is 4 + 1 = 5 rows. Two rows is the count if only one row is produced per matching value (1 and 2). Three rows is the count if you only check, for each row of R, whether S contains a matching value (a query such as SELECT * FROM R WHERE x IN (SELECT x FROM S)). The Cartesian product without a condition would be 4 × 4 = 16 rows. Be aware that duplicate values in the join column multiply the number of result rows.
- A. This assumes one row per matching value (1 and 2). Rows with the same value produce every combination.
- B. This assumes each row of R is joined only once. Duplicate values add a row for every combination.
- C. Correct. For x = 1 there are 2 × 2 = 4 combinations, plus one row for x = 2, five rows in total.