PostgreSQL EXISTS with SELECT Operator (Example)

โšก Smart Summary

PostgreSQL EXISTS is a Boolean operator that tests whether a correlated subquery returns any row, letting SELECT, INSERT, UPDATE, and DELETE statements filter data by the presence or absence of matching records efficiently.

  • ๐Ÿ“‹ Syntax: WHERE EXISTS (subquery) returns true when the subquery yields at least one row.
  • ๐Ÿ”— Correlated Subquery: EXISTS links inner and outer queries, matching rows on a shared key.
  • โšก Short-Circuit: EXISTS stops at the first matching row, so it scales well on large tables.
  • ๐Ÿ” Four Statements: The operator works inside SELECT, INSERT, UPDATE, and DELETE queries.
  • ๐Ÿ›ก๏ธ NOT EXISTS: Safely excludes rows and avoids the NULL trap that breaks NOT IN.
  • ๐Ÿค– AI Queries: Assistants generate EXISTS clauses and flag correlated-subquery performance risks.

PostgreSQL EXISTS with Select Operator

What is EXISTS in PostgreSQL?

The PostgreSQL EXISTS operator tests whether a row(s) exists in a subquery. This means that the operator is used together with a subquery. The EXISTS operator is said to have been met when at least one row is found in the subquery. You can use this operation along with SELECT, UPDATE, INSERT, and DELETE statements.

PostgreSQL EXISTS Query Syntax

Here is the syntax for the PostgreSQL EXISTS statement:

WHERE EXISTS (subquery);

The above syntax shows that the EXISTS operator takes in an argument, which is a subquery. The subquery is simply a SELECT statement that should begin with a SELECT * instead of a list of column names or expressions.

PostgreSQL EXISTS with SELECT Statement

Let us see how to use a SELECT statement with the EXISTS operator. We have the following tables:

Book:

PostgreSQL EXISTS with SELECT Statement

Price:

PostgreSQL EXISTS with SELECT Statement

Run the following statement:

SELECT *
FROM Book
WHERE EXISTS (SELECT *
              FROM Price
              WHERE Book.id = Price.id);

This returns the following:

PostgreSQL EXISTS with SELECT Statement result

The above command should return all records in the Book table whose id matches the id of any records by the subquery. Only one id was matched. Hence, only one record was returned.

PostgreSQL EXISTS with INSERT Statement

We can use the EXISTS operator in an INSERT statement. We have the following 3 tables:

Book:

PostgreSQL EXISTS with INSERT Statement

Price:

PostgreSQL EXISTS with INSERT Statement

Price2:

PostgreSQL EXISTS with INSERT Statement

We can then run the following statement:

INSERT INTO Price
SELECT id, price
FROM Price2
WHERE EXISTS (SELECT id
              FROM Book
              WHERE Book.id = Price2.id);

The Price table is now as follows:

PostgreSQL EXISTS with INSERT Statement result

The row with an id of 5 in the table named Price2 was matched. This record was then inserted into the Price table.

PostgreSQL EXISTS with UPDATE Statement

We can use the EXISTS operator in an UPDATE statement.

Run the following query:

UPDATE Price
SET price = (SELECT price
              FROM Price2
              WHERE Price2.id = Price.id)
WHERE EXISTS (SELECT id, price
FROM Price2
WHERE Price2.id = Price.id);

PostgreSQL EXISTS with UPDATE Statement

We are updating the price column of the Price table. Our aim is to have the prices of items sharing an id to be the same. Only one row was matched, that is, 5.

However, since the prices are equal, that is, 205, no update was made. If there was a difference, an update would have been made.

PostgreSQL EXISTS with DELETE Statement

A PostgreSQL DELETE statement can use the EXISTS operator. Here is an example:

DELETE FROM Price
WHERE EXISTS (SELECT *
              FROM Price2
              WHERE Price.id = Price2.id);

The Price table is now as follows:

PostgreSQL EXISTS with DELETE Statement

The row with an id of 5 has been deleted.

PostgreSQL EXISTS Statement using pgAdmin

Beyond the SQL shell, the same EXISTS queries can be executed visually through the pgAdmin interface. Now let us see how these actions can be performed using pgAdmin:

