Explain AND, OR & NOT Operator in SQL

To make WHERE Clause function, operators are used for the purpose so that one can fetch data from the database server.

In this we can focus only on logical operators, a logical operator in SQL is used for comparing to conditions or expressions so that a specific information can fetch out from a table. These logical operators are AND,OR and NOT.

AND Operator

When two or more than two expressions together are said to be true then only those rows would be fetch out that met AND condition.[All condition must be true]

For Example: Suppose you want to draw all the data whose first name starts with cooper and must be male from the table named ‘user_details’ .

SELECT * FROM user_details WHERE first_name=’cooper’AND gender=’male’

and

Explanation:
So, AND operator only grabs those rows from a table that satisfy both expressions as shown above it fetched only those data where the first name was cooper and gender was male as well.

OR Operator

When either of the condition is true then those rows will be fetched out from the table.[One of the condition must be true]

For Example:You require to obtain the data where either the first name could be cooper or gender could be male.

SELECT * FROM user_details WHERE first_name=’cooper’ OR gender=’male’

or

Explanation:
So, OR operator only grabs those rows from a table that satisfies one of the expression as shown above it fetched those data where either the first name was cooper or gender was male.

 

NOT Operator

When a condition or expression is not same as other one then this NOT operator let you to fetch those results.
For Example:You doesn’t want those data where gender is male and first name is cooper.

SELECT * FROM user_details WHERE NOT first_name=’cooper’ AND NOT gender=’male’

not
Explanation:
So, NOT operator only grabs those rows from a table that satisfies both the expression as shown above it fetched those data where the first name was not cooper and gender was not male as well.

Leave a Reply