In this SQL Server tutorial, you’ll learn how to use AND operator in the T-SQL query in SQL Server along with some examples.
What is AND Operator in SQL Server?
The AND operator in SQL Server is one of the logical operator that is mainly used to test for two or more conditions. It mainly combines two or more Boolean expressions and returns true only if all the expressions evaluate to TRUE in the query.
WHERE expression1 AND expression2 ... AND expressionn;
- These are the individual conditions and it is any valid expressions that evaluates to either true, false or unknown.
It is important to note that the AND operator is mainly used to evaluate two or more expressions and all of the conditions must return true for the record to be included in the result set.
Also, when there are more than one logical operator used in an expression, the AND operator always takes the precedence and evaluates first. The precedence can be changed by using the parenthesis.
Example 1 : Using AND operator to evaluate two conditions
The below SQL query find all the Person records where the title is Mr. and EmailPromotion is set.
SELECT * FROM [Person].[Person] WHERE Title = 'Mr.' AND EmailPromotion = 1
Example 2 : Using Multiple AND Operators in SQL
The below query returns all the Person records that meets all the conditions as specified below
- Title = “Mr.”
- EmailPromotion = 1
- PersonType = “SP”
SELECT * FROM [Person].[Person] WHERE Title = 'Mr.' AND EmailPromotion = 1 AND PersonType = 'SP'