How to use Relational Operators in MySQL
Last Updated :
24 Apr, 2024
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.
What is Relational Operators
Relational operators in MySQL are symbols or keywords to compare the values in SQL queries. They perform comparisons and return a boolean result which is either true or false. Developers can use these operators to check for equality and inequality as well as perform comparisons based on numeric values, strings, or dates.
Different Relational Operators in MySQL
MySQL enables the implementation of the relational operators which are used for the data comparison and serve their purposes separately. Some of the most commonly used relational operators include: Some of the most commonly used relational operators include
- Equal to (=): Equality operator compares if two values are equal. It is usually applied in WHERE clauses to winnow out rows whose value matches a corresponding value.
- Not equal to (!= or <>): Not equal to operator compares two values and returns true if they are not equal. It returns true if values are different. They can be used interchangeably.
- Greater than (>): The > (greater than) operator tests if the left operand is greater than the right operand. It is used to remove the rows where a column value is more than a predefined value.
- Less than (<): The less than sign checks whether the left operand is less than the right operand. It is one of the most popular operators to filter records where the value of a column is smaller than a specified one.
- Greater than or equal to (>=): The “greater than or equal to” operator looks at whether the left operand is greater than or equal to the right operand. It is used to filter the rows when the value of the column is greater than or equal to the given value.
- Less than or equal to (<=): The operator less than or equal to compares the left operand to the right operand to see if it is less than or equal to. It is meant for the elimination of the rows in which a column value is less than or equal to a certain value.
Setup an Environment
--Make the 'employees' table
CREATE TABLE employees (
employee_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary INT
);
-- Fill up the 'employees' table with some sample data.
INSERT INTO employees (employee_id, first_name, last_name, department, salary) VALUES
(1, 'Alice', 'Smith', 'Sales', 50000),
(2, 'Bob', 'Johnson', 'Marketing', 60000),
(3, 'Charlie', 'Brown', 'IT', 70000),
(4, 'David', 'Lee', 'Finance', 55000);
Output:
+-------------+------------+-----------+------------+--------+
| employee_id | first_name | last_name | department | salary |
+-------------+------------+-----------+------------+--------+
| 1 | Alice | Smith | Sales | 50000 |
| 2 | Bob | Johnson | Marketing | 60000 |
| 3 | Charlie | Brown | IT | 70000 |
| 4 | David | Lee | Finance | 55000 |
+-------------+------------+-----------+------------+--------+
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.
Conclusion
Relational operators are the inseparable features of the SQL developer's toolkit, which lets the professions filter and query the data mainly with accuracy. Thus, the knowledge of simply how to use relational operators in MySQL is another significant step on the way to discovery in further development of your systems based on the use of the database and an ability to manipulate data and analyze it.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read