In this PostgreSQL post explains how to use the PostgreSQL BETWEEN condition with syntax and examples.
The PostgreSQL BETWEEN condition is used to retrieve values within a range in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the BETWEEN condition in PostgreSQL is:
A column or calculation.
These values create an inclusive range that expression is compared to.
Let's look at some PostgreSQL BETWEEN condition examples using numeric values. The following numeric example uses the BETWEEN condition to retrieve values within a numeric range.
For example:
This PostgreSQL BETWEEN example would return all rows from the employees table where the employee_id is between 200 and 300 (inclusive). It is equivalent to the following SELECT statement:
Next, let's look at how you would use the PostgreSQL BETWEEN condition with Dates.
The following date example uses the BETWEEN condition to retrieve values within a date range.
For example:
This PostgreSQL BETWEEN condition example would return all records from the employees table where the start_date is between April 1, 2014 and April 30, 2014 (inclusive). It would be equivalent to the following SELECT statement:
The PostgreSQL BETWEEN condition can also be combined with the NOT operator. Here is an example of how you would combine the BETWEEN condition with the NOT Operator.
For example:
This PostgreSQL BETWEEN example would return all rows from the employees table where the employee_id was NOT between 500 and 599, inclusive. It would be equivalent to the following SELECT statement: