Filtering Records by Numbers

Where_Statement

In a previous post, we discussed how to write a select statement. Over the next several posts we will discuss how to use the WHERE keyword to filter records by numbers, character data, dates and times, and NULL values. This post specifically will focus on filtering records by integer numbers.

SELECT ProductID, Name, ProductNumber,
	Color, StandardCost, ListPrice
FROM Production.Product
WHERE ProductID = 2

In the code above, we are using the WHERE keyword to return the product that has a ProductID that = 2. We could also use the greater than (>), less than (<), as well as other equality operators. In this case, our SELECT statement returns the one record where ProductID equals two.

If we wanted to return more than one value, we could write our WHERE statement in one of two ways. Either by using the OR statement or by using the IN predicate.

--Searching for multiple values using the OR predicate
SELECT ProductID, Name, ProductNumber,
	Color, StandardCost, ListPrice
FROM Production.Product
WHERE ProductID = 2 OR ProductID = 4

--Searching for multiple values using the IN predicate
SELECT ProductID, Name, ProductNumber,
	Color, StandardCost, ListPrice
FROM Production.Product
WHERE ProductID IN (2, 4)

Both statements will run the same way and return the same data. If you are filtering by more than two values, the IN predicate might be more efficient from a typing standpoint.

Be the first to comment on "Filtering Records by Numbers"

Leave a comment

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.