Bird
0
0

Given two tables:

hard📝 Application Q8 of 15
SQL - Set Operations
Given two tables:
InventoryA:
item_id | item_name | quantity
1 | Chair | 5
2 | Table | 3
3 | Lamp | 7

InventoryB:
item_id | item_name | quantity
3 | Lamp | 7
4 | Sofa | 2

Which SQL query returns the common rows between these tables?
ASELECT item_id, item_name FROM InventoryA INTERSECT SELECT item_id, item_name FROM InventoryB;
BSELECT item_id, item_name, quantity FROM InventoryA INTERSECT SELECT item_id, item_name, quantity FROM InventoryB;
CSELECT * FROM InventoryA JOIN InventoryB ON InventoryA.item_id = InventoryB.item_id;
DSELECT item_id, item_name, quantity FROM InventoryA UNION SELECT item_id, item_name, quantity FROM InventoryB;
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want rows common to both tables including all three columns.
  2. Step 2: Analyze options

    SELECT item_id, item_name, quantity FROM InventoryA INTERSECT SELECT item_id, item_name, quantity FROM InventoryB; uses INTERSECT on all three columns, which returns rows exactly matching in both tables.
  3. Step 3: Evaluate other options

    SELECT item_id, item_name FROM InventoryA INTERSECT SELECT item_id, item_name FROM InventoryB; excludes quantity, so it may return partial matches. SELECT * FROM InventoryA JOIN InventoryB ON InventoryA.item_id = InventoryB.item_id; is a JOIN, which returns combined rows but not necessarily identical rows. SELECT item_id, item_name, quantity FROM InventoryA UNION SELECT item_id, item_name, quantity FROM InventoryB; is UNION, which returns all rows from both tables.
  4. Final Answer:

    SELECT item_id, item_name, quantity FROM InventoryA INTERSECT SELECT item_id, item_name, quantity FROM InventoryB; -> Option B
  5. Quick Check:

    INTERSECT returns exact matching rows [OK]
Quick Trick: Use INTERSECT on all columns to find exact common rows [OK]
Common Mistakes:
MISTAKES
  • Using JOIN instead of INTERSECT for exact matches
  • Using UNION which combines all rows
  • Selecting fewer columns causing partial matches

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes