Teachnique
      CourseRoadmaps
      Login

      HomeIntroductionFeaturesVersionsVariablesInstallationAdministrationPHP SyntaxNode.js SyntaxJava SyntaxPython SyntaxConnectionWorkbench

      Create DatabaseDrop DatabaseSelect DatabaseShow DatabaseCopy DatabaseDatabase ExportDatabase ImportDatabase Info

      Create UsersDrop UsersShow UsersChange PasswordGrant PrivilegesShow PrivilegesRevoke PrivilegesLock User AccountUnlock User Account

      Create TablesShow TablesAlter TablesRename TablesClone TablesTruncate TablesTemporary TablesRepair TablesDescribe TablesAdd/Delete ColumnsShow ColumnsRename ColumnsTable LockingDrop TablesDerived Tables

      QueriesConstraintsInsert QuerySelect QueryUpdate QueryDelete QueryReplace QueryInsert IgnoreInsert on Duplicate Key UpdateInsert Into Select

      Create ViewsUpdate ViewsDrop ViewsRename Views

      IndexesCreate IndexDrop IndexShow IndexesUnique IndexClustered IndexNon-Clustered Index

      Where ClauseLimit ClauseDistinct ClauseOrder By ClauseGroup By ClauseHaving ClauseAND OperatorOR OperatorLike OperatorIN OperatorANY OperatorEXISTS OperatorNOT OperatorNOT EQUAL OperatorIS NULL OperatorIS NOT NULL OperatorBetween OperatorUNION OperatorUNION vs UNION ALLMINUS OperatorINTERSECT OperatorINTERVAL Operator

      Using JoinsInner JoinLeft JoinRight JoinCross JoinFull JoinSelf JoinDelete JoinUpdate JoinUnion vs Join

      Unique KeyPrimary KeyForeign KeyComposite KeyAlternate Key

      TriggersCreate TriggerShow TriggerDrop TriggerBefore Insert TriggerAfter Insert TriggerBefore Update TriggerAfter Update TriggerBefore Delete TriggerAfter Delete Trigger

      Data TypesVARCHARBOOLEANENUMDECIMALINTFLOATBITTINYINTBLOBSET

      Regular ExpressionsRLIKE OperatorNOT LIKE OperatorNOT REGEXP Operatorregexp_instr() Functionregexp_like() Functionregexp_replace() Functionregexp_substr() Function

      Fulltext SearchNatural Language Fulltext SearchBoolean Fulltext SearchQuery Expansion Fulltext Searchngram Fulltext Parser

      Date and Time FunctionsArithmetic OperatorsNumeric FunctionsString FunctionsAggregate Functions

      NULL ValuesTransactionsUsing SequencesHandling DuplicatesSQL InjectionSubQueryCommentsCheck ConstraintsStorage EnginesExport Table into CSV FileImport CSV File into DatabaseUUIDCommon Table ExpressionsOn Delete CascadeUpsertHorizontal PartitioningVertical PartitioningCursorStored FunctionsSignalResignalCharacter SetCollationWildcardsAliasROLLUPToday DateLiteralsStored ProcedureExplainJSONStandard DeviationFind Duplicate RecordsDelete Duplicate RecordsSelect Random RecordsShow ProcesslistChange Column TypeReset Auto-IncrementCoalesce() Function

      Useful FunctionsStatements ReferenceQuick GuideUseful ResourcesDiscussion

      Feedback

      Submit request if you have any questions.

      Course
      ANY Operator

      MySQL Tutorial

      This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.

      ANY Operator

      The operators in MySQL have the same meaning as that of operators in mathematics. They are keywords that are used in MySQL statements for performing comparisons or logical operations.

      ANY Operator in MySQL

      The MySQL ANY keyword can be used with a comparison operator (such as =, <, >, <=, >=, <>) to compare a value with a set of values returned by the subquery.
      • This operator will return true if the given condition is satisfied for any of the values in the set.
      • This operator will return false if none of the values in the specified set satisfy the given condition.
      The ANY operator must be preceded by a standard comparison operator i.e. >, >=, <, <=, =, <>, !=, and followed by a subquery.

      Syntax

      Following is the syntax of the ANY operator in MySQL
      SELECT column_name1, column_name2, ...
      FROM table_name
      WHERE column_name operator ANY (subquery);
      Where,
      • column_name is the name of a column to be compared with a subquery.
      • operator is a comparison operator such as =, <, >, <=, >=, or <>.
      • subquery is a SELECT statement that returns a single column of values.

      Example

      Firstly, let us create a table named CUSTOMERS using the following query
      CREATE TABLE CUSTOMERS (
      ID INT AUTO_INCREMENT,
      NAME VARCHAR(20) NOT NULL,
      AGE INT NOT NULL,
      ADDRESS CHAR (25),
      SALARY DECIMAL (18, 2),
      PRIMARY KEY (ID)
      );
      The following query inserts 7 records into the above-created MySQL table
      INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
      (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
      (2, 'Khilan', 25, 'Delhi', 1500.00 ),
      (3, 'Kaushik', 23, 'Kota', 2000.00 ),
      (4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
      (5, 'Hardik', 27, 'Bhopal', 8500.00 ),
      (6, 'Komal', 22, 'Hyderabad', 4500.00 ),
      (7, 'Muffy', 24, 'Indore', 10000.00 );
      Execute the below query to retrieve all the records present in the CUSTOMERS table
      SELECT * FROM CUSTOMERS;
      Following is the CUSTOMERS table
      ID
      NAME
      AGE
      ADDRESS
      SALARY
      1
      Ramesh
      32
      Ahmedabad
      2000.00
      2
      Khilan
      25
      Delhi
      1500.00
      3
      Kaushik
      23
      Kota
      2000.00
      4
      Chaitali
      25
      Mumbai
      6500.00
      5
      Hardik
      27
      Bhopal
      8500.00
      6
      Komal
      22
      Hyderabad
      4500.00
      7
      Muffy
      24
      Indore
      10000.00

      ANY with ">" Operator

      The MySQL ANY operator can be used with the comparison operator ">" (greater than) to verify whether a particular column value is greater than the column value of any of the other records returned by the subquery.

      Example

      In the following query, we are selecting all records from the CUSTOMERS table where the SALARY column is greater than any of the salaries associated with customers whose age is 22.
      SELECT * FROM CUSTOMERS
      WHERE SALARY > ANY
      (SELECT SALARY FROM CUSTOMERS WHERE AGE = 22);

      Output

      The salary of customer with age 22 is 4500. The following are the customers whose salaries are greater than 4500 (age=22).
      ID
      NAME
      AGE
      ADDRESS
      SALARY
      4
      Chaitali
      25
      Mumbai
      6500.00
      5
      Hardik
      27
      Bhopal
      8500.00
      7
      Muffy
      24
      Indore
      10000.00

      ANY with "<" Operator

      We can use the comparison operator "<" (less than) with the MySQL ANY operator to verify whether a particular column value is less than the column value of any of the records returned by the subquery.

      Example

      In this query, we are selecting all records from the CUSTOMERS table where the SALARY column is less than any of the salaries associated with customers whose age is 32.
      SELECT * FROM CUSTOMERS
      WHERE SALARY < ANY
      (SELECT SALARY FROM CUSTOMERS WHERE AGE = 32);

      Output

      The salary of 32 aged customer is 2000. The only customer with salary less than 2000 is 'Khilan'
      ID
      NAME
      AGE
      ADDRESS
      SALARY
      2
      Khilan
      25
      Delhi
      1500.00

      ANY with "=" operator

      We can use the MySQL ANY operator with the comparison operator "=" (equal to) to fetch the records from a table where a column value is equal to any value returned by a subquery.

      Example

      Here, we are trying to select all records from the CUSTOMERS table where the AGE column matches any of the AGE associated with customers named "Khilan".
      SELECT * FROM CUSTOMERS
      WHERE AGE = ANY
      (SELECT AGE FROM CUSTOMERS WHERE NAME = "Khilan");

      Output

      The age of 'khilan' is 25. Another customer whose age is equal to 25 is 'Chaitali'.
      ID
      NAME
      AGE
      ADDRESS
      SALARY
      2
      Khilan
      25
      Delhi
      1500.00
      4
      Chaitali
      25
      Mumbai
      6500.00

      ANY with "<>" Operator

      We can use the MySQL ANY operator with "<>" (not equal to) comparison operator to fetch the records from a table where a column value is not equal to any value returned by a subquery.

      Example

      In this query, we are selecting all records from the CUSTOMERS table where the ADDRESS column does not match any of the addresses associated with customers named "Ramesh".
      SELECT * FROM CUSTOMERS
      WHERE ADDRESS <> ANY
      (SELECT ADDRESS FROM CUSTOMERS WHERE NAME = "Ramesh");

      Output

      The address of 'Ramesh' is Ahmedabad. Following are the customers whose address is not equal to Ahmedabad.
      ID
      NAME
      AGE
      ADDRESS
      SALARY
      2
      Khilan
      25
      Delhi
      1500.00
      3
      Kaushik
      23
      Kota
      2000.00
      4
      Chaitali
      25
      Mumbai
      6500.00
      5
      Hardik
      27
      Bhopal
      8500.00
      6
      Komal
      22
      Hyderabad
      4500.00
      7
      Muffy
      24
      Indore
      10000.00

      ANY with "<=" Operator

      The MySQL ANY operator returns true if a value is less than or equal to any value in a specified set when used with the "<=" comparison operator.

      Example

      Here, we are selecting all records from the CUSTOMERS table where the AGE column is less than or equal to any age value in the AGE column where the SALARY is equal to 10000.
      SELECT * FROM CUSTOMERS
      WHERE AGE <= ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY = 10000);

      output

      The age of customer whose salary is 10000 is 24. So, the following are the customers whose age is less than 24
      ID
      NAME
      AGE
      ADDRESS
      SALARY
      3
      Kaushik
      23
      Kota
      2000.00
      6
      Komal
      22
      Hyderabad
      4500.00
      7
      Muffy
      24
      Indore
      10000.00

      ANY with ">=" Operator

      The MySQL ANY operator returns true if a value is greater than or equal to any value in a specified set when used with the ">=" comparison operator.

      Example

      In this query, we are selecting all records from the CUSTOMERS table where the 'AGE' is greater than or equal to any value in the result set obtained by selecting 'AGE' from 'CUSTOMERS' where 'SALARY' is equal to 10,000.
      SELECT * FROM CUSTOMERS
      WHERE AGE >= ANY
      (SELECT AGE FROM CUSTOMERS WHERE SALARY = 10000);

      Output

      The output for the program above is produced as given below
      ID
      NAME
      AGE
      ADDRESS
      SALARY
      1
      Ramesh
      32
      Ahmedabad
      2000.00
      2
      Khilan
      25
      Delhi
      1500.00
      4
      Chaitali
      25
      Mumbai
      6500.00
      5
      Hardik
      27
      Bhopal
      8500.00
      7
      Muffy
      24
      Indore
      10000.00

      ANY Operator Using a Client Program

      Besides using MySQL queries to perform the ANY operator, we can also use client programs like Node.js, PHP, Java, and Python to achieve the same result.

      Syntax

      Following are the syntaxes of this operation in various programming languages
      PHPNodeJSJavaPython
      To perform the ANY Operator on MySQL table through a PHP program, we need to execute SELECT statement with ANY operator using the mysqli function query() as follows
      $sql = "SELECT COLUMN1, COLUMN2, ... FROM TABLE_NAME
      WHERE COLUMN_NAME OPERATOR ANY (SUBQUERY)";
      $mysqli->query($sql);

      Example

      Following are the programs
      PHPNodeJSJavaPython
      $dbhost = 'localhost';
      $dbuser = 'root';
      $dbpass = 'password';
      $dbname = 'TUTORIALS';
      $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
      if($mysqli->connect_errno ) {
      printf("Connect failed: %s", $mysqli->connect_error);
      exit();
      }
      //printf('Connected successfully.');
      $sql = "SELECT * FROM tutorials_tbl WHERE tutorial_author = ANY (SELECT tutorial_author FROM tutorials_tbl WHERE tutorial_id > 3)";
      $result = $mysqli->query($sql);
      if ($result->num_rows > 0) {
      printf("Table records: \n");
      while($row = $result->fetch_assoc()) {
      printf("Id %d, Title: %s, Author: %s, S_date %s",
      $row["tutorial_id"],
      $row["tutorial_title"],
      $row["tutorial_author"],
      $row["submission_date"]);
      printf("\n");
      }
      } else {
      printf('No record found.');
      }
      mysqli_free_result($result);
      $mysqli->close();

      Output

      The output obtained is as follows
      Table records:
      Id 4, Title: Learn PHP, Author: John Poul, S_date 2023-07-26
      Id 5, Title: Learn MySQL, Author: Abdul S, S_date 2023-07-26
      Id 6, Title: Learn MySQL, Author: Mahesh, S_date 2023-07-26