With SELECT Statement

Below are the steps to use an EXISTS query in PostgreSQL with a SELECT statement using pgAdmin:

Step 1) Login to your pgAdmin account. Open pgAdmin and Login to your account using your credentials.

Step 2) Create a Demo Database.

  1. From the navigation bar on the left, click Databases.
  2. Click Demo.

PostgreSQL EXISTS Statement using pgAdmin

Step 3) Type the below query in the query editor:

SELECT *
FROM Book
WHERE EXISTS (SELECT *
              FROM Price
              WHERE Book.id = Price.id);

Step 4) Click the Execute button.

PostgreSQL EXISTS Statement using pgAdmin

It should return the following:

PostgreSQL EXISTS Statement using pgAdmin

With INSERT Statement

To accomplish the same through pgAdmin, do this:

Step 1) Login to your pgAdmin account.

Step 2)

  1. From the navigation bar on the left, click Databases.
  2. Click Demo.

EXISTS Statement in PostgreSQL using pgAdmin

Step 3) Type the query in the query editor:

INSERT INTO Price
SELECT id, price
FROM Price2
WHERE EXISTS (SELECT id
              FROM Book
              WHERE Book.id = Price2.id);

Step 4) Click the Execute button.

EXISTS Statement in PostgreSQL using pgAdmin

The Price table should now be as follows:

EXISTS Statement in PostgreSQL using pgAdmin

With UPDATE Statement

To accomplish the same through pgAdmin, do this:

Step 1) Login to your pgAdmin account.

Step 2)

  1. From the navigation bar on the left, click Databases.
  2. Click Demo.

EXISTS Statement in PostgreSQL using pgAdmin

Step 3) Type the query in the query editor:

UPDATE Price
SET price = (SELECT price
              FROM Price2
              WHERE Price2.id = Price.id)
WHERE EXISTS (SELECT id, price
FROM Price2
WHERE Price2.id = Price.id);

Step 4) Click the Execute button.

PostgreSQL EXISTS Statement using pgAdmin

The Price table should now be as follows:

PostgreSQL EXISTS Statement using pgAdmin

With DELETE Statement

To accomplish the same through pgAdmin, do this:

Step 1) Login to your pgAdmin account.

Step 2)

  1. From the navigation bar on the left, click Databases.
  2. Click Demo.

PostgreSQL EXISTS Statement using pgAdmin

Step 3) Type the query in the query editor:

DELETE FROM Price
WHERE EXISTS (SELECT *
              FROM Price2
              WHERE Price.id = Price2.id);

Step 4) Click the Execute button.

PostgreSQL EXISTS Statement using pgAdmin

The Price table should now be as follows:

PostgreSQL EXISTS Statement using pgAdmin

Download the Database used in this Tutorial

FAQs

EXISTS tests whether a correlated subquery returns any row and stops at the first match, so it suits large subqueries. IN compares a value against a list and fits small, constant sets.

NOT IN can return zero rows when the subquery contains a NULL, because the comparison becomes unknown. NOT EXISTS ignores NULLs and evaluates each row correctly, making it the reliable choice for anti-joins.

A correlated subquery references a column from the outer query, such as WHERE Book.id = Price.id. EXISTS re-checks it for each outer row and returns true once a match is found.

Yes. EXISTS returns true as soon as the subquery produces one row, so it does not scan the rest. This short-circuit behavior makes it efficient on large correlated subqueries.

No. EXISTS only checks whether the subquery returns a row, so SELECT *, SELECT 1, or SELECT id behave the same. The column list inside the subquery does not affect the result.

Index the columns joined inside the subquery, keep the correlation condition sargable, and run EXPLAIN ANALYZE. PostgreSQL often rewrites EXISTS as an efficient semi-join when indexes support it.

AI assistants turn plain-English rules into EXISTS clauses, build the correlated subquery, and warn about NOT IN NULL traps. They also suggest indexes and EXPLAIN hints for tuning.

Yes. An AI Copilot detects subquery patterns and rewrites IN as EXISTS or NOT IN as NOT EXISTS, improving NULL safety and performance on large tables.

Summarize this post with: