SQL AND, OR and NOT

SQL AND, OR and NOT Operators are used to get selected data from the SQL TABLE.

SQL "AND" Operator

The AND operator is used to filter data according to TRUE all the conditions.


SELECT * FROM table-name WHERE 

`column1`='value1' 
AND `column2`='value2'
AND `column3`='value3'
AND `column4`='value4'

SQL "OR" Operator

The OR operator is used to filter data according to TRUE any one condition.


SELECT * FROM table-name WHERE 

`column1`='value1' 
OR `column2`='value2'
OR `column3`='value3'
OR `column4`='value4'

SQL "NOT" Operator

The NOT operator is used to filter data according to FALSE conditions.


SELECT * FROM table-name WHERE 

NOT `column1`='value1'

SELECT * FROM table-name WHERE 
NOT `column1`='value1'
AND NOT `column2`='value2'

SQL "AND", "OR" and "NOT" how to use in one query


SELECT * FROM table-name 

WHERE (`column1`='value1' AND `column2`='value2') 
OR (`column3`='value3' AND `column4`='value4')

SELECT * FROM table-name 
WHERE (`column1`='value1' OR `column2`='value2') 
AND (`column3`='value3' OR `column4`='value4')

SELECT * FROM table-name 
WHERE `column1`='value1' 
AND (`column2`='value2' OR `column3`='value3')

SELECT * FROM table-name WHERE 
NOT `column1`='value1'
AND NOT `column2`='value2'

SELECT * FROM table-name WHERE 
NOT `column1`='value1'
OR NOT `column2`='value2'

Share