Practical 3
AIM: Use of operators in SQL
SQL Operators perform arithmetic, comparison, and logical operations to manipulate and
retrieve data from databases.
In this article, we will discuss Operators in SQL with examples, and understand how they
work in SQL.
Operators in SQL
Operators in SQL are symbols that help us to perform specific mathematical and logical
computations on operands. An operator can either be unary or binary.
The unary operator operates on one operand, and the binary operator operates on two
operands.
Types of Operators in SQL
Different types of operators in SQL are:
• Arithmetic operator
• Comparison operator
• Logical operator
• Bitwise Operators
• Compound Operator
• SQL Arithmetic Operators
SQL arithmetic operators
Arithmetic operators in SQL are used to perform mathematical operations on numeric
values in queries. Some common arithmetic operators are:
Operator Description
+ The addition is used to perform an addition operation on the data values.
– This operator is used for the subtraction of the data values.
This operator works with the ‘ALL’ keyword and it calculates division
/
operations.
* This operator is used for multiplying data values.
Operator Description
% Modulus is used to get the remainder when data is divided by another.
SQL Comparison Operators
Comparison operator in SQL are used to compare one expression’s value to other
expressions. SQL supports different types of comparison operator, which are described
below:
Operator Description
= Equal to.
> Greater than.
< Less than.
>= Greater than equal to.
<= Less than equal to.
<> Not equal to.
SQL Logical Operators
Logical operator in SQL are used to combine or manipulate conditions in SQL queries to
retrieve or manipulate data based on specified criteria.
Operator Description
Logical AND compares two Booleans as expressions and returns
AND
true when both expressions are true.
Logical OR compares two Booleans as expressions and returns
OR
true when one of the expressions is true.
Not takes a single Boolean as an argument and change its value
NOT
from false to true or from true to false.
SQL Bitwise Operators
Bitwise operators in SQL are used to perform bitwise operations on binary values in SQL
queries, manipulating individual bits to perform logical operations at the bit level. Some SQL
Bitwise Operators are:
Operator Description
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise XOR (exclusive OR) operator
~ Bitwise NOT (complement) operator
<< Left shift operator
>> Right shift operator
SQL Compound Operators
Compound operator in SQL are used to perform an operation and assign the result to the
original value in a single line. Some Compound operators are:
Operator Description
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulo and assign
&= Bitwise AND and assign
^= Bitwise XOR and assign
|= Bitwise OR and assign
SQL Special Operators
Special operators are used in SQL queries to perform specific operations like comparing
values, checking for existence, and filtering data based on certain conditions.
Operators Description
ALL is used to select all records of a SELECT
STATEMENT. It compares a value to every value in a
ALL list of results from a query. The ALL must be preceded
by the comparison operators and evaluated to TRUE if
the query returns no rows.
Operators Description
ANY compares a value to each value in a list of results
ANY from a query and evaluates to true if the result of an
inner query contains at least one row.
The SQL BETWEEN operator tests an expression
BETWEEN against a range. The range consists of a beginning,
followed by an AND keyword and an end expression.
The IN operator checks a value within a set of values
IN separated by commas and retrieves the rows from the
table that match.
The EXISTS checks the existence of a result of a
subquery. The EXISTS subquery tests whether a
EXISTS
subquery fetches at least one row. When no data is
returned then this operator returns ‘FALSE’.
SOME operator evaluates the condition between the
outer and inner tables and evaluates to true if the final
SOME
result returns any one row. If not, then it evaluates to
false.
The UNIQUE operator searches every unique row of a
UNIQUE
specified table.