The EXISTS condition is used to test whether a specified row exists in the subquery.
Syntax
EXISTS (subquery)
Usage instructions
- If the subquery returns at least one row, it means the data you are looking for exists, and the condition evaluates to TRUE.
- If the subquery returns no rows, the condition evaluates to FALSE.
Examples
obclient> CREATE TABLE dept (dept_id INT);
obclient> CREATE TABLE emp (empno INT, dept_id INT);
obclient> INSERT INTO dept VALUES (10), (20), (30);
obclient> INSERT INTO emp VALUES (1, 10), (2, 20);
obclient> SELECT dept_id FROM dept d WHERE EXISTS (SELECT * FROM emp e
WHERE d.dept_id = e.dept_id) ORDER BY dept_id;
