EXISTS in MySQL — How It Works

What the MySQL EXISTS function does, with an example.

Published October 22, 2026

EXISTS (subquery) returns true if the subquery returns at least one row. It is efficient for existence checks because it stops at the first match.

How it works

NOT EXISTS finds rows with no related rows, a common anti-join.

Try it in MySQL

SELECT * FROM users u WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id)
users with orders
SELECT * FROM users u WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id)
users without orders
SELECT EXISTS(SELECT 1 FROM users WHERE email = 'a@x.com')
1 or 0

Practical examples

Example 1

SELECT * FROM users u WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);
-- users with orders

Example 2

SELECT * FROM users u WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);
-- users without orders

Example 3

SELECT EXISTS(SELECT 1 FROM users WHERE email = 'a@x.com');
-- 1 or 0

Use it in a real query

SELECT * FROM users u WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);

Common mistakes

  • Selecting columns in the subquery (SELECT 1 is fine).
  • Using IN with NULL-prone subqueries (NOT IN problems).
  • Missing the correlation condition.

Key takeaways

  • EXISTS in one line: EXISTS (subquery) returns true if the subquery returns at least one row.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

EXISTS vs IN?

EXISTS is often faster and handles NULLs more safely, especially NOT EXISTS.

Does EXISTS work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles