EXISTS checks if a subquery returns any rows. If it does, the condition is true; if not, it is false.
A subquery with EXISTS only checks for the presence of rows, not the actual data. A join combines rows from two tables based on a condition.
SELECT customer_name FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);
SELECT 1 often used inside an EXISTS subquery?Because EXISTS only cares if rows exist, not the data itself. SELECT 1 is simple and efficient.
Yes, sometimes. EXISTS stops searching as soon as it finds a matching row, which can be faster than joining all rows.
EXISTS checks if the subquery returns any rows, making the condition true if yes.
Option A correctly uses EXISTS with a subquery.
SELECT name FROM students s WHERE EXISTS (SELECT 1 FROM enrollments e WHERE e.student_id = s.id);
The query returns students who have at least one matching enrollment.
SELECT 1 be preferred inside an EXISTS subquery?EXISTS only checks for row existence, so the actual column selected does not matter.
EXISTS returns true if the subquery returns at least one row.