OPERATORS
--->Operators are used to express the conditions in Select
statements.Operators are represented by special characters or
by keywords.The different types of Operators available in Oracle
SQL are: Arithmetic operators; Assignment operator; Relational
operators; Logical operators; Special Operators; Set Operators
> Arithmetic operators:
-->The arithmetic operations can be Used to perform any
Arithmetic Operations like Addition(+),subtraction(-),
Multiplication(*) and Divided by(/).
Syntax:
<Column Name><Arithmetic Operators><value>;
Arithmetic operator Addition(+):
--->used to perform addition between two numbers and date.
Ex:
Display salary of employees with 2000 increment in their salary.
Sql> SELECT ename,sal,sal + 2000 "Incremented salary" FROM
emp;
Arithmetic Operator Subtraction (-):
--->used to perform subtraction between two numbers and
dates.
Ex:
Display the details of employees decreasing their salary by 200.
Sql> select ename,sal,sal-200 from emp;
Arithmetic Operator Multiplication(*) :
--->Used to perform multiplication.
Ex:
Display the details of the employees Incrementing their salary
two times.
Sql> SELECT sal * 2 FROM emp;
Arithmetic Operator Division ( / ):
---->Used to perform Division test. Division will display only the
Quotient value not the remainder value.
---->For Example 6/2 gives 3 because 2 divides 6 by 3 times.
Ex:
Display half of the salary of employees.
Sql> SELECT sal, sal/2 FROM emp;
> Assignment operators(=):
---->This operator is used for equality test. Used to test the
equality of two operands.
Syntax:
<Column Name><Assignment Operator><Value>;
Ex:
Display the details of Employees whose salary is equal to 950.
Sql> SELECT *FROM emp WHERE sal=950;
> Relational Operators:
----> Relational Operators are <,>,<=,>=,NOT EQUAL TO (!=
(OR) ^= (OR) <>)
Lessthan(<):
Ex:
Display the details of the employees whose salary is less than
3000.
Sql> SELECT * FROM emp WHERE sal < 3000;
Greater than(>):
Ex:
Display the details of Employees whose salary is greater than
3000
Sql> SELECT * FROM emp WHERE sal > 3000;
Lessthan or Equals to Operator(<=):
Ex:
Display the details of Employees whose salary is less than or
equal to 3000.
Sql> SELECT * FROM EMP WHERE sal <= 3000;
Greater than or Equals to(>=):
Ex:
Display the details of Employees whose salary is greater than or
equal to 3000.
Sql> SELECT * FROM emp WHERE sal >= 3000;
Not equals to ( != OR ^= OR <> ):
---->This operator is used for inequality test.
Ex:
Display the details of employees whose salary is not equals to
3000.
Sql> SELECT * FROM emp WHERE sal != 3000;
Sql> SELECT * FROM emp WHERE sal ^= 3000;
Sql> SELECT * FROM emp WHERE sal <> 3000;
> Logical operators:
----> Logical Operators are AND,OR,NOT
AND Operator:
--->Returns ‘True’ if both component conditions are true.
Returns ‘False’ if any one component condition or Both
Component conditions are False.
Ex :
Display the details of Employees whose salary is Greater than
1000 AND also whose salary is less than 2000.
Sql> SELECT *FROM emp WHERE sal > 1000 AND sal <2000;
OR Operator:
---->Returns True if either component conditions become TRUE.
Returns False if both the component conditions becomes False.
Ex :
Display the details of Employees whose salary is Greater than
1000 OR also whose salary is less than 2000.
SQL> SELECT *FROM emp WHERE sal> 1000 OR sal < 2000;
NOT operator:
---->The NOT operator returns ‘True’ if the condition is False
and returns ‘False’ if the following condition is True.
Ex :
Display the details of employees whose salary is not Equals to
3000.
SQL> SELECT * FROM emp WHERE not sal =3000;
> Special operators:
----> Special Operators are (+ve)Operators and (-ve)Operators
(+ve)Operators (-ve)Operators
IN NOT IN
BETWEEN NOT BETWEEN
IS NULL IS NOT NULL
LIKE NOT LIKE
IN Operator:
--->Returns true if value is available in given list of values
--->Supports with all types of data .
Ex:
To display only employees whose empno's 7125,7369,7782 are
fetched?
SQL> SELECT *FROM emp WHERE empno IN (7125, 7369, 7782);
NOT IN Operator:
---> This operator is quite opposite to ‘IN’ clause.
Ex:
SQL> SELECT *FROM emp WHERE empno NOT IN
(7125,7369,7782);
BETWEEN Operator:
-->Returns true if value specified is within the specified range.
-->Supports with numbers and date values.
-->BETWEEN is an inclusive operator which includes range limits
in output
Ex: To display all employee records are fetched whose salary is
between 2000 and 3000?
SQL> SELECT *FROM emp WHERE sal BETWEEN 2000 AND
3000;
-->Whenever lower bound value is larger than upper bound
then it shows ‘no rows selected’
Ex (1):
SQL> SELECT *FROM emp WHERE sal BETWEEN 3000 AND 2000;
O/P:
-- No rows selected
NOT BETWEEN Operator:
-->Returns true if value specified is not within the specified
range.
-->Supports with numbers and date values.
-->NOT BETWEEN is an exclusive operator which eliminates
range limits from Output.
Ex (1):
SQL> SELECT *FROM emp WHERE sal NOT BETWEEN 2000 AND
3000;
NOTE:
Lower bound – value must be lower when compare to upper
bound value
Upper bound- value must be higher when compare to lower
bound value
Ex:
SQL> select ename,sal,job from emp where job NOT BETWEEN
'MANAGER' AND 'SALESMAN';
SQL> select ename,sal,job,hiredate from emp where hiredate
NOT BETWEEN '17-DEC-81' AND '20-JUN-83';
LIKE Operator:
-->Used to search for pattern in a given input. It is supported
with character data only. It uses two Meta characters
% (percentage) and _ (underscore) are two Meta characters.
-----> % (percentage) represents "zero or more characters"in the
given input.
-----> _ (underscore) represents "one character" in given input.
Syntax:
Select *From <tableName> Where <character data type
column> like ‘<value>’;
Ex (1):
Display the employees whose name is starting with ‘S’ in EMP
table.
SQL> SELECT * FROM emp WHERE ename LIKE'S%'
Ex (2):
Display the employees whose name ends with ‘S’ in EMP table
SQL> SELECT * FROM emp WHERE ename LIKE'%S'
Ex (3):
Display the employees whose names are having second letter
as ‘L’ in EMP table
SQL> SELECT * FROM emp WHERE ename LIKE '_L%'
IS NULL Operator:
--->Used to search for NULL values In the given input
--->Supports with all types of data
Syntax:
Select *from <tableName> Where <column Name> IS NULL;
Ex (1):
SQL> SELECT * FROM emp WHERE sal IS NULL;
O/P:
-- No rows selected;
Ex (2):
Disply all employees whose has commission enabled.
SQL> SELECT *FROM emp WHERE comm IS NULL;
IS NOT NULL Operator:
-->Use to search for NOT NULL values in the given input
-->Supports with all types of data
Syntax:
Select *from <table Name> Where <column Name> is not null;
Ex (1):
Dispaly all employees whose commission IS NOT NULL.
SQL> SELECT *FROM emp WHERE comm IS NOT NULL;
Ex (2):
SQL> select empno,ename,job,sal ,deptno from emp where mgr
IS NOT NULL;
> Set Operators:
--->SQL set operators allows combine results from two or
more SELECT statements. SQL set operators combine rows from
different queries with strong preconditions - all involved SELECTS
must.
SYNTAX:
TableA
<set operator>
TableB
<set operator>
Tablec
Operato
r Returns
UNION All distinct rows selected by either query
UNION ALL All rows selected by either query, including all duplicate
s
INTERSECT All distinct rows selected by both queries
MINUS All distinct rows selected by the first query but not the s
econd
Set Operators are-
--->The generic syntax of a query involving a set operation is:
<component query>{UNION | UNION ALL | MINUS |
INTERSECT}<component query>
Ex for UNION:
SQL> select empno,ename from emp where deptno=20
UNION
select empno,ename from emp where deptno=30;
Ex for UNION ALL:
SQL> select job from emp where deptno=20
UNION ALL
select job from emp where deptno=30;
EX for INTERSECT:
SQL> select empno,ename from emp where deptno=10
INTERSECT
select empno,ename from emp where deptno=30;
Ex for MINUS:
SQL>select job from emp where deptno=20
MINUS
select job from emp where deptno=30;