0% found this document useful (0 votes)
6 views13 pages

Week 4 b2

This document explains the use of logical and relational operators in SQL, including AND, OR, NOT, and various relational operators like equal, not equal, greater than, and less than. It provides syntax examples and scenarios for applying these operators in SQL queries to filter data based on multiple conditions. Additionally, it covers nested logical operators, the IN operator, BETWEEN operator, LIKE operator, EXISTS operator, and the use of ANY and ALL with comparison operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Week 4 b2

This document explains the use of logical and relational operators in SQL, including AND, OR, NOT, and various relational operators like equal, not equal, greater than, and less than. It provides syntax examples and scenarios for applying these operators in SQL queries to filter data based on multiple conditions. Additionally, it covers nested logical operators, the IN operator, BETWEEN operator, LIKE operator, EXISTS operator, and the use of ANY and ALL with comparison operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

WEEK 4

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.

2. OR: TRUE if one of the Boolean expressions is TRUE.

3. NOT: Reverses the value of any other Boolean operator.


The Logical Operators are used to compare two conditions to check whether a row
(or rows) can be selected for the output.
Logical AND Operator
The Logical AND operator compares two conditions and returns TRUE if both of
the conditions are TRUE and returns FALSE when either is FALSE. If you want to
select rows that must satisfy all the given conditions, then in such cases you need
to use the AND operator.
We can use AND condition with SELECT, INSERT, UPDATE or DELETE
statements to test two or more conditions in an individual query.
Syntax to use AND Operator:

Note: Returns ‘True’ if both component conditions are true. Returns ‘False’ if any
one component condition or Both Component conditions are False.

False Condition Example using AND Operator:


Let’s modify the SQL statement so that the second condition becomes false. As in
our employee table, there is no gender in the yar whose age is 30. Now, in the
below SQL Statement, the second condition becomes false. And its return empty
set.

Logical NOT Operator :


We can also use the NOT keyword in the statement to revert one of the conditions.
Suppose our requirement is to fetch all the yar whose age is 29 and gender is not
equal to male. Then in that case we can use NOT Operator as shown in the below
query.

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).

Between Operator Example:


If you want to fetch all the employees from the yar table where the employee age is
between 20 and 22, then we need to write the SELECT SQL Query using the
Between Operator.

Like Operator Using %


The percent (%) means it will search for zero or more characters after the “%”.
Here ‘%’ is the wildcard character that we will use before or after characters to
search for the required matched string of characters.
Example: Fetch all the yar whose names start with the letter r.

The SQL EXISTS Operator


The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
EXISTS Syntax:
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

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

WHERE age > ALL (

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

WHERE age < ANY (

SELECT age

FROM yar

);

Relational Operators with Examples


What are Relational Operators?
The Relational Operators are used to compare two values i.e. these operators are
used for comparing one expression with another expression. The relational
operators determine whether the two values are equal or a value is greater than the
other, or less than the other.
Types of Relational Operators
The different types of relational operators are as follows:
1. Equal (=) Operator
2. Not Equal (!= or <>) Operator
3. Greater Than (>) Operator
4. Less Than (<) Operator
5. Greater Than or Equal To (>=) Operator
6. Less Than or Equal To (<=) Operator

Equal (=) Relational Operator


The Equal (=) Operator is used to check whether the two expressions are equal or
not. If both the expressions are equal then the condition becomes true and will
return the matched rows.

Not Equal (!=) Relational Operator


The Not Equal (!=) Operator is just the opposite of the equal operator. That means
this operator is used to check whether the two expressions are equal or not. If both
the expressions are not equal then the condition becomes true and will return the
not-matched records.
Not Equal (<>) Relational Operator
The Not Equal (<>) Operator is the same as the Not Equal (!=) operator. That
means it is also used to check whether the two expressions are equal or not. If both
the expressions are not equal then the condition becomes true and will return the
not-matched records.

Greater Than (>) Relational Operator


The Greater Than (>) Operator is used to check whether the left-hand expression
value is higher than the right-hand expression value. If the left-hand expression
value is higher than the right-hand expression value then the condition becomes
true and it will return the matched records.
This operator is used for the Greater than test.
Less Than (<) Relational Operator
The Less Than (>) Operator is used to check whether the left-hand expression
value is lower than the right-hand expression value. If the left-hand expression
value is lower than the right-hand expression value then the condition becomes
true and will return the matched records.
This operator is used for less than test.

Greater Than or Equal To (>=) Operator


The Greater than or Equal To (>=) Operator is used to check whether the left-hand
expression value is higher than or equals to the right-hand expression value or not.
If the left-hand expression value is higher than or equals to the right-hand
expression value then the condition becomes true and will return all the matched
records.
This operator is used to check the Greater than or equal test.
Less Than or Equal To (<=) Operator
The Less than or Equal To (<=) Operator is used to check whether the left-hand
expression value is lower than or equal to the right-hand expression value or not. If
the left-hand expression value is lower than or equals to the right-hand expression
value then the condition becomes true and it will return all the matching records.
This operator is used for Less than or Equal to test.

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.

You might also like