What is the difference between IN and EXISTS in SQL?
- IN: Checks whether a value is present in a list or a subquery’s result set.
- EXISTS: Checks whether a subquery returns any rows, returning TRUE if the
subquery returns one or more rows, otherwise FALSE.
Example with IN:
SELECT name
FROM employees
WHERE department_id IN (SELECT id FROM departments WHERE name =
'HR');
Example with EXISTS:
SELECT name
FROM employees e
WHERE EXISTS (SELECT 1 FROM departments d WHERE d.id =
e.department_id AND d.name = 'HR');