Monday, August 29, 2011

SQL BETEEN CLAUSE


The SQL BETWEEN & AND keywords define a range of data between 2 values.
The SQL BETWEEN syntax looks like this:

SELECT Column1, Column2, Column3, … FROM Table1
WHERE Column1 BETWEEN Value1 AND Value2

The 2 values defining the range for SQL BETWEEN clause can be dates, numbers or just text.
In contrast with the SQL IN keyword, which allows you to specify discrete values in your SQL WHERE criteria, the SQL BETWEEN gives you the ability to specify a range in your search criteria.
 
We are going to use the familiar Company table to show how SQL BETWEEN works:

FirstName
LastName
Email
DOB
Phone
Scott
Armstrong
Armstrong.yahoo.com
2/4/1968
887-676-2321
Adam
Red
red@himail.com
4/4/1978
792 343-2422
Santo
Harry
Harry@livingin.org
5/24/1978
416 323-3232
Jackson
Jimmy
Jimmy@supermail.co.uk
20/10/1980
416 323-8888

Consider the following SQL BETWEEN statement:

SELECT *
FROM Company
WHERE DOB BETWEEN '1/1/1965' AND '1/1/1979'

FirstName
LastName
Email
DOB
Phone
Scott
Armstrong
Armstrong.yahoo.com
2/4/1968
887-676-2321
Adam
Red
red@himail.com
4/4/1978
792 343-2422
Santo
Harry
Harry@livingin.org
5/24/1978
416 323-3232


The SQL BETWEEN statement above will select all Company having DOB column between '1/1/1965' and '1/1/2079' dates. Here is the result of this SQL expression:

MORE BASIC SQL COMMANDS

No comments:

Post a Comment