Friday 3 April 2015

SQL Server WHERE Clause

WHERE : Specifies the search condition for the rows returned by the query.

Operators in The WHERE Clause :

Operator
Description
=
Equal
<> 
Not equal. Note: In some versions of SQL this operator may be written as !=
> 
Greater than
< 
Less than
>=
Greater than or equal
<=
Less than or equal
BETWEEN
Between an inclusive range
LIKE
Search for a pattern
IN
To specify multiple possible values for a column

Examples : The following examples show how to use some common search conditions in the where clause.

Finding a row by using a simple equality
SELECT ProductID, Name FROM Production.Product WHERE Name = 'Blade' ;

Finding a row by using a simple equality
SELECT ProductID, Name, Color FROM Production.Product WHERE Name LIKE ('%Frame%');

Finding rows by using a comparison operator
SELECT ProductID, Name FROM Production.Product WHERE ProductID <= 12 ;

Finding rows that meet any of three conditions
SELECT ProductID, Name FROM Production.Product WHERE ProductID = 2
OR ProductID = 4 OR Name = 'Spokes' ;

Finding rows that must meet several conditions
SELECT ProductID, Name, Color FROM Production.Product WHERE Name LIKE ('%Frame%')AND Name LIKE ('HL%')AND Color = 'Red' ;

Finding rows that are in a list of values
SELECT ProductID, Name, Color FROM Production.Product
WHERE Name IN ('Blade', 'Crown Race', 'Spokes');

Finding rows that have a value between two values
SELECT ProductID, Name, Color FROM Production.Product WHERE ProductID BETWEEN 725 AND 734;

No comments:

Post a Comment