I thought that the following SQL statement returns lists salary less than 5000. But I got salaries that are greater than or equal to 5000. I really don't know why. Did I miss something?
select salary
from employees
where exists (select 1 from employees e where salary < 5000);
The query inside the
existsoperator in your query returns at least one row, so the operator evaluates totrue. The issue here is that it's not correlated to the outer query in any way, so it returns the same result for every row in the outer query (i.e., alwaystrue).To get the result you want, you need to correlate between the queries by having a term from the outer query in the inner query's
whereclause:But, to be completely honest, you don't really need an
existsoperator here, and can just query the salary directly: