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
- AND
Meaning It will return TRUE if both conditions are TRUE
- OR
it will Return TRUE if either of the statements or conditions return true
- NOT
It will return TRUE if the following condition is FALSE.
Sample Table
Sample table used in this Logical operators in SQL. tblsampleemployee
EMP_ID | EMPNAME | CONTACT | SALARY | DEPT_NAME |
1 | Kobe Bryant | 7895412 | 50000 | SHIPPING |
2 | Jordan Michael | 7895412 | 70000 | IT |
3 | James BOnd | 1232456 | 45000 | SALES |
4 | Anne Curtis | 7878898 | 35000 | SHIPPING |
5 | John Wick | 12334557 | 65000 | EXECUTIVE |
6 | Hatch Glets | 66548782 | 57000 | IT |
7 | Xyre Anderson | 78778923 | 48000 | SALES |
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:
EMPNAME | SALARY |
Kobe Bryant | 50000 |
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
AND | TRUE | FALSE | NULL |
TRUE | TRUE | FALSE | NULL |
FALSE | FALSE | FALSE | FALSE |
NULL | NULL | FALSE | NULL |
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:
EMPNAME | SALARY |
Kobe Bryant | 50000 |
Jordan Michael | 70000 |
John Wick | 65000 |
Hatch Glets | 57000 |
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
AND | TRUE | FALSE | NULL |
TRUE | TRUE | TRUE | TRUE |
FALSE | TRUE | FALSE | NULL |
NULL | TRUE | NULL | NULL |
Using the OR Operator
Sample Query:
SELECT `EMPNAME`,`SALARY`,`DEPT_NAME` FROM `tblsampleemployee` WHERE `DEPT_NAME` NOT IN ('IT','SALES')
Output:
EMPNAME | SALARY | DEPT_NAME |
Kobe Bryant | 50000 | SHIPPING |
Anne Curtis | 35000 | SHIPPING |
John Wick | 65000 | EXECUTIVE |
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
NOT | TRUE | FALSE | NULL |
FALSE | TRUE | NULL |
Video demonstration below
Watch the Video here:
Related Article you may like
- SQL SELECT Statement WHERE | Restricting and Sorting of Data
- Writing Basic SQL Select Statements With Actual Demonstration
For Inquiries
If you have any questions about Logical Operator in SQL, please leave a comment below.
Sir kwa na gamay ang video tutorial pro mas Mani ang may face to face
😇