# BETWEEN

### Introduction to the PostgreSQL BETWEEN operator <a href="#introduction-to-the-postgresql-between-operator" id="introduction-to-the-postgresql-between-operator"></a>

The `BETWEEN` operator allows you to check if a value falls within a range of values.

The basic syntax of the `BETWEEN` operator is as follows

```sql
value BETWEEN low AND high;
```

If the `value` is greater than or equal to the `low` value and less than or equal to the `high` value, the `BETWEEN` operator returns `true`; otherwise, it returns `false`.

You can rewrite the `BETWEEN` operator by using the greater than or equal ( `>=`) and less than or equal to ( `<=`) operators and the logical AND operator.

```sql
value >= low AND value <= high
```

If you want to check if a value is outside a specific range, you can use the `NOT BETWEEN` operator as follows,

```sql
value NOT BETWEEN low AND high
```

The following expression is equivalent to the expression that uses the `NOT BETWEEN` operators

```sql
value < low OR value > high
```

## PostgreSQL BETWEEN operator examples <a href="#postgresql-between-operator-examples" id="postgresql-between-operator-examples"></a>

Let’s take a look at the `payment`table in the [dvdrental](/database-engineering/sample-databases/dvd-rental-database.md)

### 1) Using the PostgreSQL BETWEEN operator with numbers <a href="#id-1-using-the-postgresql-between-operator-with-numbers" id="id-1-using-the-postgresql-between-operator-with-numbers"></a>

The following query uses the `BETWEEN` operator to retrieve payments with `payment_id` is between `17503` and `17505`

```sql
SELECT
  payment_id,
  amount
FROM
  payment
WHERE
  payment_id BETWEEN 17503 AND 17505
ORDER BY
  payment_id;
```

Output:

```
payment_id | amount
------------+--------
      17503 |   7.99
      17504 |   1.99
      17505 |   7.99
(3 rows)
```

### 2) Using the PostgreSQL NOT BETWEEN example <a href="#id-2-using-the-postgresql-not-between-example" id="id-2-using-the-postgresql-not-between-example"></a>

The following example uses the `NOT BETWEEN` operator to find payments with the `payment_id` not between `17503` and `17505`

```sql
SELECT
  payment_id,
  amount
FROM
  payment
WHERE
  payment_id NOT BETWEEN 17503 AND 17505
ORDER BY
  payment_id;
```

Output:&#x20;

```
payment_id | amount
------------+--------
      17506 |   2.99
      17507 |   7.99
      17508 |   5.99
      17509 |   5.99
      17510 |   5.99
...
```

### 3) Using the PostgreSQL BETWEEN with a date range <a href="#id-3-using-the-postgresql-between-with-a-date-range" id="id-3-using-the-postgresql-between-with-a-date-range"></a>

If you want to check a value against a date range, you use the literal date in ISO 8601 format, which is `YYYY-MM-DD`.

The following example uses the `BETWEEN` operator to find payments whose payment dates are between `2007-02-15` and `2007-02-20` and amount more than 10.

```sql
SELECT
  payment_id,
  amount,
  payment_date
FROM
  payment
WHERE
  payment_date BETWEEN '2007-02-15' AND '2007-02-20'
  AND amount > 10
ORDER BY
  payment_date
LIMIT
  5;
```

<figure><img src="/files/JOoRc2uQJnx2hnM03Wba" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://courses.parottasalna.com/database-engineering/filtering-data/between.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
