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?

  1. A2 rows
  2. B3 rows
  3. 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.
Practice this topic Back to topic

Related calculators

Books on Database Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Database Basics questions

  1. A customer table (cust_id, name) has four rows: (C1, Aoki), (C2, Inoue), (C3, Ueda), (C4, Eto). An orders table (order_id, cust_id, amount) has five rows: (O1, C1, 3000), (O2, C2, 8000), (O3, C1, 6000), (O4, C3, 2000), (O5, C2, 9000). How many rows does SELECT name FROM customer WHERE cust_id IN (SELECT cust_id FROM orders WHERE amount > 5000) return?
  2. From a customer table (cust_id, name) and an orders table (order_id, cust_id, amount), you want the names of customers who have never placed an order. Which SQL statement is correct?
  3. Which correctly describes a database view in a relational database?
  4. What is the main purpose of normalization of tables in a relational database?
  5. The primary key of an order table (order_no, item_no, qty, cust_no, cust_name) is {order_no, item_no}, and its functional dependencies are {order_no, item_no} → qty, order_no → cust_no and cust_no → cust_name. Which is the correct result of normalizing this table up to third normal form (3NF)?