What if you could find matching data across tables with just one simple query?
Why Subquery with IN operator in SQL? - Purpose & Use Cases
Imagine you have a big list of customers and a separate list of orders. You want to find all customers who made orders last month. Doing this by hand means checking each customer against every order one by one.
Manually comparing each customer to every order is slow and tiring. It's easy to miss some matches or make mistakes, especially when the lists are large. This wastes time and causes errors.
Using a subquery with the IN operator lets you automatically find all customers whose IDs appear in the orders list. This saves time and avoids mistakes by letting the database do the matching for you.
Check each customer ID against all order IDs one by one.
SELECT * FROM customers WHERE customer_id IN (SELECT customer_id FROM orders WHERE order_date >= '2024-05-01' AND order_date < '2024-06-01');
This lets you quickly find all related records across tables without complex manual checks.
A store manager wants to see which customers bought products in the last month to send them special offers. Using a subquery with IN finds these customers instantly.
Manual matching is slow and error-prone.
Subquery with IN automates finding related data.
It makes queries simpler and results accurate.