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
?
The first query returns all rows from the table if at least one John exists in the whole table. The second query returns all rows from the DEPARTMENT if at least one John exists in this DEPARTMENT. So in the second query departments without Johns won't be output.
If you link subquery in EXISTS with the main table e1.department_id = e2.department_id
then it will be evaluated for each of main rows.