Introduction to MySQL Operators
MySQL comes with special characters or words to perform certain operations. MySQL Operators are applied to the operands in order to carry out specific operations.
What are Operators in MySQL?
Operators are used to specifying 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 right operand from the left operand | a – b |
* | Multiplication of two operands | a * b |
/ | Division of left operand by the right operand | a / b |
% | Modulus – the remainder of the division of 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 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 left operand is less than that of a 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 left operand is greater than or equal to the right operand, the condition becomes true; if not then false. | a >= b |
<= | If the value of left operand is less than or equal to the right operand, the condition becomes true; if not then false. | a <= b |
!< | If the value of left operand is not less than the value of right operand, the condition becomes true; if not then false. | a !< b |
!> | If the value of left operand is not greater than the value of right operand, 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 EMPLOYEE table as shown below to understand how to use the comparison operators as 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 the presence of a row in a table which satisfies a certain condition specified in the query. |
OR |
It is used to combine multiple conditions in a statement by using the WHERE clause. |
AND |
It allows the existence of 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 earlier to understand the usage of logical operators as shown 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
This has been a guide to MySQL Operators. Here we discuss different types of MySQL Operators like Arithmetic Operators, Comparison Operators, and Logical Operators with appropriate examples. You may also look at the following articles to learn more –
- IS MySQL is an OpenSource?
- How to use MySQL Commands
- Top 25 MySQL Query Commands
- Most Useful Cheat Sheet MySQL
- SQL Operators | Types with Code
7 Online Courses | 8 Hands-on Projects | 73+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses