Introduction to SQL Server AND operator

Introduction to SQL Server AND operator

The AND is a logical operator that allows you to combine two Boolean expressions. It returns TRUE only when both expressions evaluate to TRUE.
The following illustrates the syntax of the AND operator:
The boolean_expression is any valid Boolean expression that evaluates to TRUEFALSE, and UNKNOWN.
The following table shows the result when you combine TRUEFALSE, and UNKNOWN values using the AND operator:
TRUEFALSEUNKNOWN
TRUETRUEFALSEUNKNOWN
FALSEFALSEFALSEFALSE
UNKNOWNUNKNOWNFALSEUNKNOWN
When you use more than one logical operator in an expression, SQL Server always evaluates the AND operators first. However, you can change the order of evaluation by using parentheses.

SQL Server AND operator examples

See the following products table in the sample database:

A) Using AND operator example

The following example finds the products where the category identification number is one and the list price is greater than 400:
Here is the result:
SQL Server AND operator example

B) Using multiple AND operators example

The following statement finds the products that meet all the following conditions: category id is 1, the list price is greater than 400, and the brand id is 1:
The result is as follows:
SQL Server AND multiple operators example

C) Using the AND operator with other logical operators

See the following query example:
The following shows the result:
SQL Server AND and OR operators example
In this example, we used both OR and AND operators in the condition. As always, SQL Server evaluated the AND operator first. Therefore, the query retrieved the products whose brand id is two and list price is greater than 1,000 or the products whose brand id is one.
To get the product whose brand id is one or two and list price is larger than 1,000, you use parentheses as follows:
The query returned the following result:
SQL Server AND and OR operators with parentheses
In this tutorial, you have learned how to use the SQL Server AND operator to combine two Boolean expressions.

Post a Comment

0 Comments