In SQL,BETWEEN
is an operator that selects data between two values.
It contains these two boundary values.BETWEEN
Operators are commonly usedWHERE
In clause, select values within a range.
The following isBETWEEN
Some common uses of:
Select a value between two values: useBETWEEN
Select values in the column, which are greater than or equal to one boundary value and less than or equal to another boundary value.
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
For example, selectproducts
All products in the table with prices between 10.00 and 20.00:
SELECT * FROM products WHERE price BETWEEN 10.00 AND 20.00;
Select records within the date range: BETWEEN
It is also often used to select records within a specific date range.
SELECT * FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-01-31';
This will return all orders for January.
**useNOT BETWEEN
**: NOT BETWEEN
Operators andBETWEEN
Instead, it selects values that are not within this range.
SELECT * FROM table_name WHERE column_name NOT BETWEEN value1 AND value2;
For example, selectemployees
Employees whose wages are not between 5,000 and 10,000 in the table:
SELECT * FROM employees WHERE salary NOT BETWEEN 5000 AND 10000;
CombinedLIKE
Operators use:BETWEEN
Can be withLIKE
Operators are used in conjunction to select a range in the string.
SELECT * FROM customers WHERE last_name BETWEEN 'A%' AND 'C%';
This query will select all clients whose last name starts with A, B, or C.
Use empty values: ifBETWEEN
Any endpoint ofNULL
, then the result is false becauseNULL
Not equivalent to any value.
SELECT * FROM table_name WHERE column_name BETWEEN value1 AND NULL;
This query does not return any rows, because no column values can be located in aNULL
is within the upper limit range.
Use in complex expressions:BETWEEN
It can also be used for more complex expressions, including functions and calculations.
SELECT * FROM sales WHERE (quantity * unit_price) BETWEEN 50 AND 200;
This will select a sales record with the product between 50 and 200.
BETWEEN
Operators are a very useful tool in SQL, which allows you to quickly select values within a certain range. useBETWEEN
Can avoid writing multipleAND
Conditions make the query more concise. remember,BETWEEN
The operator is inclusive, which means it includes the specified boundary value.
This is the end of this article about the common usage of SQL BETWEEN. For more relevant content on SQL between usage, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!