Skip to content
Advertisement

Getting an error using EXISTS operator but correct result with IN operator in

enter image description hereI am getting an error An expression of non-Boolean type specified in a context where a condition is expected while using EXISTS But when I am using IN operator it is giving the correct result.

Below is enter image description hereboth query

SELECT * FROM emp
WHERE Emp_id IN (SELECT Emp_id FROM Department WHERE Department.Emp_id=emp.Emp_id)

SELECT * FROM emp Department
WHERE emp.Emp_id EXISTS (SELECT Emp_id FROM Department WHERE emp.Emp_id=Department.Emp_id)

Advertisement

Answer

EXISTS is a unary operator, not a binary operator, and takes only a subquery as its argument. You say:

WHERE EXISTS (subquery...)

not

WHERE somefield EXISTS (subquery...)

Since EXISTS merely tests whether the subquery would return at least one row, the fields selected are not relevant and people commonly do:

EXISTS (SELECT 1 FROM ...)

though selecting null or any other value or multiple values works just the same.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement