WHERE Conditions

The WHERE conditions filter the output of the FROM clause and restrict the returned rows in the result set. The conditions can refer to the data within the tables, expressions, built-in SQL Server scalar functions, other queries, or user-defined functions. The WHERE conditions can also use several possible comparison operators and wildcards, as listed in Table 6.1. In addition, you can combine multiple WHERE conditions using Boolean AND, OR, and NOT operators.

Table 6.1 Standard Comparison Operators

Description Operator Example
Equals = Quantity = 12
Greater than > Quantity > 12
Greater than or equal to >= Quantity >= 12
Less than < Quantity < 12
Less than or equal to <= Quantity<= 12
Not equal to <> , != Quantity <> 12 , Quantity != 12
Not less than !< Quantity !< 12
Not greater than !> Quantity !> 12
Best Practice
To improve the performance of a client/server database, let the database engine do the work of restricting the rows returned, rather than make the client application wade through unnecessary data.
The comparison operators that include an exclamation point are not ANSI standard SQL. <> is portable; != is not.

referenceaero In addition to the standard comparison operators, which are no doubt familiar, SQL provides four special comparison operators: BETWEEN, IN, LIKE, and IS. The first three are explained in this section. Testing ...

Get Microsoft SQL Server 2012 Bible now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.