Week 4 b2
Week 4 b2
OPERATORS
Aim:
To apply different logical operators and relational operators for rows in a table.
If you want to combine more than one condition, then you need to use the Logical
Operators. The Logical Operators are basically used to check for the truth-ness of
some conditions. Logical operators return a Boolean data type with a value of
TRUE, or FALSE. In Oracle, there are three Logical Operators available. They are
as follows:
1. AND: TRUE if both Boolean expressions are TRUE.
Note: Returns ‘True’ if both component conditions are true. Returns ‘False’ if any
one component condition or Both Component conditions are False.
Logical OR Operator:
The OR operator in Oracle is useful to add multiple conditions in a single SQL
statement. It displays the data rows if any one of the multiple conditions is TRUE.
If all the conditions are false the SQL statement won’t return any result set.
Let’s modify the SQL statement so that both conditions become FALSE.
Logical NOT Operator:
The Logical NOT Operator takes a single Boolean as an argument and changes its
value from false to true or from true to false. If we want to select rows that do not
satisfy a condition, then you need to use the logical NOT operator. NOT results in
the reverse of a condition. That is, if a condition is satisfied, then the row is not
returned.
Note: The NOT operator returns True if the condition is False and returns ‘False’ if
the following condition is True.
Nested Logical Operators:
We can also use multiple logical operators in a single SQL statement. When we
combine the logical operators in a SELECT statement, the order in which the
statement is processed is
1. NOT
2. AND
3. OR
Nested Logical Operators Example:
In this case, the rows that satisfy at least one of the above conditions are returned.
IN Operator:
Returns true if the value is available in the given list of values. Supports with all
types of data (data types).
SQL ALL
SQL ALL compares a value of the first table with all values of the second table
and returns the row if there is a match with all values.The condition compares the
student ages (returned by subquery) with the teacher's age. If the teacher's age is
greater than all student's ages, the corresponding row of the Teachers table is
selected.
Syntax:
SELECT *
FROM friend
SELECT age
FROM yar
);
ANY and ALL with Comparison Operators
We can use any comparison operators like =, >, <, etc. with
the ANY and ALL keywords.Let's see an example where we want teachers whose
age is less than any student.
SELECT *
FROM friend
SELECT age
FROM yar
);
Result Analysis:
I learned how to use logical operators and types of logical operators along with
relational operators to compare relation between values using operations with
examples.