Bird
0
0

You have two tables:

hard📝 Application Q15 of 15
SQL - Set Operations
You have two tables:
ProductsA with columns product_id, name, price
ProductsB with columns product_id, name, price

You want to find products that exist in both tables with the same product_id and name, ignoring price differences.

Which query correctly uses INTERSECT to achieve this?
ASELECT product_id, name, price FROM ProductsA INTERSECT SELECT product_id, name, price FROM ProductsB;
BSELECT product_id, name FROM ProductsA INTERSECT SELECT product_id, name FROM ProductsB;
CSELECT product_id FROM ProductsA INTERSECT SELECT product_id FROM ProductsB;
DSELECT * FROM ProductsA INTERSECT SELECT * FROM ProductsB;
Step-by-Step Solution
Solution:
  1. Step 1: Identify columns to compare

    You want to compare only product_id and name, ignoring price differences.
  2. Step 2: Use INTERSECT on matching columns

    SELECT product_id, name FROM ProductsA INTERSECT SELECT product_id, name FROM ProductsB; selects product_id and name from both tables and intersects them, returning only products common by those two columns.
  3. Step 3: Analyze other options

    SELECT product_id, name, price FROM ProductsA INTERSECT SELECT product_id, name, price FROM ProductsB; includes price, so products with different prices won't match. SELECT product_id FROM ProductsA INTERSECT SELECT product_id FROM ProductsB; compares only product_id, ignoring name. SELECT * FROM ProductsA INTERSECT SELECT * FROM ProductsB; compares all columns, including price, which is not desired.
  4. Final Answer:

    SELECT product_id, name FROM ProductsA INTERSECT SELECT product_id, name FROM ProductsB; -> Option B
  5. Quick Check:

    INTERSECT on selected columns matches desired fields [OK]
Quick Trick: Select only columns to compare before INTERSECT [OK]
Common Mistakes:
MISTAKES
  • Including extra columns that cause mismatches
  • Selecting * when columns differ
  • Comparing only one column when two are needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes