Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL BETWEEN Operator

In PostgreSQL, the BETWEEN operator is used with the WHERE clause of a query to match a column value or an expression against a list of values. For example, get the data from a table where the value in the column falls between x and y.

The BETWEEN operator is used with the numeric and date columns only.

Syntax:
column BETWEEN <low_value> AND <high_value>

If the value is greater than or equal to low_value and less than or equal to high_value then the expression evaluates to be true, otherwise expression evaluates to be false. The BETWEEN operator always includes both the lower range and higher range while evaluating the expression.

The above is same as below, without using the BETWEEN operator.

column &gt;= <low_value> AND column &lt;= <high_value>

Let's use the following Employee table to understand the BETWEEN operator.

Sample Table

Now, let's get all employees whose salary is between 20000 and 50000. For this, we can use the BETWEEN operator with low and high values, as shown below:

Example: BETWEEN Operator
SELECT * FROM Employee 
WHERE salary BETWEEN 20000 AND 50000;

Above you can see, emp_id 1 has a salary of 20000, which is lower range value in the BETWEEN operator, is also considered while evaluating BETWEEN operator.

The BETWEEN operator can be used for specifying the date range in the query. The following fetches employees whose hire_date is between 1st Jan 2010 and 31st Dec 2015.

Example: BETWEEN Operator
SELECT * FROM Employee 
WHERE hire_date BETWEEN DATE '2010-01-01' and '2015-12-31';

Note that the null value is not considered when filtering data with BETWEEN operator.

The BETWEEN operator can also be written using greater than or equal (>=) and less than or equal (<=) operators, as shown bellow.

Example: Using >= AND <=
SELECT * FROM Employee 
WHERE salary &gt;= 20000 AND salary &lt;= 50000;

NOT BETWEEN

The NOT operator can be used with BETWEEN operator to validate the expression to check values outside the range.

The above is the same as below query using OR operator:

Note: The between clause cannot be used with string columns/values.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.