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 the WHERE clause, and syntax for AND operator in SQL will be like as following statements.

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 create, and the result is like below.

 

Newly created employeedetails table in sql server

 

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

SQL AND Operator Example1

In the following SQL query, we check multiple conditions (Location, Salary) with AND operator. It will return records that satisfy both conditions.

 

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

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

 

SQL AND Operator Example Output

SQL Server AND Operator Example2

In the following SQL query, we check Location and Empname conditions with AND operator. It will return records that satisfy the defined conditions.

 

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

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

 

SQL Server AND Operator Example with Output