PostgreSQL Between Query Syntax PostgreSQL Between Query with Numeric PostgreSQL Between Query with Date Postgres Between Query using NOT Operator PostgreSQL Between Query using pgAdmin
PostgreSQL Between Query Syntax
Here is the syntax of the BETWEEN operator in PostgreSQL: The expression is simply a column or a calculation. The value-1, 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:
Let us the list of all books whose price is between 200 and 280: This will return the following:
Only two items have a price ranging between 200 and 280. Note that the two 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:
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: This returns the following:
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: This will return the following:
Two items with 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.
How To Use Between Query with Numeric in PostgreSQL using pgAdmin
Here is how to use Between query with Numeric 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
From the navigation bar on the left- Click Databases. Click Demo.
Step 3) Type the Query Type the below query in the query editor: Step 4) Execute the Query Click the Execute button
It should return the following:
With Date
To accomplish the same through pgAdmin, do this: Step 1) Login to your pgAdmin account. Step 2)
From the navigation bar on the left- Click Databases. Click Demo.
Step 3) Type the query in the query editor: Step 4) Click the Execute button.
It should return the following:
Using NOT Operator
To accomplish the same through pgAdmin, do this: Step 1) Login to your pgAdmin account. Step 2)
From the navigation bar on the left- Click Databases. Click Demo.
Step 3) Type the query in the query editor: Step 4) Click the Execute button.
It should return the following:
Summary
We use the BETWEEN operator to retrieve the value(s) that lie within a specified range using SELECT, UPDATE, INSERT, or DELETE statement. The range is specified using the AND operator. The operator can be used with numeric and date values. When used with numeric values, it helps us retrieve values that lie within the specified range. When used with date values, it helps us retrieve values that lie within a certain range. When used with the NOT operator, the BETWEEN operator returns values that don’t lie within the specified range.
Download the Database used in this Tutorial