Home » SQL Server – AND Operator

SQL Server – AND Operator

  • by

In this article, we will discuss the AND operator in SQL Server. AND is the logical operator used to combine multiple Boolean expressions.

Syntex:

expression1 AND expression2

AND the logical operator returns true when two expressions value is true

AND Operator examples

Will take Employee table for following examples,

Column_name
EmployeeID
Employee Name
Salary
DateOfBirth
JoinYear

Using AND Operator

The following query will return the employee records having a Salary greater than 10000 and joined after 2015(JoinYear).

SELECT  [EmployeeID]
      ,[Employee Name]
      ,[Salary]
      ,[DateOfBirth]
      ,[JoinYear]
  FROM [Blog].[dbo].[Employee] where Salary>10000 and JoinYear>2015

Output:

Using AND Operator

Using Multiple AND Operator

In this example, we can use multiple <strong>AND</strong> operators in the query. The following query will return the employee records having a Salary greater than 10000, joined after 2015(JoinYear) and <strong>DateOfBirth</strong> is before 1996-02-28.

SELECT  [EmployeeID]
      ,[Employee Name]
      ,[Salary]
      ,[DateOfBirth]
      ,[JoinYear]
FROM [Blog].[dbo].[Employee] 
where Salary>10000 and JoinYear>2015 and DateOfBirth<'1996-02-28'

Output:

Using Multiple AND Operator

Using AND Operator with other logical operators

In the below query, we have used AND and OR logical operators, In the condition, if Salary is 10000 or 20000 anyone is returned true then the expression will return true.

SELECT  [EmployeeID]
      ,[Employee Name]
      ,[Salary]
      ,[DateOfBirth]
      ,[JoinYear]
  FROM [Blog].[dbo].[Employee] 
  where Salary=10000 or Salary=20000
  and DateOfBirth<'1996-02-28'

Output:

Using AND Operator with other logical operators

Need help?

Read this post again, if you have any confusion or else add your questions in Community

Tags: