SQL
`EXISTS`
<div id="rrdiagram2"></div>  
The `EXISTS` operator tests for the existence of any row inside the subquery. It returns either true when the subquery returns one or more records, and false otherwise. The `EXISTS` operator is generally the most useful as a *correlated* subquery to express semijoin operations. However, it can be used as an uncorrelated subquery as well.  
For example, we can use it to figure out if there are any grades present for a given course:  
```sql
SELECT EXISTS (FROM grades WHERE course = 'Math') AS math_grades_present;
```  
| math_grades_present |
|--------------------:|
| true                |  
```sql
SELECT EXISTS (FROM grades WHERE course = 'History') AS history_grades_present;
```  
| history_grades_present |
|-----------------------:|
| false                  |  
> The subqueries in the examples above make use of the fact that you can omit the `SELECT *` in DuckDB thanks to the [`FROM`-first syntax](#docs:sql:query_syntax:from). The `SELECT` clause is required in subqueries by other SQL systems but cannot fulfil any purpose in `EXISTS` and `NOT EXISTS` subqueries.  
#### `NOT EXISTS` {#docs:sql:expressions:subqueries::not-exists}  
The `NOT EXISTS` operator tests for the absence of any row inside the subquery. It returns either true when the subquery returns an empty result, and false otherwise. The `NOT EXISTS` operator is generally the most useful as a *correlated* subquery to express antijoin operations. For example, to find Person nodes without an interest:  
```sql
CREATE TABLE Person (id BIGINT, name VARCHAR);
CREATE TABLE interest (PersonId BIGINT, topic VARCHAR);

INSERT INTO Person VALUES (1, 'Jane'), (2, 'Joe');
INSERT INTO interest VALUES (2, 'Music');

SELECT *
FROM Person
WHERE NOT EXISTS (FROM interest WHERE interest.PersonId = Person.id);
```  
| id | name |
|---:|------|
| 1  | Jane |  
> DuckDB automatically detects when a `NOT EXISTS` query expresses an antijoin operation. There is no need to manually rewrite such queries to use `LEFT OUTER JOIN ... WHERE ... IS NULL`.