What is Logical AND Operator in SQL Server - SQL Server / TSQL Tutorial Part 120

Logical Operators are used to test some conditions. If the condition is true, the row will be selected for output.


What is AND Logical Operator : 

Logical AND operator is used when we want to return the row if all the specified conditions are true.

Scenario : 

Think about a situation where you need to return all the records from a dbo.Customer table where FName='Raza' and CountryShortName='US'

Create the dbo.Customer table with some sample records by using below scripts.

Create table dbo.Customer
 (Id int,
  FName VARCHAR(50),
  LName VARCHAR(50),
  CountryShortName CHAR(2))
GO
insert into dbo.Customer
Values (
1,'Raza','M','PK'),
(2,'Rita','John','US'),
(3,'Sukhi','Singh',Null),
(4,'James','Smith','CA'),
(5,'Robert','Ladson','US'),
(6,'Alice','John','US'),
(7,'Raza','M','US')


To return the records where FName='Raza' and CountryShortName='US' we can use below query.

Select * From dbo.Customer
where FName='Raza'
AND CountryShortName='US'


Noticed that our query returned single record as both conditions are only true for single record.

No comments:

Post a Comment