(2 row(s) affected)
But wait! How do I know the name of the department with the ID of 6? Well,
you could use a similar query to find out. Try this:
SELECT Department
FROM Departments
WHERE DepartmentID = 6
Executing this query reveals that the department with the ID of 6 is Engineering.
Department
--------------------------------------------------
Engineering
(1 row(s) affected)
Selecting Ranges of Values with BETWEEN
There may be times when youll want to search within a database table for rows
that fall within a certain range of values. For instance, if you wanted to retrieve
from the Departments table all departments that have IDs between 2 and 5, you
could use the BETWEEN keyword like so:
SELECT DepartmentID, Department
FROM Departments
WHERE DepartmentID BETWEEN 2 AND 5
As we requested, all departments whose IDs are between 2 and 5 are returned.
Note that the range is inclusive, so departments with IDs of 2 and 5 will also be
retrieved.
Note that any conditions that use BETWEEN could be easily rewritten by combining
two greater than or equal and less than or equal conditions:
SELECT DepartmentID, Department
FROM Departments
WHERE DepartmentID >= 2 AND DepartmentID <= 5
We could also use the NOT keyword before the BETWEEN keyword to specify all
items that fall outside the range, as follows:
303
Selecting Ranges of Values with BETWEEN

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.