Bird
0
0

How can subqueries help when you need to delete all customers who have no orders?

hard📝 Application Q9 of 15
SQL - Subqueries
How can subqueries help when you need to delete all customers who have no orders?
ADELETE FROM customers WHERE id IN (SELECT customer_id FROM orders)
BDELETE FROM customers WHERE id NOT IN (SELECT customer_id FROM orders)
CDELETE FROM customers WHERE EXISTS (SELECT * FROM orders)
DDELETE FROM customers WHERE id = (SELECT customer_id FROM orders)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to delete customers who do not appear in the orders table.
  2. Step 2: Analyze options

    DELETE FROM customers WHERE id NOT IN (SELECT customer_id FROM orders) deletes customers whose id is not in the list of customer_ids from orders, correctly identifying those with no orders.
  3. Final Answer:

    DELETE FROM customers WHERE id NOT IN (SELECT customer_id FROM orders) -> Option B
  4. Quick Check:

    Subquery finds customers with orders, NOT IN deletes those without [OK]
Quick Trick: Use NOT IN with subqueries to find missing related rows [OK]
Common Mistakes:
MISTAKES
  • Deleting customers who have orders
  • Using EXISTS without correlation
  • Using = with subquery returning multiple rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes