SQL Keywords

SQL EXISTS

What is the SQL EXISTS operator?

EXISTS returns TRUE if a correlated or uncorrelated subquery produces at least one row, otherwise FALSE.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL EXISTS: Supported in PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, IBM Db2, Google BigQuery, Snowflake, Redshift, and all ANSI-compliant systems

SQL EXISTS Full Explanation

EXISTS is a boolean operator used in WHERE, HAVING, JOIN ON, or CASE expressions to test whether a subquery yields any rows. The database engine stops scanning the subquery once it finds the first qualifying row, making EXISTS highly efficient for existence checks. EXISTS does not return the rows themselves; it only evaluates to TRUE or FALSE for each row in the outer query. Because EXISTS ignores the columns listed in the SELECT clause of its subquery, many developers use SELECT 1 or SELECT NULL for clarity, but any valid select list is accepted. A correlated subquery can reference columns from the outer query, allowing row-by-row checks. NOT EXISTS inverts the result, returning TRUE when the subquery is empty. Caveats: ORDER BY is disallowed unless combined with LIMIT/OFFSET; DISTINCT inside the subquery rarely improves performance; avoid returning large result sets in the subquery because some optimizers may materialize them when correlation is absent.

SQL EXISTS Syntax

EXISTS ( subquery )
NOT EXISTS ( subquery )

SQL EXISTS Parameters

  • subquery (required) - A SELECT statement enclosed in parentheses evaluated for each row of the outer query.

Example Queries Using SQL EXISTS

-- 1. Select customers that have placed at least one order
SELECT c.customer_id, c.name
FROM customers AS c
WHERE EXISTS (
  SELECT 1 FROM orders AS o
  WHERE o.customer_id = c.customer_id
);

-- 2. Find products that have never been ordered
SELECT p.product_id, p.product_name
FROM products AS p
WHERE NOT EXISTS (
  SELECT 1 FROM order_items AS oi
  WHERE oi.product_id = p.product_id
);

-- 3. Use EXISTS in a HAVING clause to filter groups
SELECT d.department_id, COUNT(*) AS emp_count
FROM employees AS e
JOIN departments AS d USING (department_id)
GROUP BY d.department_id
HAVING EXISTS (
  SELECT 1 FROM projects AS pr
  WHERE pr.department_id = d.department_id
);

-- 4. Conditional logic with CASE
SELECT order_id,
       CASE WHEN EXISTS (SELECT 1 FROM refunds r WHERE r.order_id = o.order_id)
            THEN 'Refunded'
            ELSE 'Completed'
       END AS order_status
FROM orders o;

Expected Output Using SQL EXISTS

  • Each outer query row is included when EXISTS evaluates to TRUE and excluded when FALSE
  • Queries using NOT EXISTS return rows for which the subquery yields no matches

Use Cases with SQL EXISTS

  • Check if related data exists without counting or joining
  • Filter parent rows that have or lack child rows
  • Express semi-joins (EXISTS) and anti-joins (NOT EXISTS) clearly
  • Implement business rules in CASE expressions
  • Improve performance compared to LEFT JOIN + IS NULL patterns, especially with indexes

Common Mistakes with SQL EXISTS

  • Selecting unused columns inside the subquery and assuming they matter
  • Writing SELECT * inside EXISTS, which can mislead code reviewers even though performance is identical
  • Forgetting to correlate the subquery, causing EXISTS to evaluate globally rather than per outer row
  • Replacing NOT EXISTS with NOT IN on nullable columns, leading to unexpected NULL handling
  • Adding ORDER BY inside the EXISTS subquery, which is invalid unless limited

Related Topics

NOT EXISTS, IN, ANY/SOME, ALL, JOIN, WHERE, HAVING, Subqueries, Correlated Subquery

First Introduced In

SQL-86 (adopted by all major databases)

Frequently Asked Questions

What is the difference between EXISTS and NOT EXISTS?

EXISTS returns TRUE when the subquery has rows. NOT EXISTS returns TRUE when the subquery is empty, letting you express anti joins.

Is EXISTS faster than JOIN?

For existence checks, EXISTS often outperforms JOIN because the database can stop at the first match and avoid materializing result sets.

Can I use ORDER BY inside an EXISTS subquery?

Standard SQL forbids ORDER BY unless it is paired with LIMIT or OFFSET. Ordering is irrelevant because the engine stops after the first row.

Do I need to SELECT 1 inside EXISTS?

No. Any valid select list works, but SELECT 1 or SELECT NULL clarifies that the data is unused.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!