MYSQL

13.MySQL EXISTS, ANY, and ALL: Deep Dive Tutorial with Real Examples

When working with advanced SQL logic, subqueries play a crucial role. Three powerful keywords that extend the utility of subqueries in MySQL are EXISTS, ANY, and ALL. These allow you to perform highly conditional queries and filter data based on the presence or comparison of related data sets.

In this tutorial, we’ll use the following sample data to explore each keyword with clarity and depth.

👨‍💼 employees Table

employee_id name department_id manager_id
1 Alice 1 NULL
2 Bob 2 1
3 Charlie 3 1
4 David NULL 2
5 Eva 2 2

🏢 departments Table

department_id department_name
1 HR
2 IT
4 Marketing

💻 laptops Table

laptop_id employee_id brand
1 1 Dell
2 2 HP
3 3 Lenovo
4 5 Apple

✅ MySQL EXISTS

The EXISTS keyword checks for the existence of rows returned by a subquery. It returns TRUE if the subquery returns any rows.

🔹 Example 1: List departments that have at least one employee

SELECT department_name
FROM departments d
WHERE EXISTS (
  SELECT 1
  FROM employees e
  WHERE e.department_id = d.department_id
);

Explanation: For each department, the subquery checks if at least one employee exists in that department.

Result:

department_name
HR
IT

Marketing is excluded as it has no employees.

🔹 Example 2: List employees who have laptops

SELECT name
FROM employees e
WHERE EXISTS (
  SELECT 1
  FROM laptops l
  WHERE l.employee_id = e.employee_id
);

Result:

name
Alice
Bob
Charlie
Eva

David has no laptop, so he is excluded.


✅ MySQL ANY

The ANY keyword compares a value to any value returned by a subquery. It’s often used with =, >, <, >=, <=, or !=.

🔹 Example 3: Employees in departments larger than department 1

SELECT name
FROM employees
WHERE department_id = ANY (
  SELECT department_id
  FROM employees
  GROUP BY department_id
  HAVING COUNT(*) > (
    SELECT COUNT(*)
    FROM employees
    WHERE department_id = 1
  )
);

Explanation:

  • The inner subquery counts employees in each department.
  • Compares those counts against the size of department 1 (HR).
  • The outer query returns employees from any department larger than HR.

Result:

name
Bob
Eva

✅ MySQL ALL

The ALL keyword compares a value to all values returned by a subquery. It must satisfy the condition for every returned value.

🔹 Example 4: Employees from departments with equal or higher employee count than all other departments

SELECT name
FROM employees
WHERE department_id = ALL (
  SELECT department_id
  FROM employees
  GROUP BY department_id
  HAVING COUNT(*) <= (
    SELECT COUNT(*)
    FROM employees
    WHERE department_id = 2
  )
);

Explanation:

  • IT has 2 employees. This query returns employees from departments that have employee count ≤ IT.
  • ALL ensures that comparison is made against every other department.

Result:

name
Bob
Eva

✅ Summary Table

Keyword Meaning Works With Use Case
EXISTS TRUE if subquery returns any rows Subquery Filter records based on presence of related data
ANY TRUE if condition is true for any row Comparison + subq Check if value is greater/smaller than any
ALL TRUE if condition is true for all Comparison + subq Ensure value meets condition for all rows

✅ Practice Tasks

  1. Use EXISTS to list employees who have a manager.
  2. Use ANY to find employees from departments that have more employees than HR.
  3. Use ALL to find departments that have fewer employees than all others.

🧠 Pro Tips

  • EXISTS is faster than IN when the subquery returns many rows.
  • Use indexed columns inside subqueries for better performance.
  • Combine EXISTS with NOT to find non-matching sets.

Leave a Reply

Your email address will not be published. Required fields are marked *