What Is SQL Operator
What Is SQL Operator
What Is SQL Operator
Predicate Description
SELECT
SUM(CASE
WHEN part
BETWEEN 100 Predicate in a CASE expression specifying a condition
AND 199 that determines the value passed to the SUM function
THEN 0 for a particular row in the Orders table.
ELSE cost
END)
FROM Orders;
SQL Operator:
Operators are the foundation of any programming language.
We can define operators as a reserved word or character or symbol which is
used primarily in a SQL WHERE clause to specify conditions in a SQL SELECT
UPDATE and DELETE statements by performing any kind of operations such
as logical, comparison and Arithmetic operations (query a database/tables.) on
databases/tables. Operators act as a connector between two or more
conditions (for various conditional statements). The operator manipulates the
data and gives the result based on the operator’s functionality.
https://minigranth.in/sql-tutorial/sql-operators
Types of SQL operators:
SQL provides three types of operations that can be
performed on the database/tables according to the need. These operators are
namely :
Arithmetic Operators.
Comparison Operators.
Logical Operators.
Arithmetic operators: The SQL Arithmetic operator is used to perform
arithmetic computations and operations on the two operands or the numerical
data present in the tables of the database.
The Arithmetic operators are capable of performing all arithmetic operations
like addition, subtraction, multiplication, division, and modulus on the
operands of the operator.
Operator Description
+ Addition operator - it is used to perform addition operations on
the operands.
- Subtraction operator – he Subtraction operator is used to
perform subtraction operations on the operands (to get the
difference between operands).
* Multiplication operator – The Multiplication operator is used to
perform multiplication operations on the operands.
/ Division operator - The Division operator is used to perform
division operations on the operands.
% Modulus operator - The Modulus operator is used to perform
modulus operation on the operands (to get the remainder when
one operand is divided by the other).
Comparison operators: The SQL Comparison operator is used to compare the
two operands or the two data values present in the database tables. The
comparison operators are also capable of comparing one expression to the
other expression. Some of the comparison operators are:
Operator Description
Equals operator - The Equal operator is used to show data that
=
matches with the provided value in the query.
Greater Than operator - The Greater Than operator is used to
>
show data that are greater than the provided value in the query.
Less Than operator - The Lesser Than operator is used to show
<
data that are lesser than the provided value in the query.
Greater Than or Equal To operator - The Greater Than or
>= Equals To operator is used to show data that are greater than
and equal to the provided value in the query.
Less Than or Equal To operator - The Lesser Than or Equals
<= To operator is used to show data that are lesser than and equal
to the provided value in the query.
Not Equal To operator - The Not Equal operator is used to show
<> or !=
data that do not match with the provided value in the query.
!< Not Less Than operator - Compares two expressions and returns
TRUE if the left operand does not have a value lower than the right
operand; otherwise, the result is FALSE.
!> Not Greater Than operator - Compares two expressions and
returns TRUE if the left operand doesn’t have a greater value than
the right operand; otherwise, the result is FALSE.
Bit-wise Operators Bit-wise operators are used to perform bit-wise
operations (0 & 1) over the expressions which includes :
SQL Statements
Like any programming language, SQL uses statements. A statement is a
specific instruction for the computer to do something.
For example: SQL contains the CREATE statement. As you can probably
guess, this statement is used to create a new database or a new table or a new
user.
There are many different kinds of SQL statements. In the following, we'll focus
on just one of them: the SELECT statement.
Example
7 rows selected.
EMPNO ENAME SAL ANNUAL COMM
The above query performs multiplication and division operation on each and every value of the salary
column and displayed.
Bitwise operators apply true false conditions on the individual binary bits of numbers
Bitwise AND(&): Returns true you only are both input bits are true otherwise false
Bitwise OR(|): Returns true if either of the input bits is true otherwise false
Bitwise XOR(^): Returns true only if both the input bits are different
Comparison operators are generally used along with where clause for filtering the data as per the
required condition specified
Assume variable a holds 10 and variable b holds 20 then
Operator Example
= (a = b) is not true.
!= (a != b) is true.
Example
select empno,ename,sal,job
from emp
where sal>2500;
O/P
5 rows selected.
EMPNO ENAME SAL JOB
The above displays the details of employees whose salary is greater than 2500.
AND operator
The AND results true only when all the conjunction of conditions specified after the where clause
are satisfied
Example
O/P
2 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
Here two conditions first employee must be a manager at the same time he must be getting salary greater
than 2500 then only condition becomes true and the record gets displayed.
OR operator
Example
select * from emp
where JOB='ANALYST' OR JOB='MANAGER';
O/P
5 rows selected
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
In the example above, the data is fetched if the employee is an analyst or a manager i.e data is fetched
even if any one condition is satisfied.
SQL Special Operators
IN operator
It is used to specify a set of values and operation n is performed on all the values specified in the set
and if any of the value that is present in the list matches with the values present in a table then it
returns true and is operation is performed
Example
select empno,job,sal,hiredate from emp
where ename in('SCOTT','FORD','SMITH','JONES')
O/P
4 rows selected.
EMPN
O JOB SAL HIREDATE
Records of all those employees that are specified in the list of in are displayed.
BETWEEN Operator
It is used to perform data comparison and manipulation over a range of values present in the
database table
Example
select empno,job,sal,hiredate
from emp
where sal between 800 and 2900;
O/P
3 rows selected.
EMPNO JOB SAL HIREDATE
The details of those employees whose salary is present in the range between 800 to 2900
retrieved and it also considered specified values inclusive of the range
In the case of between operator lower value is first specified and followed by the higher value
& and operator in between this higher and lower values.
LIKE operator
The like operator is a pattern matching operator and returns those records that match the specified
pattern
Example
select empno,ename,hiredate,sal,job
from emp
where ename like 'S%';
O/P
2 rows selected.
EMPNO ENAME HIREDATE SAL JOB
All the records of employees whose name starting with letter ‘S’ are displayed.
IS NULL operator
All operations upon null values present in the table must be done using this ‘is null’ operator .we
cannot compare null value using the assignment operator
Example
select * from emp
where comm is null
O/P
4 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
The details of those employees whose commission value is Null are displayed.
NOT operator
Not operator is a negation operator which is used along with like between, is null, in operators, It
performs reverse r action of all these operators.
IS NOT NULL
select * from emp
where comm is not null;
O/P
3 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Details of all those employees whose Commission value is not null value are displayed.
NOT BETWEEN
select * from emp
where sal NOT between 800 and 2900
O/P
4 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
The details of those employees whose salary does not fall in the range between 800 to 2900 displayed.
NOT LIKE
select * from emp
where ename NOT like 'S%';
O/P
5 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
The details of all those employees whose name doesn’t start with letter ‘S’ are displayed.
NOT IN
select * from emp
where ename not in('SCOTT','FORD','SMITH','JONES');
O/P
5 rows selected
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
The details of all employees whose names are not among the list are displayed