PostgreSQL BETWEEN Query with Example

⚡ Smart Summary

PostgreSQL BETWEEN retrieves values that fall within an inclusive range inside SELECT, INSERT, UPDATE, or DELETE statements. It compares an expression against two boundary values, works with numeric and date data, and supports negation through the NOT operator.

  • 📌 Core Purpose: The BETWEEN operator matches an expression against a range defined by two values joined with AND.
  • 🔢 Inclusive Bounds: Both boundary values are included, so BETWEEN 200 AND 280 returns rows equal to 200 or 280.
  • 📅 Numeric and Dates: The same syntax filters numeric columns and date columns using quoted date literals.
  • 🚫 NOT Variant: Adding NOT before BETWEEN returns every row that falls outside the specified range.
  • 🖥️ pgAdmin Execution: The identical queries run in the pgAdmin query editor by selecting the Demo database and clicking Execute.

PostgreSQL BETWEEN Query with Example

BETWEEN Operator in PostgreSQL

The PostgreSQL BETWEEN Operator helps in retrieving values within a range in a SELECT, UPDATE, INSERT, or DELETE statement. With the BETWEEN operator, it is possible to match a value against a range of values. Because the range is inclusive, both boundary values are treated as part of the match.

PostgreSQL Between Query Syntax

Here is the syntax of the BETWEEN operator in PostgreSQL:

expression BETWEEN value-1 AND value-2;

The expression is simply a column or a calculation. The value-1 and value-2 will create a range for us to compare the expression to.

PostgreSQL Between Query with Numeric

We need to create an example that shows how to apply the BETWEEN operator on numeric values. Consider the Price table given below:

Price:

PostgreSQL Between Query with Numeric

Let us see the list of all books whose price is between 200 and 280:

SELECT *
FROM Price
WHERE price BETWEEN 200 AND 280;

This will return the following:

PostgreSQL Between Query with Numeric

Only two items have a price ranging between 200 and 280. Note that the two boundaries are included, that is, 200 and 280.

PostgreSQL Between Query with Date

The BETWEEN operator can be used on date values. This means that we can specify the range of date values that we need to work with. Consider the following Employees table:

Employees:

PostgreSQL Between Query with Date

Suppose we want to see all the employees who were employed between 2013-01-01 and 2015-01-01, we can run the following command:

SELECT *
FROM Employees
WHERE employment_date BETWEEN '2013-01-01' AND '2015-01-01';

This returns the following:

PostgreSQL Between Query with Date

Postgres Between Query using NOT Operator

We can combine the BETWEEN operator with the NOT operator. In such a case, the list of values that are not within the specified range will be returned.

For example, to see all the items where the price is not between 200 and 280, we can run the following query:

SELECT *
FROM Price
WHERE price NOT BETWEEN 200 AND 280;

This will return the following:

Postgres Between Query using NOT Operator

Two items with a price not ranging between 200 and 280 were found. Hence, their details were returned.

PostgreSQL Between Query using pgAdmin

Now let’s see how these actions can be performed using pgAdmin. The queries are identical to the ones above; only the execution environment changes.

How To Use Between Query with Numeric in PostgreSQL using pgAdmin

Here is how to use the Between query with Numeric values in PostgreSQL using pgAdmin:

Step 1) Login to your pgAdmin account.

Open pgAdmin and login to your account using your credentials.

Step 2) Create a Demo Database.

  1. From the navigation bar on the left, click Databases.
  2. Click Demo.

PostgreSQL Between Query using pgAdmin

Step 3) Type the below query in the query editor:

SELECT *
FROM Price
WHERE price BETWEEN 200 AND 280;

Step 4) Click the Execute button.

PostgreSQL Between Query using pgAdmin

It should return the following:

PostgreSQL Between Query using pgAdmin

With Date

To accomplish the same through pgAdmin, do this:

Step 1) Login to your pgAdmin account.

Step 2)

  1. From the navigation bar on the left, click Databases.
  2. Click Demo.

Between Query in PostgreSQL With Date

Step 3) Type the query in the query editor:

SELECT *
FROM Employees
WHERE employment_date BETWEEN '2013-01-01' AND '2015-01-01';

Step 4) Click the Execute button.

Between Query in PostgreSQL With Date

It should return the following:

Between Query in PostgreSQL With Date

Using NOT Operator

To accomplish the same through pgAdmin, do this:

Step 1) Login to your pgAdmin account.

Step 2)

  1. From the navigation bar on the left, click Databases.
  2. Click Demo.

Between Query in PostgreSQL using NOT Operator

Step 3) Type the query in the query editor:

SELECT *
FROM Price
WHERE price NOT BETWEEN 200 AND 280;

Step 4) Click the Execute button.

PostgreSQL Between Query using NOT Operator

It should return the following:

PostgreSQL Between Query using NOT Operator

Download the Database used in this Tutorial

FAQs

Yes. AI assistants can convert plain English, such as “orders between two dates,” into a valid BETWEEN query with the correct column, bounds, and quotes. Always test the generated SQL on your schema before running it.

Yes. AI tools can point out that BETWEEN is inclusive of both bounds, that date literals may need timestamps, and that column data types affect comparisons. Verify each explanation against your actual data and column definitions.

Yes. BETWEEN is inclusive, so x BETWEEN 200 AND 280 matches rows where the value equals 200, 280, or anything in between. It is equivalent to writing x >= 200 AND x <= 280.

Wrap each date in single quotes, for example employment_date BETWEEN ‘2013-01-01’ AND ‘2015-01-01’. For timestamp columns, extend the upper bound or use < the next day so late-in-day times are not excluded.

Summarize this post with: