Open In App

SQL Server AND Operator

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Logical operators are used for combining multiple boolean expressions which are combinations of results of multiple conditions which are formed by the comparators. Some of the logical operators are AND, OR, EXISTS, IN, LIKE, BETWEEN, etc, Logical operators are frequently used and very handy for testing multiple conditions. It is also used for combining multiple boolean expressions and it returns true only when all the conditions are true. It is used with the WHERE clause by specifying multiple boolean expressions that return any one of the following values True, False, or Unknown. In this article, you will get a clear understanding of the AND Operator.

AND Operator Table

A

B

A AND B

True

True

True

True

False

False

False

True

False

Explanation: This table specifies that you will get the True as a result when both conditions are executed as correct, otherwise it will give you False even one of two conditions is True. No worries we will help you to understand about AND Operator using the below examples.

Syntax:

SELECT * FROM table_name 
WHERE condition1 AND condition2 AND

Explanation: condition1 and condition2 are conditions that return boolean values on evaluation.

How to Use the AND Operator Step by Step

For Understanding the AND Operator in SQL Server we need a table for performing operations or queries. So here we have a CoursesActive table. If you don’t know how to create table in SQL Server then you can refer to this for How to Create a Table in SQL Server.

The Result Looks Like:

CoursesActiveTable

CoursesActive Table

Example Using AND Operator

Let’s say we have to retrieve the course names from the “CoursesActive” table where the course cost is greater than 2000 and the number of students enrolled exceeds 200,000.

SELECT courseName FROM CoursesActive
WHERE courseCost>2000 AND studentsEnrolled>200000


The Result Looks Like:

SingleANDOptr

After Query Run Our Table Looks

Explanation : In this query we have checked the two conditions one with the courseCost>2000 and the other is studentsEnrolled>200000 the courses with both the conditions true are only 3 records.

Using AND Operator with Aggregative Function

Let’s say we have to fetch all details from the “CoursesActive” table where the course cost exceeds 1000 and the course name contains the term ‘java‘ (case-insensitive).

SELECT * FROM CoursesActive
WHERE courseCost>1000 AND LOWER(courseName) LIKE '%java%';


The Result Looks Like:

ANDwithAggregate

After Query Run Our Table Looks

Explanation: In this query we have used two condition with the AND operator one of the condition is courseCost>1000 and the other is courseName consisting of the name java, we have used the LIKE operator to check if there exists java in the courseName the rows with the both the conditions true are only these two in the result.

Using AND Operator With Other Logical Operator

Let’s say we have to retrieve the rows from the “CoursesActive” table where the courseId is greater than 1006 and the courses whose courseCost is greater than 2000 or the courses in which there are more than 1,00,000 students enrolled.

  SELECT * FROM CoursesActive
WHERE courseId>1005 AND (courseCost>2000 OR studentsEnrolled> 100000)


The result looks like:

ANDwithOtherLogical

After Query Run Our Table Looks

Explanation: In this query we have used both AND and OR logical operators where the first condition is courseId> 1005 and the second operand is logical expression with OR and returns true when either of two conditions courseCost > 2000 or the studentsEnrolled <100000 is true.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads