MySQL consists of various operators for performing efficient data queries, and the BETWEEN operator is a key operator for filtering records within a specific range. By specifying a lower and upper bound, BETWEEN helps you easily retrieve data that falls between two values. This operator simplifies queries involving ranges of numbers or dates, making data analysis more simple and effective.
In this article, we will explore the BETWEEN operator, highlighting its usage and examples.
MySQL BETWEEN Operator
The BETWEEN operator in MySQL is used to filter results within a specified range, including both boundary values. It simplifies queries by allowing you to set a lower and upper limit for numerical, date, or text data. This operator is useful for retrieving data that falls within a particular range efficiently.
Syntax:
The syntax for using BETWEEN Operator in MySQL is as follows:
column_name BETWEEN value1 AND value2;
Parameters:
- column_name: The column to be filtered.
- value1: The lower bound of the range.
- value2: The upper bound of the range.
MySQL BETWEEN Operator Examples
Let’s look at some examples of the BETWEEN Operator in MySQL. Learning the BETWEEN Operator with examples will help in understanding the concept better.
First, let’s create a table:
Demo MySQL Tables
We create the first table "employees_2023" in this example.
CREATE TABLE employees_2023 (
employee_id INT,
name VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees_2023 (employee_id, name, salary) VALUES
(1, 'Gaurav', 75000.00),
(2, 'Yuvraj', 82000.00),
(3, 'Shruti', 69000.00);
SELECT * FROM employees_2023;
Output:
+-------------+--------+----------+
| employee_id | name | salary |
+-------------+--------+----------+
| 1 | Gaurav | 75000.00 |
| 2 | Yuvraj | 82000.00 |
| 3 | Shruti | 69000.00 |
+-------------+--------+----------+
To create the second table “employees_2024“, write the following SQL queries:
CREATE TABLE employees_2024 (
employee_id INT,
name VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees_2024 (employee_id, name, salary) VALUES
(4, 'Anjali', 90000.00),
(5, 'Vaishnavi', 85000.00),
(6, 'Gauri', 78000.00);
SELECT * FROM employees_2024;
Output:
+-------------+-----------+----------+
| employee_id | name | salary |
+-------------+-----------+----------+
| 4 | Anjali | 90000.00 |
| 5 | Vaishnavi | 85000.00 |
| 6 | Gauri | 78000.00 |
+-------------+-----------+----------+
Example 1: Filter Employees with Salary Between 70000 and 80000
In this example, we are using the BETWEEN operator to retrieve employees from the employees_2023 table whose salaries fall between 70,000 and 80,000. This query helps identify employees within this specific salary range.
SELECT * FROM employees_2023 WHERE salary BETWEEN 70000 AND 80000;
Output:
+-------------+--------+----------+
| employee_id | name | salary |
+-------------+--------+----------+
| 1 | Gaurav | 75000.00 |
+-------------+--------+----------+
Example 2: Filter Employees with Salary Between 75000 and 85000
In this example, we are using the BETWEEN operator to find employees in the employees_2024 table with salaries ranging from 75,000 to 85,000. This query highlights employees whose salaries fall within this range.
SELECT * FROM employees_2024 WHERE salary BETWEEN 75000 AND 85000;
Output:
+-------------+-----------+----------+
| employee_id | name | salary |
+-------------+-----------+----------+
| 5 | Vaishnavi | 85000.00 |
| 6 | Gauri | 78000.00 |
+-------------+-----------+----------+
Example 3: Filter Employees with Salary Not in the Range of 70000 to 80000
In this example, we are using the NOT BETWEEN operator to select employees from the employees_2023 table whose salaries do not fall between 70,000 and 80,000. This query filters out those outside the specified salary range.
SELECT * FROM employees_2023 WHERE salary NOT BETWEEN 70000 AND 80000;
Output:
+-------------+--------+----------+
| employee_id | name | salary |
+-------------+--------+----------+
| 2 | Yuvraj | 82000.00 |
| 3 | Shruti | 69000.00 |
+-------------+--------+----------+
Conclusion
In conclusion, the MySQL BETWEEN operator is an useful Operator for filtering records within a specific range, whether for numerical values, dates, or text. Its ability to simplify queries by setting lower and upper limits makes data retrieval more efficient and precise. Using this operator effectively can enhance your database queries and improve data analysis.
Similar Reads
MySQL IN Operator
The MySQL IN operator is used to filter data based on a specified set of values. It is a shorthand for multiple OR conditions which allows us to specify a list of values in a WHERE clause to match records that have any of those values. This makes your SQL queries more concise and easier to read. MyS
3 min read
MySQL EXCEPT Operator
Comparison of two different sets of data in a database and finding entries unique to one set is one of the common operations done on databases. The EXCEPT operator in SQL compares the two result sets returned and returns only the rows that are found in one but not the other. Moreover, MySQL, one of
6 min read
MySQL INTERSECT Operator
In MySQL, the INTERSECT operator is used to find common records between two result sets. However, MySQL does not support the INTERSECT operator natively. To achieve similar functionality, you can use alternative methods such as INNER JOINs or subqueries. These techniques allow you to retrieve the in
4 min read
MySQL LIKE Operator
The MySQL LIKE operator helps us search for specified patterns in a column. It is useful when we need to find records that match specific patterns, like names starting with a certain letter or containing a specific word. In this article, we will cover the concept, syntax, and examples of using the L
3 min read
SQL | BETWEEN & IN Operator
In SQL, the BETWEEN and IN operators are widely used for filtering data based on specific criteria. The BETWEEN operator helps filter results within a specified range of values, such as numbers, dates, or text, while the IN operator filters results based on a specific list of values. Both operators
5 min read
SQL BETWEEN Operator
The BETWEEN operator in SQL is used to filter records within a specific range. Whether applied to numeric, text, or date columns it simplifies the process of retrieving data that falls within a particular boundary. In this article, we will explore the SQL BETWEEN operator with examples. SQL BETWEEN
3 min read
PL/SQL BETWEEN Operator
When working with databases, filtering data based on certain criteria is a common requirement. PL/SQL offers various operators to facilitate this, with one of the most effective being the BETWEEN operator. This operator enables us to select values that lie in a specific range whether those values ar
4 min read
Arithmetic Operators in MySQL
Arithmetic operators in MySQL are tools for performing basic math in database queries. They handle addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) operations. These operators are vital for tasks like calculating totals, differences, products, quotients, and remainder
5 min read
MySQL NOT EQUAL Operator
SQL (Structured Query Language) is a powerful language used for managing and manipulating relational databases. It provides a standardized language for querying the databases which are found to be structured, allowing users to define, manipulate, and control the data retrieval with ease. SQL operate
4 min read
MySQL UNION Operator
Database management is an important concept of handling and organizing data effectively. One powerful Operator in MySQL for combining results from multiple queries is the UNION operator. This operator allows you to merge the results of two or more SELECT statements into a single result set, eliminat
4 min read