MySQL ANY and ALL Operators

MySQL ANY and ALL Operators

The ANY and ALL operators in MySQL are used with subqueries to compare a value to one or more values returned by the subquery. They are often used with comparison operators like =, >, <, >=, <=, and !=.


🔹 ANY Operator

  • ANY compares a value to each value in a list or subquery.

  • Returns TRUE if the comparison is TRUE for at least one value.

Syntax:



 

Example: Find students with marks greater than any student in HR department.



 

  • ✅ Returns students whose marks are greater than at least one student in HR.


🔹 ALL Operator

  • ALL compares a value to all values in a list or subquery.

  • Returns TRUE only if the comparison is TRUE for every value.

Syntax:



 

Example: Find students with marks greater than all students in IT department.



 

  • ✅ Returns students whose marks are higher than every student in IT.


🔹 Example Table: students

id name dept marks
1 John IT 85
2 Emma HR 90
3 Raj IT 76
4 Sara Finance 92
5 Aman HR 88

🔹 ANY Example Result



 

  • Marks in HR: 90, 88

  • ANY comparison: marks > 88 or 90 → 85, 92 are greater than at least one?

  • Result:

name marks
John 85
Emma 90
Sara 92

🔹 ALL Example Result



 

  • Marks in IT: 85, 76

  • ALL comparison: marks > 85 and 76 → 90, 92 are greater than all in IT

  • Result:

name marks
Emma 90
Sara 92

🔹 Key Points

Operator Purpose
ANY TRUE if comparison is true for any value in the subquery
ALL TRUE if comparison is true for all values in the subquery
Use Case Compare a value against multiple values in a subquery

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

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