Skip to content

EXISTS

  • subquery: This is a SELECT query that returns a result set. EXISTS(subquery) will evaluate as true if the subquery returns at least one line.
SELECT name
FROM employees
WHERE EXISTS (SELECT *
FROM department
WHERE employees.dept_id = department.id);
| name |
|-------|
| John |
| Emily |
| Paul |

The EXISTS function in this SQL query checks whether any row returned by the subquery matches a condition. If there exists a department with a matching department id for an employee, the employee’s name is returned. In the output, it shows the name of all employees who are part of any department.