EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial ANY in MySQL
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL?Table?Size
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

ANY in MySQL

By Aanchal SharmaAanchal Sharma

Any in MySQL

Introduction to ANY in MySQL

ANY is a MySQL operator that returns a true value if any of the Subquery condition in MySQL is fulfilled when the SQL query is executed. However, ALL SQL operator works related to ANY operator but it returns true if all the Subquery values is satisfied by the condition in MySQL. Basically, MySQL introduces some special characters or words that help to perform a specified operation or more. We can use the SQL operators together with operands to generate a SQL statement code to contain a certain condition or execute specific operations. Hence, like other operators the ANY operator in MySQL is also used to govern a provisional expression in a SQL statement query to fulfill the benchmarks and provide a result set with true values.

Syntax

  • Following is the elementary syntax of the ANY operator used in MySQL:

SELECT Column1 [Column2,..] FROM TableA WHERE Column2 Operator ANY (SELECT Column2 FROM TableB WHERE condition);

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • We can use the All SQL operator with the below query:

SELECT Column1 [Column2,..] FROM TableA WHERE Column2 Operator ALL (SELECT Column2 FROM TableB WHERE condition);

  • Remember that the SQL operator used here should be a typical one of comparison operator that is commonly used in MySQL such as <, >, =, !=, <>, <=, >=.

How does ANY Operator work in MySQL?

Generally, in MySQL operation with ANY and comparison operator we have the following syntax:

Operand ComparisonOperator ANY (Subquery);

  • Since ANY operator should return true if the condition is true so, the ANY SQL keyword should track a comparison operator and if a comparison is valid for ANY of the values provided in the column by the result of the subquery.
  • For illustration, let us consider the succeeding statement:

SELECT K1 FROM Q1 WHERE K1 > ANY (SELECT K1 FROM Q2);

  • Suppose, there exists a field row in the Q1 table with value (11), then the ANY operator returns TRUE from the expression if there exist values in table Q2 at least one less than 11, for example (30, 12, 8). Here, the condition will be fulfilled because, in Q2, a value 8 is available which is a smaller amount than 11 in Q1 so the ANY keyword will result in TRUE value.
  • But supposing Q2 comprises (30,12,31), then no values of subquery will be less than Q1 value and thus the ANY operator will show FALSE as the conditional expression is not satisfied. Also, if Q2 holds NULL values like (NULL, NULL, NULL) then, the expression will be unknown.
  • The keyword IN in MySQL works as a code-named (alias) for = ANY when used with a Subquery in the query statement. Hence, we can say that the below two SQL statements are identical in MySQL;

Code:

SELECT K1 FROM Q1 WHERE K1 = ANY (SELECT K1 FROM Q2);
SELECT K1 FROM Q1 WHERE K1 IN (SELECT K1 FROM Q2);

  • But we cannot say that IN SQL operator and = ANY operator keyword are synonyms when we use a list of expressions because IN takes the list of expressions and = ANY cannot.
  • Also, NOT IN cannot be an alias or designated name for <> ANY operator but can be used for <> ALL.
  • The keyword SOME in MySQL is an alias given to ANY keyword and therefore the two declarations of SQL code are equivalent:

Code:

SELECT K1 FROM Q1 WHERE K1 <> ANY (SELECT K1 FROM Q2);
SELECT K1 FROM Q1 WHERE K1 <> SOME (SELECT K1 FROM Q2);

  • We can use table query in a scalar IN, ANY, SOME subquery condition is given that the table contains only one column. Understand if Q2 in the above table has only one column, then the above SQL statements can be re-written as follows:

Code:

SELECT K1 FROM Q1 WHERE K1 > ANY (Table Q2);

// (SELECT K1 FROM Q2) is substituted with Table Q2

Code:

SELECT K1 FROM Q1 WHERE K1 = ANY (Table Q2);
SELECT K1 FROM Q1 WHERE K1 IN (Table Q2);
SELECT K1 FROM Q1 WHERE K1 <> ANY (Table Q2);
SELECT K1 FROM Q1 WHERE K1 <> SOME (Table Q2);

  • Remember <> operator defines the true meaning of SQL syntax indicating that there are nearly some values that are not equal to a condition but not all values.

Examples to Implement ANY in MySQL

Let us show some examples and take some tables named ‘Customers’ and ‘Orders’ to execute SQL queries using ANY keyword:

1. Customers Table

ANY in MYSQL - 1

2. Orders Table

ANY in MYSQL - 2

  1. ANY with = operator

Code:

SELECT Name, City FROM Customers WHERE CustomerNum = ANY (SELECT CustomerNum FROM Orders WHERE STATUS = 'Success');

Output:

= operator

  1. ANY with > operator

Code:

SELECT Name, City, CustomerNum FROM Customers WHERE CustomerNum = ANY (SELECT CustomerNum FROM Orders WHERE Budget>8000);

Output:

> operator

Advantages of using ANY operator in MySQL

  • ANY is a type of logical operator in MySQL which returns the Boolean value as a result of the SQL query. It is used to select any or some tuples of the SELECT statement.
  • The ANY operator allows comparing the value of a table to each value in the result list or rows provided by the subquery condition. After this, the ANY keyword if find any value that matches at least one value or row of the inner query then, it gives TRUE as a result.
  • Thus, in MySQL the ANY operator should be preceded by comparison operators. So, the ANY operator returns TRUE if any of the inner queries execute to satisfy the condition.
  • The ANY keyword is useful to provide the result which is a distinct column value from a table that matches or has any record in another table.
  • You can perform many different types of comparison using ANY operator with SELECT and WHERE keywords and out a condition to match and provide the required result from a combination of two tables.
  • It is helpful to get the result if there exists any value in one table that is satisfying a certain condition that shows the value that matches.

Conclusion

The ANY operator in MySQL is used to filter the result set from SQL syntax only if any of the values meet the condition otherwise it shows false. Like other different operators in MySQL, the ANY keyword is also an operator used along with comparison operators with a distinct result by comparing subquery results and operands in the SQL expression. I hope you like the article and learned some new points.

Recommended Articles

This is a guide to ANY in MySQL. Here we discuss Syntax, how does it work with examples to implement with proper codes and outputs and advantages to use. You can also go through our other related articles to learn more –

  1. MySQL Outer Join
  2. ORDER BY in MySQL
  3. MySQL Subquery | Types and Examples
  4. MySQL Timestamp | How to Works?
  5. Guide to IF Statement in MySQL
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (17 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more