Pages

Tuesday 2 August 2011

Logical Operators in SQL Server.

0 comments
 
Tags : Logical Operators,Sql Server 2005,Sql Server 2008,Sql Server Operators.

Hi Friends,In this article i would like to explain Logical Operators in SQL Server.

* Logical operators, like comparison operators, return a Boolean data type with a value of TRUE, FALSE, or UNKNOWN.

* Available Logical Operators :


1) AND:

Performs a logical AND operation. The expression evaluates to TRUE if all conditions are TRUE.

Example :

SELECT * FROM Class
WHERE ( Marks > 40 AND Marks < 100)


2) OR :

Performs a logical OR operation. The expression evaluates to TRUE if atleast one condition is TRUE.

Example :

SELECT * FROM Class
WHERE EmployeeName LIKE 'K%' OR Marks > 40


3) BETWEEN :

It returns TRUE if the operand is within a range otherwise FALSE.

Example :

Select * from Employee
WHERE EmpSalary BETWEEN 5000 AND 15000


4) IN :

This Operator returns TRUE if the operand is equal to one of a list of expressions Otherwise flase.

Example :

SELECT * FROM Students
WHERE StudentId IN(SELECT StudentId FROM CSEDEPT WHERE CSEDeptID=20)


5) EXISTS :

Specifies a subquery to test for the existence of rows.

Example :

SELECT * FROM Students WHERE EXISTS
(
SELECT * FROM Students WHERE CollegeCode='B2'
)


6) LIKE :

Determines whether a specific character string matches a specified pattern.

Example :

SELECT * FROM EnggColleges
WHERE CollegeName LIKE 'S%'

Here,it will return all the records which has 's' as first letter in CollegeName


7) ALL :

It Returns TRUE if all of a set of comparisons are TRUE other wise returns FALSE

Example :

SELECT * FROM Employee
WHERE EMPLOYEEID >= ALL (SELECT EMPLOYEEID from Salary WHERE Salary>5000 )


8) ANY :

ANY returns TRUE when the comparison specified is TRUE for ANY pair, otherwise, returns FALSE.

Example :

SELECT * FROM Colleges WHERE 20000 > ANY
(
SELECT CollegeFess FROM Colleges
)


9) Not :

NOT operator is used To find rows that do not match a value.

Example :

SELECT * FROM Colleges
WHERE CollegeCode NOT IN (50,100,150,200)


Thank You...
Shout it

Leave a Reply