0% found this document useful (0 votes)
8 views

Logical Condition

This module covers the use of logical operators AND, OR, and NOT in SQL queries to define conditions for selecting rows from a database. It explains how to apply these operators, the rules of precedence for evaluating conditions, and provides examples using the AUTHORS table. Students should be able to apply logical conditions and identify the order of precedence after completing the lesson.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Logical Condition

This module covers the use of logical operators AND, OR, and NOT in SQL queries to define conditions for selecting rows from a database. It explains how to apply these operators, the rules of precedence for evaluating conditions, and provides examples using the AUTHORS table. Students should be able to apply logical conditions and identify the order of precedence after completing the lesson.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MODULE OF INSTRUCTION

Defining Condition using Logical


Operators
Welcome to the 7th module of this course Database Management System
1! For this lesson, it covers the where condition with the additional
restriction which applies the logical condition AND, OR and NOT.
Sample and demonstration in this lesson uses the AUTHORS table as
shown below:
Table 1.0 Authors

The select statement is used to create different types of reports where the
Logical
user can apply the basic select, restrict the rows returned by the query,
Condition
use single row or multiple row functions and sort the data either
ascending or descending.

To combine the result of two statements or also known as the component


you may use logical conditions to produce a single result based on those
conditions or it inverts the result of a single condition. A row is returned

Database Management System 1 1


Week 3 Logical Condition

only if the overall result if the condition is true.

After completing this lesson, the student should be able to:


 Define the rows returned using the three logical condition AND, OR
and NOT.
 Identify the result when the order of precedence is applied in
WHERE clause.

Three types of Logical Condition


Operator Description
AND Returns TRUE if both component conditions are true

OR Returns TRUE if either component condition is true

NOT Returns TRUE if the condition is false

Using the AND Operator


 Both the component conditions must be true for any record to be
selected.
 Example:
SELECT ID, FNAME, LNAME, BOOK, TYPE
FROM AUTHORS
WHERE TYPE='CSIT'
AND BOOK LIKE '%A';
 Output:

 Explanation:

_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
As shown in the output only those authors who have a type of CSIT
and book that contains the last letter equal to A are selected.

Using the OR Operator


 Either component condition can be true for any record to be selected.
 Example:
SELECT ID, FNAME, LNAME, BOOK, TYPE
FROM AUTHORS
WHERE BOOK='CSIT'
OR BOOK LIKE '%A';
 Output:

 Explanation:
As shown in the output only those authors who have a type of CSIT
or either a book that contains the last letter equal to A are selected.

Using the NOT Operator


 Reverse the value from TRUE to FALSE or FALSE to TRUE.
 The NOT operator can also be used with other SQL operators, such as
BETWEEN, LIKE, and NULL.
 Example:
SELECT ID, FNAME, LNAME, TYPE,YR_PUB
FROM AUTHORS
WHERE YR_PUB NOT IN(2010, 1995);
 Output:

Database Management System 1 3


Week 3 Logical Condition

 Explanation:
As shown in the output only those authors who have a YR_PUB not
equal to the value inside the IN (SET) are selected.

Rules of Precedence
 The rules of precedence determine the order in which expressions are
evaluated and calculated.
 The table in the slide lists the default order of precedence.
To override the default order of expression to be calculated first put
parentheses around the expression.
Operator Meaning
1 Arithmetic operators
2 Concatenation operator
3 Comparison conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to
7 NOT logical condition
8 AND logical condition
9 OR logical condition

 Example:
SELECT (FNAME||','||LNAME)AS NAME, BOOK,

_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
TYPE,AC_NO
FROM AUTHORS
WHERE TYPE='CSIT'
OR AC_NO LIKE'%S'
AND LNAME LIKE 'S%';
 Output:

 Explanation:
In this example, there are two conditions:
 The first condition is that the AC_NO end with capital letter S and
the LNAM start with capital letter S.
 The second condition is that the TYPE is CSIT.
 Therefore, the SELECT statement reads as follows:
“Select the row if an author AC_NO ends with letter S and the
LNAME starts with letter S or the TYPE is equal to CSIT.
 Example: with parentheses
SELECT (FNAME||','||LNAME)AS NAME, BOOK,
TYPE,AC_NO
FROM AUTHORS
WHERE (TYPE='CSIT'
OR AC_NO LIKE'%S')
AND LNAME LIKE 'S%';

 Output:

Database Management System 1 5


Week 3 Logical Condition

 Explanation:
 In this example, there are two conditions:
 The first condition is that the AC_NO ends with letter S or the TYPE
are equal to CSIT
 The second condition is that the LNAME ends with letter S.
 “Select the row if an author AC_NO ends with letter S and the
LNAME starts with letter S and the TYPE is equal to CSIT.

Lesson Summary:
In this lesson, you should have learned how to use the following
statements:
 Apply the logical condition.
 Identify the order of precedence.

Terms to Remember!
 AND - Both component condition can be true for any record to be
selected.
 Logical condition - combines the result of two component conditions
to produce a single result based on those conditions or it inverts the
result of a single condition
 NOT - Reverse the value from TRUE to FALSE or FALSE to TRUE.
 OR - Either component condition can be true for any record to be
selected.
 Rules of precedence - determine the order in which expressions are
evaluated and calculated.

Textbook:
 Oracle Press (2010). Applied Oracle Security

_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
References:

References  Pratt, Philip J. (2010). Database management systems


 Rob, Peter & Coronel, Carlo (2009). Database Management Systems
 Schwalbe, Kathy (2011). Management of Information Technology
Projects
 Wheeler, Evan (2011). Security Risk Management : Building an
Information Security Risk Management Program from the Ground Up

Supplementary Reading and Video Link


Supplementary Reading
https://www.tutorialspoint.com/sql/sql-and-or-clauses.htm
https://docs.oracle.com/cd/B19306_01/server.102/b14200/conditions004.htm
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/logical-
operators-transact-sql

Supplementary Video
https://www.youtube.com/watch?v=JRmpM9UNY-w
https://www.youtube.com/watch?v=OjJUPYS6o1E
https://www.youtube.com/watch?v=eXGOYouMkJA

Suggested Reading
 SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
 Database management system. In Encyclopedia Britannica, Retrieved
from http://www.britannica.com/EBchecked/topic/152201/database-
management-system-DBMS.
 SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQL
 Database Administration. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/Database_administration.aspx
 SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
 Tutorialspoint.com
 oracle.com

Database Management System 1 7


Week 3 Logical Condition

 apex.oracle.com

_____________________________________________________________________________________
8

You might also like