Logo 
Search:

SQL Server Articles

Submit Article
Home » Articles » SQL Server » Operator RSS Feeds

AND Logical Operator

Posted By: Sarita Patel     Category: SQL Server     Views: 2288

This article explains logical operator AND with examples in sql server.

And combines two boolean expressions and returns TRUE when both expressions are TRUE. When more than one logical operator is used in a statement,  AND operators are evaluated first. You can change the order of evaluation by using parentheses.

Below table explains the return value when you compare TRUE, FALSE and UNKNOWN values using the AND operator.

TRUE

FALSE

UNKNOWN

TRUE

TRUE

FALSE

UNKNOWN

FALSE

FALSE

FALSE

FALSE

UNKNOWN

UNKNOWN

FALSE

UNKNOWN

 



Syntax of Logical Operator AND :

boolean_expression AND boolean_expression

boolean_expression is valid sql server expression that returns a boolean value: TRUE, FALSE, or UNKNOWN.

Return type of AND operator is boolean. It returns TRUE when both expressions are TRUE. 




Examples of Logical Operator AND :


Example 1 : Using AND operator in a where clause

SELECT ProductName, UnitPrice

FROM   Products

WHERE  UnitPrice > 15 AND ProductName LIKE 'c%'


Output

ProductName                                UnitPrice

Chai                                                18.00

Chang                                            19.00

Chef Anton's Cajun Seasoning       22.00

Chef Anton's Gumbo Mix              21.35

Carnarvon Tigers                           62.50

Côte de Blaye                                 263.50

Chartreuse verte                             18.00

Camembert Pierrot                         34.00

 

Above query shows the use of and operator in where clause where it returns records for the product whose name starts with character 'c' and with a unit price greater than $15.




Example 2 : Using AND operator in a if statement

DECLARE @Number1 INT, @Number2 INT, @Number3 INT

SET @Number1 = 21

SET @Number2 = 21

SET @Number3 = 30

 

IF (@Number1 = @Number2 AND @Number1 < @Number3)

BEGIN

            PRINT 'Number3 has gretest value among the three numbers.'

END

ELSE IF (@Number1 = @Number3 AND @Number1 < @Number2)

BEGIN

            PRINT 'Number2 has gretest value among the three numbers.'

END

ELSE

BEGIN

            PRINT 'Number1 has gretest value among the three numbers.'

END


Output

Number3 has gretest value among the three numbers.

Above example explains the use of and operator in if statement where numbers are compared to display the highest number among the three. We can get the highest number by comparing two expression using and operator.

  
Share: 

 
 

Didn't find what you were looking for? Find more on AND Logical Operator Or get search suggestion and latest updates.

Sarita Patel
Sarita Patel author of AND Logical Operator is from United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!