SQL EXISTS
SQL

SQL EXISTS — How to Check If a Subquery Returns Any Rows

CHAPTER 27 of 30 SQL Tutorial — Free Course on Neotech Navigators
The SQL EXISTS operator tests whether a subquery returns any rows at all. It returns TRUE if the subquery produces one or more rows, and FALSE if it produces zero rows. EXISTS is commonly used to check for related records in another table. In this chapter, you will learn the EXISTS syntax, compare EXISTS vs IN, and see practical examples.

SQL EXISTS Syntax

SQL — EXISTS Syntax
SELECT columns
FROM table_name
WHERE EXISTS (
  SELECT 1 FROM other_table
  WHERE condition
);

The subquery inside EXISTS does not need to return specific columns — SELECT 1 or SELECT * both work because EXISTS only checks whether rows exist, not what they contain.

Sample Tables

We will use the Employees and Departments tables from the Joins chapters:

TABLE: Employees
+----+-----------+--------+--------+
| ID | Name      | DeptID | Salary |
+----+-----------+--------+--------+
| 1  | Priya K.  | 101    | 55000  |
| 2  | John D.   | 102    | 62000  |
| 3  | Arun M.   | 101    | 78000  |
| 4  | Emma W.   | 103    | 51000  |
| 5  | Ravi S.   | NULL   | 72000  |
+----+-----------+--------+--------+
TABLE: Departments
+-----+----------------+-----------+
| ID  | DeptName       | Location  |
+-----+----------------+-----------+
| 101 | IT             | Delhi     |
| 102 | Marketing      | London    |
| 103 | Sales          | Mumbai    |
| 104 | HR             | New York  |
+-----+----------------+-----------+

SQL EXISTS Example

Find departments that have at least one employee:

SQL
SELECT d.DeptName, d.Location
FROM Departments AS d
WHERE EXISTS (
  SELECT 1 FROM Employees AS e
  WHERE e.DeptID = d.ID
);
✓ OUTPUT
+-----------+-----------+
| DeptName  | Location  |
+-----------+-----------+
| IT        | Delhi     |
| Marketing | London    |
| Sales     | Mumbai    |
+-----------+-----------+

HR is excluded because no employee has DeptID = 104. The subquery checks each department — if any employee matches, EXISTS returns TRUE and the department is included.

NOT EXISTS — Find Missing Records

Find departments that have no employees:

SQL — Empty Departments
SELECT d.DeptName, d.Location
FROM Departments AS d
WHERE NOT EXISTS (
  SELECT 1 FROM Employees AS e
  WHERE e.DeptID = d.ID
);
✓ OUTPUT
+-----------+-----------+
| DeptName  | Location  |
+-----------+-----------+
| HR        | New York  |
+-----------+-----------+

EXISTS with Additional Conditions

Find departments that have at least one employee earning over 70,000:

SQL
SELECT d.DeptName
FROM Departments AS d
WHERE EXISTS (
  SELECT 1 FROM Employees AS e
  WHERE e.DeptID = d.ID
    AND e.Salary > 70000
);
✓ OUTPUT
+-----------+
| DeptName  |
+-----------+
| IT        |
+-----------+

Only IT qualifies because Arun (78000) and Ravi (72000) both work in IT and earn over 70,000.

EXISTS vs IN

Both EXISTS and IN can check for related records, but they work differently:

Feature EXISTS IN
How it works Checks if subquery returns any rows Checks if value matches a list
Stops early Yes — stops at first match No — evaluates entire list
NULL handling Works correctly with NULLs NOT IN fails with NULLs
Performance Better for large subquery results Better for small lists
Correlated Always correlated (references outer query) Can be non-correlated

💡 When to Use EXISTS vs IN

Use EXISTS when the subquery returns a large number of rows or when you need to handle NULLs safely. Use IN when you have a small, static list of values. For NOT conditions, prefer NOT EXISTS over NOT IN because NOT IN fails silently when the list contains NULL.

For the complete subquery reference, see the official MySQL EXISTS documentation. In the next chapter, you will learn SQL Subqueries in full depth.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query using NOT EXISTS that finds employees who are not assigned to any department (DeptID does not match any Department ID).

ANSWER
SELECT e.Name, e.Salary
FROM Employees AS e
WHERE NOT EXISTS (
  SELECT 1 FROM Departments AS d
  WHERE d.ID = e.DeptID
);

Expected Output:

+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Ravi S.   | 72000  |
+-----------+--------+

📝 What You Learned in This Chapter

  • EXISTS returns TRUE if a subquery produces one or more rows
  • NOT EXISTS returns TRUE if a subquery produces zero rows
  • EXISTS only checks row existence — the selected columns do not matter
  • EXISTS handles NULLs correctly, unlike NOT IN
  • EXISTS is typically faster for large datasets because it stops at the first match
  • Use EXISTS for correlated subqueries, IN for small static lists

Frequently Asked Questions

What does SQL EXISTS do?
EXISTS tests whether a subquery returns any rows. It returns TRUE if at least one row is found and FALSE if no rows match. It is used in the WHERE clause to filter rows based on the existence of related data in another table.
What is the difference between EXISTS and IN?
EXISTS checks if a correlated subquery returns any rows and stops at the first match. IN compares a value against a complete list. EXISTS is better for large datasets and handles NULLs correctly, while IN is simpler for small static lists.
Why use SELECT 1 inside EXISTS?
EXISTS only cares whether rows exist, not what columns they contain. SELECT 1, SELECT *, and SELECT column_name all produce the same result. SELECT 1 is a convention that makes the intent clear and avoids fetching unnecessary data.
Is EXISTS faster than IN?
It depends. EXISTS can be faster when the subquery returns many rows because it stops at the first match. IN may be faster for small, indexed lists. Modern query optimizers often convert between the two internally, so the performance difference is sometimes negligible.
When should I use NOT EXISTS instead of NOT IN?
Always prefer NOT EXISTS when the subquery column might contain NULLs. NOT IN returns no rows if the list contains NULL, which is a common bug. NOT EXISTS handles NULLs correctly and predictably.

📖 Chapter 27 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com