SoFunction
Updated on 2025-04-21

Summary of common usages of SQL  BETWEEN

In SQL,BETWEENis an operator that selects data between two values.

It contains these two boundary values.BETWEENOperators are commonly usedWHEREIn clause, select values ​​within a range.

The following isBETWEENSome common uses of:

Select a value between two values: useBETWEENSelect 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, selectproductsAll 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 rangeBETWEENIt 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 BETWEENOperators andBETWEENInstead, it selects values ​​that are not within this range.

SELECT *
FROM table_name
WHERE column_name NOT BETWEEN value1 AND value2;

For example, selectemployeesEmployees whose wages are not between 5,000 and 10,000 in the table:

SELECT *
FROM employees
WHERE salary NOT BETWEEN 5000 AND 10000;

CombinedLIKEOperators use:BETWEENCan be withLIKEOperators 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: ifBETWEENAny endpoint ofNULL, then the result is false becauseNULLNot 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 aNULLis within the upper limit range.

Use in complex expressions:BETWEENIt 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.

BETWEENOperators are a very useful tool in SQL, which allows you to quickly select values ​​within a certain range. useBETWEENCan avoid writing multipleANDConditions make the query more concise. remember,BETWEENThe 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!