Postgres Exist Clause

cd .. || cd

July 20, 2023 · 1 mins · Robertus Chris

Table of Contents

A Brief Intro

Have you ever wondering, rather than show the data, can we show the true or false if the data exist or not?

If so, then this short post might be for you!

How?

In this post we will take a look at exists postgresql function. Before we start, please keep in mind that exists use subquery, so there is a penalty of subquery here.

The exists will evaluate if the subquery return any rows. If it returns at least one row, the result of exists is true, if not, then the result of exists is false.

We can use that in SELECT statement like this:

SELECT
exists (
  SELECT users.email
  FROM users
  WHERE users.email = participants.email
)
FROM participants;

Or we can use it in WHERE statement like this:

SELECT participants.email
FROM participants
WHERE exists (
  SELECT users.email
  FROM users
  WHERE (users.role = 'sponsor' AND users.email = participants.email)
)

This can be useful if we don’t want to join the table. Alright, that’s it for this post. See you next time!

References