Home SQL Between
E-mail Print PDF

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

1
2
3
4
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2

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:

1
2
3
SELECT * FROM Persons
WHERE LastName
BETWEEN 'Hansen' AND 'Pettersen'

Query Result:

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes

Note - 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.

Note 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".

1
2
3
SELECT * FROM Persons
WHERE LastName
NOT BETWEEN 'Hansen' AND 'Pettersen'

Query Result:

P_Id LastName FirstName Address City
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

Comments

 
+5 #1 Александр Викторович 2010-08-03 18:55 In the first example, between Hansen and Svendsen Patterson is Quote
 
 
+2 #2 Алексей Леонидович 2010-08-16 17:49
In the first example, between Hansen and Svendsen Patterson is

Example - for those who know the alphabet. S comes after P and after H, ie, not between H and P.
Quote
 
 
+5 #3 Шелест Константин 2010-10-28 18:17 A little crooked, and all thanks! Quote
 
 
+2 #4 Йопер 2010-12-28 16:20
In the first example, between Hansen and Svendsen Patterson is

Example - for those who know the alphabet. S comes after P and after H, ie, not between H and P.


Then the paper is inadequate!
Quote
 
 
+1 #5 Lektra 2011-01-26 19:10 Article adequate, it was necessary to read comments after the example carefully. There are written, that conclusion will depend on the database (in different versions of the databases, a different answer). Quote
 
 
+1 #6 SL 2011-06-06 11:28 Note sample will be in alphabetical order.
question about the sample, so that everything is fine.
Quote
 

Authorization

Nice Ajax Poll

Which one of my extensions is the best?

Statistics

Translate

русскийitalianoDeutschEnglishLATVIANукраїнськаfrançaispolski
1

Advertisement