Logical Operators In SQL [ AND, OR, NOT ]

Logical Operators In SQL [ AND, OR, NOT ]

This article will discuss and give more example about Logical Operators in SQL [AND, OR, NOT].

This SQL SELECT Statement tutorial will focus more on using logical conditions. The Logical conditions combines the result of two component conditions to produce single result based on them or inverts the results of a single condition.

Take note: It will only returned a row when the overall result of the condition is true.

Three Logical Conditions are:

Logical operators in SQL AND, OR, and NOT

  1. AND

    Meaning It will return TRUE if both conditions are TRUE

  2. OR

    it will Return TRUE if either of the statements or conditions return true

  3. NOT

    It will return TRUE if the following condition is FALSE.

Sample Table

Sample table used in this Logical operators in SQL. tblsampleemployee

EMP_IDEMPNAMECONTACTSALARYDEPT_NAME
1Kobe Bryant789541250000SHIPPING
2Jordan Michael789541270000IT
3James BOnd123245645000SALES
4Anne Curtis787889835000SHIPPING
5John Wick1233455765000EXECUTIVE
6Hatch Glets6654878257000IT
7Xyre Anderson7877892348000SALES

Using AND Operator

Take Note: When using AND operator, it requires both condition to be true.

Sample Query:

SELECT `EMPNAME`, `SALARY` 
FROM `tblsampleemployee` 
WHERE `EMPNAME` = 'Kobe Bryant' 
AND `SALARY`=50000 

Output:

EMPNAMESALARY
Kobe Bryant50000

Explanation:

In the example, both conditions must be true for any record to be selected. Therefore only employee named “Kobe Bryant” that match in our conditions. Which the employee name is Kobe Bryant and Salary of 50000.

Take note: All characters are case sensitive. it means it will not returned row if ‘EMPNAME’ is “kobe bryant”. The K and B should be in uppercase.

Here’s the AND TRUTH TABLE

ANDTRUEFALSENULL
TRUETRUEFALSENULL
FALSEFALSEFALSEFALSE
NULLNULLFALSENULL

Using the OR Operator

Take Note: When using OR operator, it requires either condition to be true.

Sample Query:

SELECT `EMPNAME`, `SALARY` 
FROM `tblsampleemployee` 
WHERE `EMPNAME` = 'Kobe Bryant' 
OR `SALARY`> 50000

Output:

EMPNAMESALARY
Kobe Bryant50000
Jordan Michael70000
John Wick65000
Hatch Glets57000

Explanation:

In the example, either conditions can be true for any record to be selected. Therefore only employee named “Kobe Bryant” and those employee has a salary greater than 50000 is selected.

Here’s the OR TRUTH TABLE

ANDTRUEFALSENULL
TRUETRUETRUETRUE
FALSETRUEFALSENULL
NULLTRUENULLNULL

Using the OR Operator

Sample Query:

SELECT `EMPNAME`,`SALARY`,`DEPT_NAME` 
FROM `tblsampleemployee` 
WHERE `DEPT_NAME` NOT IN ('IT','SALES') 

Output:

EMPNAMESALARYDEPT_NAME
Kobe Bryant50000SHIPPING
Anne Curtis35000SHIPPING
John Wick65000EXECUTIVE

Explanation:

In the example above displays the Employee name, Salary, and Department Name whose department is not IT and Sales.

Here’s the NOT TRUTH TABLE

NOTTRUEFALSENULL
FALSETRUENULL

Video demonstration below

Watch the Video here:

Related Article you may like

For Inquiries

If you have any questions about Logical Operator in SQL, please leave a comment below.

2 thoughts on “Logical Operators In SQL [ AND, OR, NOT ]”

Leave a Comment