MySQL
Comparison Operators
In MySQL, comparison operators are used to compare two values and return a boolean result (either TRUE or FALSE). They are commonly used in the WHERE clause to filter rows based on specific conditions, but they can also be used in SELECT, ORDER BY, and other parts of a query. These operators allow you to check for equality, inequality, and other relationships between values, such as greater than, less than, or within a range.
Here’s a detailed explanation of the most common comparison operators in MySQL, along with examples:
Equal to (
=):- The equal operator (
=) is used to compare if two values are the same. - Syntax:
SELECT first_name, last_name FROM employees WHERE department_id = 2;- This query retrieves employees who belong to department 2.
- The equal operator (
Not equal to (
<>or!=):- The not equal operators (
<>or!=) are used to compare if two values are different. - Example:
SELECT first_name, last_name FROM employees WHERE department_id <> 3;- This query returns employees who do not belong to department 3.
- The not equal operators (
Greater than (
>):- The greater than operator (
>) is used to check if the left operand is greater than the right operand. - Example:
SELECT first_name, last_name FROM employees WHERE salary > 50000;- This query retrieves employees with a salary greater than 50,000.
- The greater than operator (
Less than (
<):- The less than operator (
<) is used to check if the left operand is less than the right operand. - Example:
SELECT first_name, last_name FROM employees WHERE salary < 30000;- This query retrieves employees whose salary is less than 30,000.
- The less than operator (
Greater than or equal to (
>=):- The greater than or equal to operator (
>=) checks if the left operand is greater than or equal to the right operand. - Example:
SELECT first_name, last_name FROM employees WHERE salary >= 60000;- This query retrieves employees who have a salary of 60,000 or higher.
- The greater than or equal to operator (
Less than or equal to (
<=):- The less than or equal to operator (
<=) checks if the left operand is less than or equal to the right operand. - Example:
SELECT first_name, last_name FROM employees WHERE salary <= 40000;- This query retrieves employees who earn 40,000 or less.
- The less than or equal to operator (
BETWEEN (for range checks):
- The
BETWEENoperator is used to check if a value falls within a specified range. - Example:
SELECT first_name, last_name FROM employees WHERE salary BETWEEN 30000 AND 50000;- This query retrieves employees whose salary is between 30,000 and 50,000, inclusive.
- The
IN (for checking multiple values):
- The
INoperator is used to check if a value matches any value in a list of values. - Example:
SELECT first_name, last_name FROM employees WHERE department_id IN (1, 2, 3);- This query retrieves employees who belong to department 1, 2, or 3.
- The
IS NULL (for checking NULL values):
- The
IS NULLoperator is used to check if a value isNULL. - Example:
SELECT first_name, last_name FROM employees WHERE manager_id IS NULL;- This query retrieves employees who do not have a manager assigned (i.e.,
manager_idisNULL).
- The
IS NOT NULL (for checking non-NULL values):
- The
IS NOT NULLoperator is used to check if a value is notNULL. - Example:
SELECT first_name, last_name FROM employees WHERE manager_id IS NOT NULL;- This query retrieves employees who have a manager assigned.
- The
LIKE (for pattern matching):
- The
LIKEoperator is used for pattern matching in strings. Use%as a wildcard for multiple characters and_as a wildcard for a single character. - Example:
SELECT first_name, last_name FROM employees WHERE first_name LIKE 'J%';- This query retrieves employees whose first name starts with the letter 'J'.
- The
NOT LIKE:
- The
NOT LIKEoperator is used to filter out rows that match a specific pattern. - Example:
SELECT first_name, last_name FROM employees WHERE first_name NOT LIKE 'A%';- This query retrieves employees whose first name does not start with 'A'.
- The
Comparison operators in MySQL provide the ability to filter, sort, and compare data efficiently in a wide range of scenarios. Using these operators effectively allows for more precise and complex queries.
To gain complete access, login with gmail or outlook, no need of signup, click here
Test code
Fetch records for clients identified as female.
SELECT *
FROM org_client
WHERE gender = 'F';Fetch records for clients identified as male, filtering out those who are female.
SELECT *
FROM org_client
WHERE gender <> 'F';SELECT query to fetch salaries exceeding 150,000, excluding the amount of 150,000 itself.
SELECT *
FROM org_employee
WHERE salary > 150000
ORDER BY salary;SELECT query to fetch salaries below 150,000, excluding the amount of 150,000 itself.
SELECT *
FROM org_employee
WHERE salary < 150000
ORDER BY salary;SELECT query to retrieve salaries equal to or greater than 150,000, including the amount of 150,000 itself.
SELECT *
FROM org_employee
WHERE salary >= 150000
ORDER BY salary;SELECT query to retrieve salaries equal to or less than 150,000, including the amount of 150,000 itself.
SELECT *
FROM org_employee
WHERE salary <= 150000
ORDER BY salary;Retrieve clients by employing the BETWEEN SQL operator on numeric data, selecting those whose birth year falls within the range of 1985 and 1995.
SELECT *
FROM org_client
WHERE birth_year BETWEEN 1985 AND 1995;Here's an example SQL query utilizing the IN operator with string values. This query retrieves all rows from the employees table where the designation column corresponds to any of the specified values ('Sales Manager', 'Financial Analyst', 'CFO').
SELECT *
FROM org_employee
WHERE designation IN ('Sales Manager', 'Financial Analyst', 'CFO');Retrieve entries for clients with last names that begin with 'Me%', where '%' signifies the possibility of zero or more characters following 'Me'.
SELECT *
FROM org_client
WHERE last_name LIKE 'Me%';Retrieve entries for clients whose first names do not conclude with '%e'.
SELECT *
FROM org_client
WHERE first_name NOT LIKE '%e';Retrieve records using the IS NULL operator on a date column, select orders that have not been shipped yet.
SELECT *
FROM act_order
WHERE shipped_date IS NULRetrieve records using the IS NOT NULL operator on a date column, select orders that have been shipped yet.
SELECT *
FROM act_order
WHERE shipped_date IS NOT NULL;Here is an example SQL query employing the NOT IN operator with string values. The query retrieves all rows from the employees table where the designation column does not correspond to any of the specified values ('Sales Manager', 'Financial Analyst', 'CFO'). In other words, it retrieves all employee rows where the designation DOES NOT belong to ('Sales Manager', 'Financial Analyst', 'CFO').
SELECT *
FROM org_employee
WHERE designation NOT IN ('Sales Manager', 'Financial Analyst', 'CFO');Equal To (=)
SELECT * FROM org_client WHERE gender = 'F';
In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
IN OPERATOR
SELECT * FROM org_employee WHERE designation IN ('Sales Manager', 'Financial Analyst', 'CFO');
In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
BETWEEN OPERATOR
SELECT * FROM act_order WHERE order_date BETWEEN '2023-12-10' AND '2023-12-17';
In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
LIKE OPERATOR
SELECT * FROM org_client WHERE last_name LIKE 'Me%';
In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query. Retrieve entries for clients with last names that begin with 'Me%', where '%' signifies the possibility of zero or more characters following 'Me'.
IS NULL OPERATOR
SELECT * FROM prd_product WHERE description IS NULL;
In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query. NOTE that description value for product id 7 and 9 compose a empty string, this is not a null.


Comments Not Found