Examples of How to use Relational Operators in MySQL
Below are the different operators with examples and output and explanation.
1. Equality Check (=) Operators
Query:
SELECT * FROM employees WHERE department = 'Sales';
Output:
+-------------+------------+-----------+------------+--------+
| employee_id | first_name | last_name | department | salary |
+-------------+------------+-----------+------------+--------+
| 1 | Alice | Smith | Sales | 50000 |
+-------------+------------+-----------+------------+--------+
Explanation: This query selects all employees from the employees table whose department is ‘Sales‘. It uses the equality operator (=) to compare the values in the department column with the specified value ‘Sales‘.
2. Not Equality Check (!=) Operators
Query:
SELECT * FROM employees WHERE salary != 60000;
Output:
+-------------+------------+-----------+------------+--------+
| employee_id | first_name | last_name | department | salary |
+-------------+------------+-----------+------------+--------+
| 1 | Alice | Smith | Sales | 50000 |
| 3 | Charlie | Brown | IT | 70000 |
| 4 | David | Lee | Finance | 55000 |
+-------------+------------+-----------+------------+--------+
Explanation: This query retrieves all employees from the employees table whose salary is not equal to 60000. It uses the inequality operator (!=) to filter out rows where the salary is not equal to the specified value
3. Greater Than(>) Operators
Query:
SELECT * FROM employees WHERE salary > 55000;
Output:
+-------------+------------+-----------+------------+--------+
| employee_id | first_name | last_name | department | salary |
+-------------+------------+-----------+------------+--------+
| 2 | Bob | Johnson | Marketing | 60000 |
| 3 | Charlie | Brown | IT | 70000 |
+-------------+------------+-----------+------------+--------+
Explanation: This query retrieves all employees from the employees table whose salary is greater than 55000. It uses the greater than operator (>) to compare the values in the salary column with the specified value of 55000.
4. Less Than (<) Operators
Query:
SELECT * FROM employees WHERE salary < 60000;
Output:
+-------------+------------+-----------+------------+--------+
| employee_id | first_name | last_name | department | salary |
+-------------+------------+-----------+------------+--------+
| 1 | Alice | Smith | Sales | 50000 |
| 4 | David | Lee | Finance | 55000 |
+-------------+------------+-----------+------------+--------+
Explanation: This query retrieves all employees from the employees table whose salary is less than 60000. It uses the less than operator (<) to compare the values in the salary column with the specified value of 60000.
5. Greater Than or Equal To (>=) Operators
Query:
SELECT * FROM employees WHERE salary >= 60000;
Output:
+-------------+------------+-----------+------------+--------+
| employee_id | first_name | last_name | department | salary |
+-------------+------------+-----------+------------+--------+
| 2 | Bob | Johnson | Marketing | 60000 |
| 3 | Charlie | Brown | IT | 70000 |
+-------------+------------+-----------+------------+--------+
Explanation: This query retrieves all employees from the employees table whose salary is greater than or equal to 60000. It uses the greater than or equal to the operator (>=) to compare the values in the salary column with the specified value of 60000.
6. Less Than or Equal To (<=) Operators
Query:
SELECT * FROM employees WHERE salary <= 55000;
Output:
+-------------+------------+-----------+------------+--------+
| employee_id | first_name | last_name | department | salary |
+-------------+------------+-----------+------------+--------+
| 1 | Alice | Smith | Sales | 50000 |
| 4 | David | Lee | Finance | 55000 |
+-------------+------------+-----------+------------+--------+
Explanation: This query retrieves all employees from the employees table whose salary is less than or equal to 55000. It uses the less than or equal to the operator (<=) to compare the values in the salary column with the specified value of 55000.
How to use Relational Operators in MySQL
In the database management area, relational operators are the building blocks for data filtering and querying. MySQL, one of the most popular relational database management systems that are widely used across the world, features a wide range of relational operators that are highly efficient in handling and comparing data.
In this article, we will have a look at the role of relational operators in MySQL and ways in which developers can use them effectively and correctly.