Logical Operator and other Operators
uses in Oracle SQL.
Hello
Everyone,
Today I will
discuss about Logical Operator used in Oracle SQL. This Topic is important as
you can use Logical Operator in several SQL queries to get the result set as
per your requirement. I suggest you to please
go through my previous blogs which you will able to see in archive folder so
that you can use Logical Operator in SQL Queries. Let’s start with Logical
Operators used in SQL. SQL operators are reserved keywords which basically used
in the WHERE clause of a SQL Statement
to perform arithmetic, logical and comparison operations.
Since, there
are different types of operators in SQL, let us understand different type of
operator and its uses in SQL.
Below are
the Logical Operators used in SQL .
AND
OR
NOT
BETWEEN
LIKE
IN
EXIST
ALL
ANY
Uses of
above operator in SQL
1. AND Operator: AND operator displays the records,
which satisfy all the condition separated by AND, and give the output True. Also
this operator filters records that rely on more than one condition.
Syntax :
SELECT Column1, Column2
FROM
TableName
WHERE
Condition1 AND Condition2 AND Condition3 ...;
--- Note for
filter we can put more AND operator to filter the data as per requirement.
Let’s take example,
I have created 2 table employee and
department where I want to filter the data by using AND operator.
Note : For
reference you can check my previous blogs for aggregate function and Sub query/Joins
blog where I defined functions as well as IN, ANY and ALL operator.
Below are
the Query for your reference for AND operator.
select
dept_name, salary
from
department
where SALARY
>=45000
AND emp_id
IN(1000,2000,3000,4000)
AND
dept_name IN ('IT','SALES','HR','FINANCE','MARKETING');
If I want to
get total amount as salary for each department and suppress duplicate records
we can use below query to get the result .
select SUM(salary) TOTAL_SALARY , dept_name
from
department
where SALARY
>=30000
AND emp_id
IN(1000,2000,3000,4000)
AND
dept_name IN ('IT','SALES','HR','FINANCE','MARKETING')
GROUP BY
DEPT_NAME;
Below is the
output of both Queries:
Another Query
for AND operator by using Join :
select
sum(a.salary) ,a.dept_name ,
b.emp_name,b.designation
from department a , employee b
where a.emp_id=b.emp_id
AND a.dept_name IN
('IT','SALES','HR','FINANCE','MARKETING')
AND b.emp_id IN(1000,2000,3000,4000)
group by
a.dept_name , b.emp_name,b.designation
HAVING sum(a.salary) >=40000;
Below is the output for above query :
OR Operator: OR operator displays all the
records which satisfy any of the conditions separated by OR in query and give
the output TRUE. In another way if any condition satisfied it will give the output.
Syntax in
SQL for OR Operator:
SELECT
Column1, Column2, ..., ColumnN
FROM
TableName WHERE Condition1 OR Condition2
OR ...;
Let's write
query by using tables with both simple as well as join query to see the output.
select
dept_name, salary
from
department
where
salary>= 50000
OR
emp_id=1000
OR dept_name
IN ('IT','SALES','HR','FINANCE','MARKETING');
Note : Here
any of the condition is true it will give the result accordingly.
Output of above
Query is as below:
Using Join
with Aggregate function for OR operator.
select
sum(a.salary) ,a.dept_name ,
b.emp_name,b.designation
from department a , employee b
where a.emp_id=b.emp_id
OR a.dept_name ='IT'
OR b.emp_id IN(1000,2000,3000,4000)
group by
a.dept_name , b.emp_name,b.designation
HAVING sum(a.salary) >=40000;
Below is the
output for above query :
Here you
clearly see the difference between AND /OR operator as in AND operator it will
check all the condition whereas in OR operator any condition matches it will
give the result.
NOT Operator:
The NOT operator is used, when you want to display the records which do not
satisfy a condition/criteria.
Syntax
SELECT
Column1, Column2, ...
FROM
TableName
WHERE NOT
Condition;
Let's write
query by using tables with both simple as well as join query to see the output.
select dept_name, salary
from
department
where NOT
dept_name IN('IT','HR') ;
Here you see
the output apart from IT and HR all the records displayed by using NOT
operator.
Below is the
Output of above Query for your reference.
Note: You
can also combine the all three operators and write the query:
Query:
SELECT *
FROM DEPARTMENT
WHERE NOT
EMP_ID='1000'
AND (EMP_ID=2000
OR salary
>40000);
Below is the
output of above query.
Hope you are
able to understand AND , OR and NOT operator in SQL .
Below are
some different operator used in SQL.
BETWEEN
Operator:
The BETWEEN
operator is used, when you want to select values within a given range. In
Between both the starting and ending
values are considered.
Below is the
query we can use for Between Operator.
select *
from department
where salary between 40000 AND 120000 ;
Output of
the above query is as below
LIKE
Operator
The LIKE
operator also known as wild card used in
a WHERE clause to search for a specified pattern . There are mainly two
wildcards that are used in the LIKE operator:
% – It’s for zero or more character
_ – It’s matches exactly one character.
Syntax
SELECT
ColumnName
FROM
TableName where column_name like pattern;
select *
from department
where dept_name like 'F%';
Output of
above query is as below :
Like Operator Condition and description and when to use like is as below :
If we put
condition in SQL --WHERE CustomerName LIKE 'a%
output --Finds any values that start with “a”
If we put
condition in SQL -- WHERE CustomerName LIKE '%b'
output --Finds any values that end with “b”
If we put
condition in SQL -- WHERE CustomerName LIKE '%abc%'
output --
Finds any values that have “abc” in any position
If we put
condition in SQL -- WHERE CustomerName LIKE '_c%'
output --
Finds any values that have “c” in the second position.
If we put
condition in SQL -- WHERE CustomerName LIKE 'd_%_%'
output--Finds
any values that start with “d” and are at least 3 characters in length
If we put
condition in SQL --WHERE ContactName LIKE 'a%e'
output--Finds
any values that start with “a” and end with “e”
EXIST Operator:
Exist
operator is basically used to test whether record exist or not.
Below is the
syntax of SQL using Exist operator.
SELECT
dept_name,salary
FROM
department
WHERE EXISTS
(SELECT emp_id FROM
department
where emp_id=1000 AND dept_name IN('IT','HR') );
Output of above
Query is as below .
IN SQL we
also use below operators for calculation and logical use:
Arithmetic
operator :
% -- Modulus
/ -- Divide
* --Multiplication
- --
Subtraction
+ -- Addition
This
operator we can use in SQL for calculation and check process in select
statement
Bitwise Operator :
^ --
Biitwise Excusive OR
| -- Bitwise OR
& -
Bitwise AND
Comparison Operator
<
> -- Not equals to
<= -- Less than or equals to
> = --
Greater than equals to
< -- Less than
> -- Greater than
= -- Equals to
Compound
Operators :
|*= -- Bitwise or Equals , ^-= -- Bitwise
exclusive Equals , &= -- Bitwise and equals , %= --
Modulo
equals , /= -- Divide equals , *= Multiplies equals etc.
All this operators we can use in SQL statement , sub queries , co-related sub queries for calculation and Logical issues as per our Requirement.
IN/ANY and
ALL operator:
Please see
my previous blog as I well defined all the operator with examples and explanation.
It’s big so you can follow my below blog for your reference.
https://dikshaoracletraining.blogspot.com/2020/10/sql-subquery-and-co-related-subquery-in.html
This all are
the operators commonly used in SQL , Go
through all the Operators and try to write some queries using Logical operators
.
Please go
through this blog carefully and let me know if you have any questions. This Topic
is important for candidate who is preparing for interview and also good for
students to explore logical operator in SQL Query.
Thanks
ReplyDeleteIt is very useful for me. Thanks...
Microsoft Azure DevOps Online Training
Microsoft Azure DevOps Live Online Training