PostgreSQL – BETWEEN operator
PostgreSQL BETWEEN operator is used to match a value against a range of values.
Syntax: value BETWEEN low AND high; Or, Syntax: value >= low and value;
The BETWEEN operator is used generally with WHERE clause with association with SELECT, INSERT, UPDATE or DELETE statement.
For the sake of this article we will be using the sample DVD rental database, which is explained here and can be downloaded by clicking on this link in our examples.
Example 1:
Here we will query for the payment whose amount is between 3 USD and 5 USD, using the BETWEEN operator in the “Payment” table of our sample database.
SELECT customer_id, payment_id, amount FROM payment WHERE amount BETWEEN 3 AND 5;
Output:
Example 2:
Here we will query for getting the payment whose payment date is between 2007-02-07 and 2007-02-15 using the BETWEEN operator in the “Payment” table of our sample database.
SELECT customer_id, payment_id, amount, payment_date FROM payment WHERE payment_date BETWEEN '2007-02-07' AND '2007-02-15';
Output:
Note: While making date queries the literal date in ISO 8601 format i.e., YYYY-MM-DD should be used in PostgreSQL.
Contact Us