Bird
0
0

You have tables:

hard📝 Application Q8 of 15
SQL - Subqueries
You have tables:
Products(product_id, category_id, price)
Categories(category_id, category_name)
Which query correctly finds product_id of products in categories 'Furniture' or 'Appliances'?
ASELECT product_id FROM Products WHERE category_id IN (SELECT category_id FROM Categories WHERE category_name IN ('Furniture', 'Appliances'));
BSELECT product_id FROM Products WHERE category_id = (SELECT category_id FROM Categories WHERE category_name = 'Furniture' OR category_name = 'Appliances');
CSELECT product_id FROM Products WHERE category_id IN (SELECT category_name FROM Categories WHERE category_name = 'Furniture' OR category_name = 'Appliances');
DSELECT product_id FROM Products WHERE category_id IN (SELECT category_id FROM Categories WHERE category_name = 'Furniture' AND category_name = 'Appliances');
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    Find product_ids where category_id matches categories named 'Furniture' or 'Appliances'.
  2. Step 2: Analyze options

    SELECT product_id FROM Products WHERE category_id IN (SELECT category_id FROM Categories WHERE category_name IN ('Furniture', 'Appliances')); correctly uses IN with a subquery returning category_ids for both categories.
  3. Step 3: Identify errors in other options

    SELECT product_id FROM Products WHERE category_id = (SELECT category_id FROM Categories WHERE category_name = 'Furniture' OR category_name = 'Appliances'); uses = with subquery returning multiple rows (error). SELECT product_id FROM Products WHERE category_id IN (SELECT category_name FROM Categories WHERE category_name = 'Furniture' OR category_name = 'Appliances'); compares category_id with category_name (wrong column). SELECT product_id FROM Products WHERE category_id IN (SELECT category_id FROM Categories WHERE category_name = 'Furniture' AND category_name = 'Appliances'); uses AND which cannot be true simultaneously.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Use IN with subquery returning multiple matching category_ids [OK]
Quick Trick: Use IN with subquery returning multiple values for OR conditions [OK]
Common Mistakes:
MISTAKES
  • Using = instead of IN for multiple values
  • Comparing incompatible columns
  • Using AND instead of OR in subquery

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes