SQL AND Operator

In SQL, AND operator is used to compare data with more than one condition and it will return records only when all the defined conditions are TRUE.

 

Generally, we will use AND operator in WHERE clause and syntax for AND operator in SQL will be like as shown below.

SQL AND Operator Syntax

Following is the syntax of defining an AND operator in SQL statements.

 

SELECT column1, column2 FROM tablename WHERE column1 ='somevalue' AND column2='Somevalue'

We will check this with the example for that first create “EmployeeDetails” table by using the following script in the SQL database.

 

create table EmployeeDetails(empid int, empname varchar(50),designation varchar(50),salary int,Location varchar(50))

 

insert into EmployeeDetails

values(1,'suresh','software engineer',25000,'chennai'),

(2,'rohini','AEO',15000,'chennai'),

(3,'madhavsai','business analyst',50000,'nagpur'),

(4,'mahendra','CA',75000,'guntur'),

(5,'sateesh','Doctor',65000,'guntur')

 

select * from EmployeeDetails

When we run the above SQL script, the “EmployeeDetails” table will be created and the result will be as shown below.

 

Newly created employeedetails table in sql server

 

Now run following examples to check AND operator in SQL server.

SQL AND Operator Example1

In the following SQL query, we are checking multiple conditions (Location, Salary) with AND operator. It will return records whatever it satisfies both the conditions.

 

SELECT * FROM EmployeeDetails WHERE Location='guntur' AND Salary>40000

When we execute the above SQL query, we will get the result as shown below.

 

SQL AND Operator Example Output

SQL Server AND Operator Example2

In the following SQL query, we are checking Location and Empname conditions with AND operator. It will return records whatever it satisfies the defined conditions.

 

SELECT * FROM EmployeeDetails WHERE Location='chennai' AND Empname='suresh'

When we execute the above SQL query, we will get the result as shown below.

 

SQL Server AND Operator Example with Output