Ch. 11 · SQL & PostgreSQL

SQL Exists for Relationship Checks

SQL Exists for Relationship Checks. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Exists asks whether any qualifying row exists without requesting matching row details. It expresses membership clearly and avoids output multiplication.

Before you start

You should understand tables, keys, joins and SELECT queries. Write down a tiny dataset before reasoning about a query. SQL examples use PostgreSQL-style syntax where relevant; planner behavior, locking and available features must be checked for the database and version used in an application.

The practical goal is to reason through this situation: Find customers with at least one overdue invoice using a correlated exists condition. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Ask whether details are needed

A membership query only requires evidence of one qualifying child.

Step 2: Use EXISTS for membership

It expresses that requirement without returning every child combination.

Step 3: Measure representative execution

Review plans and indexes rather than assuming syntax alone establishes speed.

Worked scenario

Find customers with at least one overdue invoice using a correlated exists condition.

A customer has five overdue invoices but should appear once in the reminder-candidate list. EXISTS preserves that customer-level grain directly. A join returns five combinations unless another operation reduces them. When invoice detail is needed, the join serves a different purpose and may be appropriate.

Common mistake

Joining and deduplicating unnecessarily complicates a pure existence question.

Verify the behavior

Compare result counts and plans on customers with zero and many qualifying invoices.

Interview exercise

Compare query performance responsibly.

Answer and reasoning

Inspect representative plans and indexes; equivalent-looking syntax does not guarantee identical optimization in every database.

Continue learning

Compare the scenario with the SQL and PostgreSQL interview questions and test your understanding with the SQL and PostgreSQL MCQs. For terminology and implementation details, consult the reference material.

More in SQL & PostgreSQL

read ✓SQL & PostgreSQL · mid

PostgreSQL CASE Expressions

Use simple and searched CASE for conditional values, ordering and aggregation, and avoid the NULL that a missing ELSE produces.

~2 min readread →
read ✓SQL & PostgreSQL · mid

PostgreSQL Date Truncation and Ranges

Group by time with date_trunc, write range predicates with >= and < instead of BETWEEN, and handle timezones explicitly.

~2 min readread →
read ✓SQL & PostgreSQL · hard

PostgreSQL Deadlocks and Retry

Prevent deadlocks with a consistent lock order and short transactions, and retry the ones you cannot prevent.

~2 min readread →
esc