SQL Server – SELECT Statement multiple WHERE conditions

SELECT Statement multiple WHERE conditions

There is always a need for filtering the result of a query based on multiple conditions. In such cases we use multiple WHERE conditions within the WHERE clause uses the Boolean logical operators: AND, OR and NOT.

The precedence for Boolean operators is – AND comes first, the OR and then NOT.  It is good practice to use parenthesis if using multiple Boolean operators to avoid confusion.

Example:

USE AdventureWorks

— To display all the records whose city is ‘Bothell’ and id is greater then 50

SELECT * FROM Person.Address WHERE City=‘Bothell’ AND AddressID > 50

— To display all the records whose city is ‘Bothell’ or ‘Seatlle’

SELECT * FROM Person.Address WHERE City=‘Bothell’ OR City=‘Seattle’

See also:

DML SELECT Statement

SELECT Statement with WHERE clause

SELECT Statement with WHERE BETWEEN clause

SELECT Statement with WHERE IN clause

SELECT Statement with WHERE LIKE clause

Good Luck!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: