Operators in SQL are used to perform operations and specify conditions in SQL statements. There are four main types of operators: arithmetic operators for numeric calculations, comparison operators for comparing values, logical operators for combining conditions, and set operators for combining result sets. Arithmetic operators include addition, subtraction, multiplication, and division. Comparison operators compare values for equality, greater than, less than, etc. Logical operators combine conditions with AND, OR, and NOT. Set operators such as UNION, INTERSECT, and MINUS are used to combine result sets from multiple queries.
2. Type of Operators
• An operator is a reserved word or a character used
primarily in an SQL statement's WHERE clause to
perform operation(s), such as comparisons and
arithmetic operations. These Operators are used to
specify conditions in an SQL statement and to serve as
conjunctions for multiple conditions in a statement.
• Arithmetic operators
• Comparison operators
• Logical operators
• Set Operators
3. Arithmetic Operators
• Arithmetic Operators
– Arithmetic operators are used to manipulate
numeric operands, which are columns Arithmetic
operators are used to manipulate numeric
operands, which are columns
• Types of arithmetic operators:
– Monadic Arithmetic Operators, which are namely,
• + and –
– Dyadic Arithmetic Operators, which are namely,
• /,*,+ and -
Rule:
If the value of any operand in a numeric value expression is null value, then
the result of thatnumeric value expression is the null value.
4. Arithmetic Operators
Operator Description Example
+ (Addition)
Adds values on either side
of the operator.
a + b will give 30
- (Subtraction)
Subtracts righthand
operand from left hand
operand.
a - b will give -10
* (Multiplication)
Multiplies values on either
side of the operator.
a * b will give 200
/ (Division)
Divides left hand operand
by right hand operand.
b / a will give 2
% (Modulus)
Divides left hand operand
by right hand operand and
returns remainder.
b % a will give 0
5. SQL Comparison Operators
• Assume 'variable a' holds 10 and 'variable b' holds 20, then
−
Operator Description Example
=
Checks if the values of two operands are equal or not, if yes then
condition becomes true.
(a = b) is not true.
!=
Checks if the values of two operands are equal or not, if values are
not equal then condition becomes true.
(a !=b) is true.
<>
Checks if the values of two operands are equal or not, if values are
not equal then condition becomes true.
(a <> b) is true.
>
Checks if the value of left operand is greater thanthe value of right
operand, if yes then condition becomes true.
(a > b) is not true.
<
Checks if the value of left operand is less than thevalue of right
operand, if yes then condition becomes true.
(a < b) is true.
>=
Checks if the value of left operand is greater thanor equal to the
value of right operand, if yes then condition becomes true.
(a >= b) is not true.
<=
Checks if the value of left operand is less than or equal to thevalue
of right operand, if yes then condition becomes true.
(a <= b) is true.
!<
Checks if the value of left operand is not less than thevalue of
right operand, if yes then condition becomes true.
(a !<b) is false.
!>
Checks if the value of left operand is not greater thanthe value of
right operand, if yes then condition becomes true.
(a !>b) is true
6. SQL Comparison Operators - Continues
• BETWEEN
The BETWEEN operator is used to search for values that are within a set of values, given the
minimum value and the maximum value.
• IN
The IN operator is used to compare a value to a list of literal values thathave been
specified.
• IS NULL
The NULL operator is used to compare a value with a NULL value.
• ALL
The ALL operator is used to compare a value to all values in another value set.
• ANY
The ANY operator is used to compare a value to anyapplicable value in the list as per the
condition.
• UNIQUE
The UNIQUE operator searches every row of a specified table for uniqueness (no
duplicates).
• EXISTS
The EXISTS operator is used to search for the presence of a row in a specified table that
meets a certain criterion.
7. Logical Operators
• AND
• The AND operatorallows the existenceof
multiple conditionsin an SQL statement's
WHERE clause
• NOT
• The NOT operatorreversesthemeaning
of the logical operatorwith which it is
used.Eg: NOT EXISTS,NOT BETWEEN,
NOT IN, etc. This is a negateoperator.
• OR
• The OR operatoris used to combine
multiple conditionsin an SQL statement's
WHERE clause.
14. INTERSECT
• The following statement combines the
results with the INTERSECT operator,
which returns only those rows returned by
both queries:
• SELECT empno FROM emp
INTERSECT
SELECT empno FROM oldemp;
16. MINUS
• The following statement combines results
with the MINUS operator, which returns
only rows returned by the first query but
not by the second:
• SELECT empno FROM emp
MINUS
SELECT empno FROM oldemp;