I'm fairly new to SQL. From what I've read, EXISTS
returns a boolean TRUE
or FALSE
, depending on the existence of row(s) that satisfy the subquery.
If EXISTS
returns only a boolean value, how can it be used to limit rows? It would seem that either all rows specified in the parent query would be returned, or none of them would.
For example, the following query returns 110 rows:
SELECT last_name, first_name, department_id
FROM employees e1
WHERE EXISTS (SELECT 'x'
FROM employees e2
WHERE first_name = 'John');
However, when an additional correlated condition is added, only 87 rows are returned:
SELECT last_name, first_name, department_id
FROM employees e1
WHERE EXISTS (SELECT 'x'
FROM employees e2
WHERE first_name = 'John'
AND e1.department_id = e2.department_id);
I'm thinking that this has something to do with correlated subqueries, but shouldn't the entire EXISTS
clause simply return TRUE
?