Bird
0
0

You want to find all customers who have placed orders for products in the 'Electronics' category. Given tables:

hard📝 Application Q15 of 15
SQL - Subqueries
You want to find all customers who have placed orders for products in the 'Electronics' category. Given tables:
Customers(customer_id, name)
Orders(order_id, customer_id, product_id)
Products(product_id, category)
Which query correctly uses a subquery with IN to get these customers?
ASELECT name FROM Customers WHERE customer_id IN (SELECT customer_id FROM Orders WHERE product_id IN (SELECT product_id FROM Products WHERE category = 'Electronics'));
BSELECT name FROM Customers WHERE customer_id = (SELECT customer_id FROM Orders WHERE product_id IN (SELECT product_id FROM Products WHERE category = 'Electronics'));
CSELECT name FROM Customers WHERE customer_id IN (SELECT product_id FROM Products WHERE category = 'Electronics');
DSELECT name FROM Customers WHERE customer_id IN (SELECT order_id FROM Orders WHERE product_id IN (SELECT product_id FROM Products WHERE category = 'Electronics'));
Step-by-Step Solution
Solution:
  1. Step 1: Understand the relationships

    Customers link to Orders by customer_id; Orders link to Products by product_id.
  2. Step 2: Analyze the nested subqueries

    The innermost subquery selects product_ids in 'Electronics'. The middle subquery selects customer_ids from Orders with those product_ids. The outer query selects customer names with those customer_ids.
  3. Final Answer:

    SELECT name FROM Customers WHERE customer_id IN (SELECT customer_id FROM Orders WHERE product_id IN (SELECT product_id FROM Products WHERE category = 'Electronics')); -> Option A
  4. Quick Check:

    Nested IN filters customers by Electronics orders [OK]
Quick Trick: Use nested IN for multi-level filtering [OK]
Common Mistakes:
MISTAKES
  • Using = instead of IN for multiple customer_ids
  • Comparing customer_id with product_id or order_id
  • Missing nested subquery for product filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes