PostgreSQL BETWEEN
To filter the results, the PostgreSQL BETWEEN condition is used with SELECT, INSERT, UPDATE and DELETE statements to get values within a specific range from an expression.
Syntax:
WHERE expression BETWEEN value1 AND value2;
Parameters:
expression: It is used to specify the column or field.
value1, value2: They are used to specify the range for the values.
Example:
Students table:
ID | NAME | AGE |
1 | Joy | 5 |
2 | Smiley | 13 |
3 | Happy | 11 |
4 | Tom | 15 |
5 | Jerry | 10 |
6 | Bruno | 6 |
7 | David | 8 |
Query:
SELECT “NAME”, “AGE” FROM “STUDENTS” WHERE “ID” BETWEEN 2 AND 6; |
Output:
NAME | AGE |
Smiley | 13 |
Happy | 11 |
Tom | 15 |
Jerry | 10 |
Bruno | 6 |