Database Basics

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?

  1. A1 row
  2. B2 rows
  3. C3 rows
Show answer and explanation

Answer: B. 2 rows

First, the subquery in parentheses returns the cust_id of orders whose amount exceeds 5000. These are O2 (C2), O3 (C1) and O5 (C2), so the set of cust_id values is C2, C1, C2. The outer query checks, for each row of the customer table, whether its cust_id is in this set. Aoki (C1) and Inoue (C2) are, and although Inoue appears twice in the set, he is returned only once as one row of the customer table, so the result has two rows. Three rows is the count if one row is produced for every matching order, as when the customer and orders tables are joined and then filtered by amount. One row is the count of only the customers whose every order exceeds 5000 (Inoue).

  • A. This counts only customers whose every order exceeds 5000. IN includes a customer with even one matching order.
  • B. Correct. Aoki and Inoue, who have orders above 5000, give two rows.
  • C. This is the count when a join produces one row per order. With IN, the same customer is returned only once.
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. 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?
  2. Which correctly describes a database view in a relational database?
  3. What is the main purpose of normalization of tables in a relational database?
  4. 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)?
  5. The primary key of an employee table (emp_no, name, dept_code, dept_name) is emp_no, and every column holds a single value. The functional dependencies are emp_no → name, emp_no → dept_code and dept_code → dept_name. Which correctly states the normal form of this table?