MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
Comparison operators are used in the WHERE clause to filter the results of a SELECT, INSERT, UPDATE, or DELETE statement.
Here are the most common comparison operators used in MySQL:
=
: Equal to<>
or !=
: Not equal to<
: Less than>
: Greater than<=
: Less than or equal to>=
: Greater than or equal toBETWEEN ... AND ...
: Between an inclusive rangeLIKE
: Search for a patternIN
: If the operand is equal to one of a list of expressionsExamples:
=
)SELECT * FROM employees WHERE employee_id = 1;
This will select all the data from the employees
table where employee_id
is equal to 1.
<>
or !=
)SELECT * FROM employees WHERE employee_id <> 1;
This will select all the data from the employees
table where employee_id
is not equal to 1.
<
)SELECT * FROM employees WHERE salary < 50000;
This will select all the data from the employees
table where salary
is less than 50000.
>
)SELECT * FROM employees WHERE salary > 50000;
This will select all the data from the employees
table where salary
is greater than 50000.
<=
)SELECT * FROM employees WHERE salary <= 50000;
This will select all the data from the employees
table where salary
is less than or equal to 50000.
>=
)SELECT * FROM employees WHERE salary >= 50000;
This will select all the data from the employees
table where salary
is greater than or equal to 50000.
BETWEEN ... AND ...
)SELECT * FROM employees WHERE salary BETWEEN 50000 AND 60000;
This will select all the data from the employees
table where salary
is between 50000 and 60000, inclusive.
LIKE
)SELECT * FROM employees WHERE first_name LIKE 'J%';
This will select all the data from the employees
table where first_name
starts with "J". (The %
character is a wildcard that matches any number of characters.)
IN
)SELECT * FROM employees WHERE employee_id IN (1, 2, 3);
This will select all the data from the employees
table where employee_id
is either 1, 2, or 3.
These are the basic comparison operators in MySQL that you can use in your queries. You can also combine these operators using AND
and OR
to create more complex queries.
MySQL comparison operators examples:
SELECT column1 = column2 AS equal, column1 != column2 AS not_equal, column1 > column2 AS greater_than, column1 < column2 AS less_than, column1 >= column2 AS greater_than_or_equal, column1 <= column2 AS less_than_or_equal;
How to use equal to operator in MySQL:
SELECT * FROM table_name WHERE column1 = 10;
Not equal to operator in MySQL queries:
SELECT * FROM table_name WHERE column1 <> 5;
Greater than operator usage in MySQL:
SELECT * FROM table_name WHERE column1 > 20;
Less than operator in MySQL comparisons:
SELECT * FROM table_name WHERE column1 < 15;
Greater than or equal to operator in MySQL:
SELECT * FROM table_name WHERE column1 >= 10;
Less than or equal to operator in MySQL:
SELECT * FROM table_name WHERE column1 <= 25;
Combining multiple conditions with logical operators in MySQL:
SELECT * FROM table_name WHERE column1 > 10 AND column2 = 'value';
Case-sensitive vs case-insensitive comparisons in MySQL:
SELECT * FROM table_name WHERE column1 = 'Value' COLLATE utf8_bin;