SQL
Filter Records Using Subquery with EXISTS
Learn to filter main query results by efficiently checking for the existence of related records in a subquery using the SQL `EXISTS` operator.
SELECT c.customer_id, c.customer_name
FROM customers c
WHERE EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
);
How it works: This query selects customers who have placed at least one order. The `EXISTS` operator is used within the `WHERE` clause to check if the subquery returns any rows. If the subquery finds at least one order matching the current customer's ID, the `EXISTS` condition evaluates to true, and the customer is included in the result set.