BETWEEN - This statement is used in the WHERE clause to select data between two values. These may include: test, numbers and dates.
Syntax SQL BETWEEN
Example 1
There is a table "Persons":
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
| 3 | Pettersen | Kari | Storgt 20 | Stavanger |
So, now we want to select all persons whose names are located between "Hansen" and "Pettersen" from the table above. Note that sampling will occur in alphabetical order.
For this we use this query:
Query Result:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
- Note: The BETWEEN operator works differently in different databases.
In some databases, the person with the names of "Hansen" and "Pettersen" will not be displayed when receiving the result, because the BETWEEN operator returns only the values between those in the query.
In some databases, the person with the names of "Hansen" and "Pettersen" will be displayed when receiving the result.
In other databases, the person with the name "Hansen" will be on the list, but "Pettersen" will not (as in the example above), because the operator chooses between two fields, but the result shows only the first specified value without the other.
Tip: Before using the BETWEEN operator in the database, you need to check how the database responds to the requests of this type.
Example 2
So, now we want to select all persons whose names are not located between "Hansen" and "Pettersen" from table "Persons".
Query Result:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
| 3 | Pettersen | Kari | Storgt 20 | Stavanger |
SQL Between





Comments
Example - for those who know the alphabet. S comes after P and after H, ie, not between H and P. Quote
Then the paper is inadequate! Quote
question about the sample, so that everything is fine. Quote