The EXISTS operator checks whether a subquery returns any rows. It returns TRUE if at least one row is found and FALSE otherwise. Use EXISTS when you only need to test for presence, not to retrieve data from the subquery.
EXISTS yields TRUE or FALSE.employees.employee_id).SELECT 1 inside the subquery to signal intent — the DB only needs to check existence.
// Syntax for EXISTS: check if subquery returns any row(s)
SELECT column1
FROM table1
WHERE EXISTS (SELECT 1 FROM table2 WHERE condition);
// Find employees who have made at least one order (correlated subquery)
SELECT employee_name
FROM employees
WHERE EXISTS (SELECT 1 FROM orders WHERE orders.employee_id = employees.employee_id);
// Get products that have been ordered at least once
SELECT product_name
FROM products
WHERE EXISTS (SELECT 1 FROM order_items WHERE order_items.product_id = products.product_id);
WHERE or HAVING clauses.Example result: The first example returns the names of employees for whom the subquery (orders) finds at least one matching row. The DB engine evaluates the subquery for each employee until it finds a matching order; then it treats EXISTS as TRUE for that employee.
EXISTS (SELECT 1 ...) to signal intent — the actual column selected inside the subquery is irrelevant.EXISTS vs equivalent JOIN approaches — performance depends on indexes and database engine.EXISTS.INNER JOIN query into an EXISTS-based query and compare results and execution plan.orders.employee_id, order_items.product_id).