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:
Step 1: Understand the goal
We want to delete customers who do not appear in the orders table.
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.
Final Answer:
DELETE FROM customers WHERE id NOT IN (SELECT customer_id FROM orders) -> Option B
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
Master "Subqueries" in SQL
9 interactive learning modes - each teaches the same concept differently