SQL Standard

SQL Between Inclusive

The SQL between operators is used to determine or fetch results within a specific range of values. The between operator is available in Standard SQL and is adopted by most database engines.

By default, the between operator is inclusive. This means that it will include the start and end values of the provided range.

Let us explore how to use the between operator in SQL.

SQL Between Operator

One common use of the between operator is in conjunction with the where clause. This allows you to filter for results that match the specified range.

SELECT column_name(s)
FROM TABLE_NAME
WHERE column_name BETWEEN start_expression AND end_expression

The above query allows you to filter for results that match the range within the specified column.

For example, assume that we have a table as shown below:

Let us use the between operator to get the results where the price is between 50 and 1000. We can run a code as shown below:

SELECT
  *
FROM
  sample.inventory
WHERE
  price BETWEEN 50 AND 1000;

We select all the columns from the inventory table in the sample database in the query. We then use the where clause to filter the prices between 50 and 1000.

This should return a result:

SQL Between Dates

Another common use of the between operator is filtering for matching records on a specific date range.

Consider the example shown below:

SELECT
  *
FROM
  sample.inventory
WHERE
  price BETWEEN '2021-01-01'
  AND '2021-12-31';

The above query filters for records that are between January 1st 2021 to December 31st 2021.

Final Thoughts

In this article, the between operator allows you to filter for results that match within a date or numerical range. It also provides testing whether an expression falls within a set range.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list