PostgreSQL BETWEEN Operator
We use the BETWEEN operator to match a value against a range of values.The postgreSQL Between operator is used to select a value between a given range. Values can be text, dates, or numbers. The Between operator can be used with the SELECT, INSERT, UPDATE, or DELETE statements.
Between Operator Syntax :
The syntax of the Between Operator is −
The postgreSQL BETWEEN Operator will return records between the range( values1 and value2 ).
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
BETWEEN Operator Example :
Consider the Customer table with the following records -
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
1 | Aarav | 36 | Udaipur | 50000 |
2 | Vivaan | 33 | Mumbai | 30025 |
3 | Reyansh | 28 | Chennai | 35050 |
4 | Muhammad | 29 | Udaipur | 50075 |
5 | Sai | 27 | Mumbai | 27050 |
TEST CASE 1 :- The following code is an example, which would fetch the record from Customer table where Age is BETWEEN 20 to 30.
lfcdb-# SELECT * FROM Customer WHERE Age BETWEEN 20 AND 30;
The result for the respective postgreSQL query is as follows −
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
3 | Reyansh | 28 | Chennai | 35050 |
4 | Muhammad | 29 | Udaipur | 50075 |
5 | Sai | 27 | Mumbai | 27050 |
TEST CASE 2:- The following code is an example, which would fetch the record from Customer table where Age is NOT BETWEEN 20 to 30.
lfcdb-# SELECT * FROM Customer WHERE Age NOT BETWEEN 20 AND 30;
The result for the respective postgreSQL query is as follows −
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
1 | Aarav | 36 | Udaipur | 50000 |
2 | Vivaan | 33 | Mumbai | 30025 |
Visit :
Discussion