EXISTS Operator

SQL · SQL Subqueries

Theory

EXISTS (subquery) is TRUE if the subquery returns at least one row and FALSE if it returns none. It does not care what the rows contain, which is why people write SELECT 1 inside it. It is almost always correlated: WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id) keeps customers who have at least one order.

The engine can stop at the first matching row, so EXISTS is efficient, and each outer row appears once even if it has many matches, unlike a join, which repeats it. NOT EXISTS keeps rows with no match, like customers who never ordered. It is the safest way to write an anti-join: NOT IN returns nothing if the subquery contains a NULL, while NOT EXISTS handles NULLs correctly. Most modern optimisers run IN and EXISTS similarly, so choose by meaning and NULL safety.

Why we use it

"Does a related row exist?" is a very common question: customers with orders, products never sold, users without a login. EXISTS answers it without duplicating rows or bringing in extra columns.

When to use it

Use EXISTS / NOT EXISTS to filter rows by whether related rows exist. Use a JOIN when you need columns from the other table, and IN for a short fixed list of values.

How it works

  1. Take an outer row (customer c)
  2. Run the inner query with c.id
  3. Inner query found a row? Yes: EXISTS is TRUE: keep the customer. No: FALSE: discard it.
  4. Stop scanning orders at the first match
  5. Move to the next customer

Syntax

SELECT columns FROM table
WHERE EXISTS (SELECT 1 FROM other WHERE condition);

Example

SELECT name FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic