Updated May 9, 2023
Introduction to MySQL Operators
MySQL comes with special characters or words to perform certain operations. MySQL Operators are applied to the operands to carry out specific operations.
What are Operators in MySQL?
Operators are used to specify a condition in a statement in MySQL. Below are the different types of operators used in MySQL.
1. Arithmetic Operators
In MySQL, arithmetic operators are used to perform the arithmetic operations as described below.
Arithmetic Operators in MySQL | ||
Operator | Description | Example |
+ | Addition of two operands | a + b |
– | Subtraction of the right operand from the left operand | a – b |
* | Multiplication of two operands | a * b |
/ | Division of the left operand by the right operand | a / b |
% | Modulus – the remainder of the division of the left operand by the right | a % b |
The following are a few examples of operations using Arithmetic Operators.
Let us assume certain values for the below variables as
a = 10 , b = 5
- a + b will give the result as 15.
- a – b will give the result as 5.
- a * b will give the result as 50.
- a / b will give the result as 2.
- a % b will give the result as 0.
2. Comparison Operators
The comparison operators in MySql are used to compare values between operands and return true or false according to the condition specified in the statement.
Comparison Operators in MySQL | ||
Operator | Description | Example |
> | If the value of the left operand is greater than that of the value of the right operand, the condition becomes true; if not, then false. | a > b |
< | If the value of the left operand is less than that of the value of the right operand, the condition becomes true; if not, then false. | a < b |
= | If both the operands have equal value, the condition becomes true; if not, then false. | a == b |
!= | If both the operands do not have equal value, the condition becomes true; if not, then false. | a != y |
>= | If the value of the left operand is greater than or equal to the right, the condition becomes true; if not, then false. | a >= b |
<= | If the value of the left operand is less than or equal to the right, the condition becomes true; if not, then false. | a <= b |
!< | If the value of the left operand is not less than that of the right, the condition becomes true; if not, then false. | a !< b |
!> | If the value of the left operand is not greater than that of the right, the condition becomes true; if not, then false. | a !> b |
<> | If the values of two operands are not equal, the condition becomes true; if not, then false. | a <> b |
Let us take an example of the EMPLOYEE table, as shown below, to understand how to use the comparison operators stated above while performing MySQL queries.
ID | NAME | AGE | SALARY |
4 | Sushma | 32 | 35000.00 |
6 | Ritu | 23 | 23000.00 |
8 | Amit | 27 | 30000.00 |
11 | Harish | 35 | 35000.00 |
18 | Pooja | 28 | 29500.00 |
Let us use the different comparison operators to query the EMPLOYEE table, as shown below.
SELECT * FROM EMPLOYEE WHERE SALARY > 25000;
ID | NAME | AGE | SALARY |
4 | Sushma | 32 | 35000.00 |
8 | Amit | 27 | 30000.00 |
11 | Harish | 35 | 35000.00 |
18 | Pooja | 28 | 29500.00 |
SELECT * FROM EMPLOYEE WHERE SALARY = 35000;
ID | NAME | AGE | SALARY |
4 | Sushma | 32 | 35000.00 |
11 | Harish | 35 | 35000.00 |
SELECT * FROM EMPLOYEE WHERE SALARY < 35000;
ID | NAME | AGE | SALARY |
6 | Ritu | 23 | 23000.00 |
8 | Amit | 27 | 30000.00 |
18 | Pooja | 28 | 29500.00 |
SELECT * FROM EMPLOYEE WHERE SALARY != 30000;
ID | NAME | AGE | SALARY |
4 | Sushma | 32 | 35000.00 |
6 | Ritu | 23 | 23000.00 |
11 | Harish | 35 | 35000.00 |
18 | Pooja | 28 | 29500.00 |
SELECT * FROM EMPLOYEE WHERE SALARY <> 35000;
ID | NAME | AGE | SALARY |
6 | Ritu | 23 | 23000.00 |
8 | Amit | 27 | 30000.00 |
18 | Pooja | 28 | 29500.00 |
3. Logical Operators
The logical operators used in MySQL are shown below.
Logical Operators in MySQL |
|
Operator |
Description |
BETWEEN |
It is used to search within a set of values by the minimum value and maximum value provided. |
EXISTS |
It is used to search for a row in a table that satisfies a specific condition specified in the query. |
OR |
It combines multiple conditions in a statement by using the WHERE clause. |
AND |
It allows multiple conditions in an SQL statement’s WHERE clause. |
NOT |
It reverses the meaning of the logical operator with which it is used. (Examples: NOT EXISTS, NOT BETWEEN, NOT IN, etc.) |
IN |
It is used to compare a value in a list of literal values. |
ALL |
It compares a value to all values in another set of values. |
ANY |
It compares a value to any value in the list according to the condition specified. |
LIKE |
It uses wildcard operators to compare a value to similar values. |
IS NULL |
It compares a value with a NULL value. |
UNIQUE |
It searches for every row of a specified table for uniqueness (no duplicates). |
Let us take the example of the same EMPLOYEE table as shown above to understand the usage of logical operators in the below queries.
SELECT * FROM EMPLOYEE WHERE AGE <= 25 AND SALARY >= 5000;
ID | NAME | AGE | SALARY |
6 | Ritu | 23 | 23000.00 |
SELECT * FROM EMPLOYEE WHERE AGE >= 25 OR SALARY >= 25000;
ID | NAME | AGE | SALARY |
4 | Sushma | 32 | 35000.00 |
8 | Amit | 27 | 30000.00 |
11 | Harish | 35 | 35000.00 |
18 | Pooja | 28 | 29500.00 |
SELECT * FROM EMPLOYEE WHERE AGE IS NOT NULL;
ID | NAME | AGE | SALARY |
4 | Sushma | 32 | 35000.00 |
6 | Ritu | 23 | 23000.00 |
8 | Amit | 27 | 30000.00 |
11 | Harish | 35 | 35000.00 |
18 | Pooja | 28 | 29500.00 |
SELECT * FROM EMPLOYEE WHERE NAME LIKE 'Am%';
ID | NAME | AGE | SALARY |
8 | Amit | 27 | 30000.00 |
SELECT * FROM EMPLOYEE WHERE AGE BETWEEN 25 AND 30;
ID | NAME | AGE | SALARY |
8 | Amit | 27 | 30000.00 |
18 | Pooja | 28 | 29500.00 |
SELECT NAME FROM EMPLOYEE WHERE EXISTS (SELECT NAME FROM EMPLOYEE WHERE SALARY > 25000);
NAME |
Sushma |
Amit |
Harish |
Pooja |
Recommended Articles
We hope that this EDUCBA information on “MySQL Operators” was beneficial to you. You can view EDUCBA’s recommended articles for more information.