SQL AND, OR & NOT Clause

The SQL AND, NOT & OR operators are used to combine multiple conditions to filter data in an SQL statement. These there operators are called as the conjunctive operators.

 

Example Database:

This is  customers  table.

id name Location
1 Kailash Delhi
2 Amit Haryana
3 Siddarth Noida
4 Vikas Noida

 


 

AND Operator

The AND operator displays a record if all the conditions separated by AND are TRUE.

Syntax:

SELECT * FROM table_name WHERE condition1 AND condition2;

 

Example:

SELECT * FROM customers WHERE id = 1 AND location = 'Delhi';

Result: 

id name Location
1 Kailash Delhi

 

OR Operator

The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause. It means OR operator displays a record if any of the conditions separated by OR is TRUE.

Syntax:

SELECT * FROM table_name WHERE condition1 OR condition2;

 

Example:

SELECT * FROM customers WHERE id = 1 AND location = 'Noida';

Result:

id name Location
1 Kailash Delhi
3 Siddarth Noida
4 Vikas Noida

 

NOT Operator

The NOT operator displays a record if the condition(s) is NOT TRUE.

Syntax:

SELECT * FROM table_name WHERE NOT condition;

 

Example:

SELECT * FROM customers WHERE NOT location='Noida';

Result: 

id name Location
1 Kailash Delhi
2 Amit Haryana