AND, OR and NOT Conditions in Where clause SQL SERVER

We can combined AND, OR, and NOT Conditions in where clause. And and OR Condition are used to retrieve data using multiple conditions
·         And Condition shows data if condition is divide by and in where clause
  • OR condition retrieve rows if conditions are divided by OR
  • Not Condition shows a data if condition is not true

AND Query

SELECT * FROM your_table_name WHERE condition_1 AND condition _2 AND condition_3;

OR Query

SELECT * FROM your_table_name 
WHERE condition_1 OR condition_2 OR condition_3;

NOT Syntax

SELECT * FROM your_table_name WHERE NOT condition;


Well this was the syntax now will show you some examples of And, OR and NOT

Example AND Condition

The Following query returns all rows where “FirstName” is “Nancy” and “LastName” is “Davolio”
  Select * From [Employees] where FirstName='Nancy' and LastName='Davolio'; 

Output

Example OR Condition

Following query returns all rows where TitleOfCourtesy is 'Mr.' or 'Ms.'

Select * From [Employees] where TitleOfCourtesy='Mr.' or TitleOfCourtesy='Ms.';

Output



    NOT Example


Following query return rows where Title is not 'Sales Manager’

Select * From [Employees] where not Title= 'Sales Manager' ;

 Output


Combining AND, OR and NOT Conditions

We can combine the AND, OR and NOT conditions.
Following SQL query selects rows where city is "London" AND Title must be "'Sales Manager " OR "'Sales Representative”

Select * From [Employees] where  city= 'London' and (Title ='Sales Manager' or Title='Sales Representative' );


Output

Comments

Popular posts from this blog

Disable right click, Inspect Element and page source using JavaScript in Html and asp.net

Like Operator In Sql Server