SQL AND Operator

In sql, AND operator is used to compare a 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 a AND operator in sql statements.

 

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

We will check this with example for that first create “EmployeeDetails” table by using following script in 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

Once we run above sql script our table “EmployeeDetails” will created and result will be like 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 following SQL query we are checking multiple conditions (Location, Salary) with AND operator. It will return records whatever it satisfy both the conditions.

 

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

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

 

SQL AND Operator Example Output

SQL Server AND Operator Example2

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

 

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

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

 

SQL Server AND Operator Example with Output