AND, OR and NOT Keyword in MS SQL Server


AND, OR and NOT keyword help to extend the filters on respected table along with Where clause

Let consider we have the shopping table, and we need to select data from table.

and or not condition in sql server

Use AND keyword / statement in MS SQL Server to select table rows


Select the product from Shopping table where price is less than or equal to 25000 and dealer country is India.

Example

SELECT * FROM tblShoppingList
WHERE Price <= 25000 AND DealerCountry = 'India'

Output

and or not condition in sql server


Can we use AND keyword multiple time with select / where statement?


Answer is : Yes, We can use AND keyword multiple time with select / where statement.

Select the product from Shopping table where price is less than or equal to 25000 , dealer country is India and dealer city is Mumbai.

Example

SELECT * FROM tblShoppingList
WHERE Price <= 25000 AND DealerCountry = 'India' AND DealerCity = 'Mumbai'

Output

and or not condition in sql server


Note : When AND keyword use in SQL Select statement then assign value must match to columns value else it will not select row.
As many AND we increase that much filter we apply on select statement.



OR keyword / statement in MS SQL Server with Where condition.


Select the product from Shopping table where price is less than or equal to 25000 , dealer country is India and dealer city is Mumbai.

Example

SELECT * FROM tblShoppingList
WHERE Price <= 25000 AND DealerCountry = 'India' or DealerCountry = 'USA'

Output

and or not condition in sql server


Second example of OR condition, select the record from table where price is less than equal to 25000 and country is USA. USA is available in table, then also it will give the result base on first condition.

Example

SELECT * FROM tblShoppingList
WHERE Price <= 25000 OR DealerCountry = 'USA'

Output

and or not condition in sql server


Note : What is difference between AND and OR keyword in SQL Server?
AND means respected value must available in table else it will return zero row. When we use OR keyword it means we are trying to extra alternative condition, In OR value gives the result depending on first condition.


NOT keyword / statement in MS SQL Server with Where condition.


We can use NOT keyword alone, or we can use NOT keyword with AND keyword.

Below is example where we use the NOT keyword with AND keyword to extent the WHERE condition.

Example

SELECT * FROM tblShoppingList
WHERE Price <= 25000 OR DealerCountry = 'USA'

Output

and or not condition in sql server


Note : In above example list the all product having price is less than 100000 and country not equal to 'USA'.