Site icon TestingDocs.com

MySQL Logical Operators

Overview

In this tutorial, we will learn about MySQL Logical Operators with examples. We can combine several criteria with logical operators in the WHERE clause.

MySQL Logical Operators

Logical operators are used to combine multiple Boolean expressions. Logical operators in MySQL are as follows:

MySQL Logical Operator Description
AND Logical AND

Evaluates to true if both the two expressions evaluate to true.

In the older version we can also use the double ampersand (&&) operator instead of AND operator. Note that this will be deprecated in future releases.

OR Logical OR

Evaluates to true if either of the two expressions evaluates to true.

In the older version we can also use the double vertical pipes (|| )operator instead of OR operator. Note that this will be deprecated in future releases.

XOR Logical exclusive – OR

Evaluates to true if exactly one of the two expressions evaluates to true.

NOT Logical Negation

Evaluates to true if the expression evaluates to false.
We can also use an exclamation point !

Examples

AND Operator

In this example, we will use the Country table from the world MySQL database. We combine two conditions with the AND operator to retrieve rows from the table.

mysql> SELECT Name, Population
-> FROM Country
-> WHERE Continent =’North America’ AND Population > 8000000
-> ORDER BY Population;
+——————–+————+
| Name | Population |
+——————–+————+
| Haiti | 8222000 |
| Dominican Republic | 8495000 |
| Cuba | 11201000 |
| Guatemala | 11385000 |
| Canada | 31147000 |
| Mexico | 98881000 |
| United States | 278357000 |
+——————–+————+
7 rows in set (0.00 sec)

 

OR Operator

In this example, we will use the City table from the world MySQL database.

mysql> use world;
Database changed

mysql> SELECT Id, Name, District
-> FROM City
-> WHERE Name=’New York’ OR Name=’Jersey City’;
+——+————-+————+
| Id | Name | District |
+——+————-+————+
| 3793 | New York | New York |
| 3864 | Jersey City | New Jersey |
+——+————-+——-

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version