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
Meaning It will return TRUE if both conditions are TRUE
it will Return TRUE if either of the statements or conditions return true
It will return TRUE if the following condition is FALSE.
Sample table used in this Logical operators in SQL. tblsampleemployee
Using AND Operator
SELECT `EMPNAME`, `SALARY` FROM `tblsampleemployee` WHERE `EMPNAME` = 'Kobe Bryant' AND `SALARY`=50000
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
Using the OR Operator
Take Note: When using OR operator, it requires either condition to be true.
SELECT `EMPNAME`, `SALARY` FROM `tblsampleemployee` WHERE `EMPNAME` = 'Kobe Bryant' OR `SALARY`> 50000
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
Using the OR Operator
SELECT `EMPNAME`,`SALARY`,`DEPT_NAME` FROM `tblsampleemployee` WHERE `DEPT_NAME` NOT IN ('IT','SALES')
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
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
If you have any questions about Logical Operator in SQL, please leave a comment below.