3) Operators
3) Operators
===========
- to perform some operation on the given operand values.
- oracle supporting the following operators are,
Assignment operator:
=================
- to assign a value to a variable / to a attribute.
syntax:
=======
<attribute name> = <value>;
Ex:
SELECT * FROM EMP WHERE DEPTNO=10;
UPDATE EMP SET SAL=34000 WHERE JOB='CLERK';
DELETE FROM EMP WHERE SAL=5000;
Arithmetic operators:
==================
- to perform addition,subtraction,multiple and division.
syntax:
=======
< column name> <arithmetic operator> <value> ;
Ex:
waq to display emplyees salaries after adding 1000/- ?
SQL> SELECT SAL AS OLD_SALARY,SAL+1000 AS NEW_SALARY FROM EMP;
Ex:
waq to display empno,ename,basic salary and annual salary of employees?
SQL> SELECT EMPNO,ENAME,SAL AS BASIC_SALARY,
2 SAL*12 AS ANNUAL_SALARY FROM EMP;
Ex:
waq to display ename,basic salary,10% of increment salary and total salary
of employees who are working as a "manager".
SQL> SELECT ENAME,JOB,SAL AS BASIC_SALARY,
2 SAL*10/100 AS INCREMENT_SALARY,
3 SAL+SAL*10/100 AS TOTAL_SALARY
4 FROM EMP WHERE JOB='MANAGER';
Realtional operators:
==================
- comparing a specific column values with a specific condition in the query.
syntax:
======
where <column name> <relational operator> <value>;
Ex:
waq to display employees who are joined before 1981?
SQL> SELECT * FROM EMP WHERE HIREDATE<'01-JAN-81';
Ex:
waq to display employees who are joined after 1981?
SQL> SELECT * FROM EMP WHERE HIREDATE > '31-DEC-81';
Logical operators:
================
- to check more than one condition.
- AND , OR , NOT operators.
AND :
=====
- it return a value when both conditions are true.
cond1 cond2
===== =====
T T ----> T
T F ----> F
F T ----> F
F F ----> F
syntax:
=======
where <condition1> and <condition2>
Ex:
waq to display employees who are working as a "manager" and whose name is "blake"?
SQL> SELECT * FROM EMP WHERE JOB='MANAGER' AND ENAME='BLAKE';
OR:
===
- it return a value if any one condition is true from the given group of
conditions.
cond1 cond2
===== =====
T T ----> T
T F ----> T
F T ----> T
F F ----> F
syntax:
======
where <condition1> or <condition2>
Ex:
waq to display employees who are working as a "salesman" or whose salary is more
than 1500?
SQL> SELECT * FROM EMP WHERE JOB='SALESMAN' OR SAL>1500;
Ex:
waq to display employees whose empno is 7369,7788?
SQL> SELECT * FROM EMP WHERE EMPNO=7369 OR EMPNO=7788;
NOTE:
======
working on same row data then use "AND" operator.
working on different rows data then use "OR" operator.
NOT:
====
- it returns all values except the given conditional values in the query.
syntax:
=======
where not <condition1> and not <condition2>
Ex:
waq to display employees who are not working as a "clerk" and as a "salesman" ?
SQL> SELECT * FROM EMP WHERE NOT JOB='CLERK' AND NOT JOB='SALESMAN';
SET OPERATORS:
===============
- set operators are used to combined the results of two select statements
as a single set of values.
- these operators are "union,union all,intersect,minus".
syntax:
======
<select query1> <set operator> <select query2>;
Union:
=====
- it combined the results of two select statements without duplicate
values.
Union all:
========
- it combined the results of two select statements with duplicate
values.
Intersect:
========
- it return common values from the results of two select statements.
Minus:
======
- it return all values from the result of left side select query which are
not found in the result of right side select query.
DEMO_TABLES:
=============
SQL> SELECT * FROM EMP_HYD;
Ex:
waq to display all employees details who are working in "NARESHIT" organization?
SOL:
SQL> SELECT * FROM EMP_HYD UNION ALL SELECT * FROM EMP_CHENNAI; (including
duplicate rows)
SQL> SELECT * FROM EMP_HYD UNION SELECT * FROM EMP_CHENNAI;(excluding duplicate
rows)
Ex:
waq to display employee details who are working in both branches?
SQL> SELECT * FROM EMP_HYD INTERSECT SELECT * FROM EMP_CHENNAI;
Ex:
waq to display employees details who are working in HYD but not in CHENNAI
branch?
SQL> SELECT * FROM EMP_HYD MINUS SELECT * FROM EMP_CHENNAI;
Basic Rules:
===========
1. No.of columns should be same in both selected queries.
2. Order of columns and datatypes of columns are must be match in both
select statements.
======================================================================
Special operators:
================
IN :
===
- comparing the list of values with a single condition.
syntax:
======
where <column name> in(value1,value2,...................);
Ex:
waq to list out the employees whose empno is 7369,7566,7788,7900?
SQL> SELECT * FROM EMP WHERE EMPNO IN(7369,7566,7788,7900);
Ex:
waq to list out employees details who are not working as a
'clerk','manager','salesman'?
SQL> SELECT * FROM EMP WHERE JOB NOT IN('CLERK','MANAGER','SALESMAN');
BETWEEN:
=========
- is used to work on a particular range value.
syntax:
======
where <column name> between <low value> and <high value>;
Basic rules:
==========
1. it returns all values including source and destination values from the
given range value.
Ex:
waq to display employees details who are joined 1981?
SQL> SELECT * FROM EMP WHERE HIREDATE BETWEEN '01-JAN-81' AND '31-DEC-81';
Ex:
waq to display employees details who are not joined 1981?
SQL> SELECT * FROM EMP WHERE HIREDATE NOT BETWEEN '01-JAN-81' AND '31-DEC-81';
IS NULL:
=======
- this operator is used to comparing NULLS in a table.
syntax:
======
where <column name> is null
Ex:
waq to display employees whose commission is empty (or) null ?
solution:
SQL> SELECT * FROM EMP WHERE COMM=NULL;---not allowed
SQL> SELECT * FROM EMP WHERE COMM IS NULL;---allowed
Ex:
waq to display employees whose commission is not empty (or) null ?
solution:
SQL> SELECT * FROM EMP WHERE COMM IS NOT NULL;
What is NULL?
=============
- it is a unknow value / undefined value / empty.
- NULL != 0 & NULL != space.
- when we need identify a "null / empty" then we have a special keyword
is called as "IS" keyword.
Ex:
waq to display EMPNO,ENAME,SALARY,COMM and also SALARY+COMMISSION
of the employee "ALLEN"?
SOLUTION:
SQL> SELECT EMPNO,ENAME,SAL,COMM,SAL+COMM FROM EMP
WHERE ENAME='ALLEN';
Ex:
waq to display EMPNO,ENAME,SALARY,COMM and also SALARY+COMMISSION
of the employee "SMITH"?
solution:
SQL> SELECT EMPNO,ENAME,SAL,COMM,SAL+COMM FROM EMP
2 WHERE ENAME='SMITH';
EMPNO ENAME SAL COMM SAL+COMM
---------------- ---------- ---------- -------- ----------
7369 SMITH 800
- in the above example the employee "smith" salary is 800 and there is
no commission so that salary+commission is "800" only but it returns "NULL".
- if any arithmetic operator is performing some operation with "NULL"
then it again returns "NULL" only.
Ex:
if x=100;
i) x+null ---> 100+null ---> null
ii) x-null ---> 100-null ----> null
iii) x*null --> 100*null ---> null
iv) x/null ---> 100/null ----> null
What is NVL():
=============
- it stands for "null value".
- it pre-defined function in oracle db.
- this function is used to replace a user defined value inplace of NULL.
- it is having two arguments those are "expression1 & expression2".
syntax:
=======
NVL(expression1,expression2)
EX:
SQL> SELECT NVL(NULL,0) FROM DUAL;
NVL(NULL,0)
-----------
0
NVL(NULL,100)
-------------
100
NVL(0,100)
----------
0
Solution:
========
SQL> SELECT EMPNO,ENAME,SAL,COMM,SAL+NVL(COMM,0) TOTAL_SALARY FROM EMP
WHERE ENAME='SMITH';
syntax:
=======
NVL2(Expression1,Expression2,Expression3)
Ex:
SQL> SELECT NVL2(NULL,100,200) FROM DUAL;
NVL2(NULL,100,200)
------------------
200
NVL2(0,100,200)
---------------
100
Ex:
Waq to updating all employees commissions in a table based on the following
conditions are,
i) if employee commission is NULL then it will update with 400.
ii) if employee commission is NOT NULL then it will update with
commission+400.
Solution:
=======
SQL> UPDATE EMP SET COMM=NVL2(COMM,COMM+400,400);
LIKE :
=====
- fetching data from a table based on character pattern wise.
- when we use "like" operator we should use the following
wildcard operators:
% : it represent the remaining group of characters after
selected character.
_ : counting a single character in a expression.
Ex:
select * from emp where ename like 'S%';----return value
S %
== ===
S MITH
S URESH
S COTT
S UMAN
S AI
Ex:
select * from emp where ename like '___';-----> anu,sai
SMITH
SURESH
SCOTT
SUMAN
SAI
ANU
syntax:
======
where <column name> like ' [<wildcard operator>] <character pattern> [<wildcard
operator>] ';
Ex:
to display employees whose name starts with "S" character?
SQL> SELECT * FROM EMP WHERE ENAME LIKE 'S%';
Ex:
to display employees whose name ends with"R" character?
SQL> SELECT * FROM EMP WHERE ENAME LIKE'%R';
Ex:
to display employees whose name is having "I" character?
SQL> SELECT * FROM EMP WHERE ENAME LIKE'%I%';
Ex:
to display employees whose name starts with "M" and ends with "N" ?
SQL> SELECT * FROM EMP WHERE ENAME LIKE'M%N';
Ex:
to display employees whose name is having 4 characters?
SQL> SELECT * FROM EMP WHERE ENAME LIKE'____';
Ex:
to display employees whose name is having 2nd character is "O" ?
SQL> SELECT * FROM EMP WHERE ENAME LIKE'_O%';
Ex:
to display employees whose empno is starts with 7 and ends with 8 ?
SQL> SELECT * FROM EMP WHERE EMPNO LIKE '7%8';
Ex:
to display employees who are joined in 1981?
SQL> SELECT * FROM EMP WHERE HIREDATE LIKE'%81';
Ex:
to display employees who are joined in the month of "may"?
SQL> SELECT * FROM EMP WHERE HIREDATE LIKE'%MAY%';
Ex:
to display employees who are joined in the month of "may","december"?
SQL> SELECT * FROM EMP WHERE
HIREDATE LIKE'%MAY%' OR HIREDATE LIKE'%DEC%';
Ex:
to display employees who are joined in the month of "december" in 1980?
SQL> SELECT * FROM EMP WHERE
HIREDATE LIKE'%DEC%' AND HIREDATE LIKE '%80';
LIKE OPERATOR WITH SPECIAL CHARACTERS:
========================================
DEMO_TABLE:
============
SQL> SELECT * FROM CUSTOMER;
CID CNAME
---------- ----------
1021 _SMITH
1022 WAR#NER
1023 JON%ES
1024 ALL@EN
1025 MILL_ER
Ex:
to disply customers whose name is having "@" symbol?
SQL> SELECT * FROM CUSTOMER WHERE CNAME LIKE '%@%';
Ex:
to disply customers whose name is having "#" symbol?
SQL> SELECT * FROM CUSTOMER WHERE CNAME LIKE '%#%';
Ex:
to disply customers whose name is having "_" symbol?
SQL> SELECT * FROM CUSTOMER WHERE CNAME LIKE '%_%';-----wrong result
Ex:
to disply customers whose name is having "%" symbol?
SQL> SELECT * FROM CUSTOMER WHERE CNAME LIKE '%%%';-----wrong result
- Generally db server will treat " _ , %" as wildcard operators but not
as a special characters.to overcome this problem we should use a pre-defined
keyword is " ESCAPE'\' ".
SOLUTION:
==========
SQL> SELECT * FROM CUSTOMER WHERE CNAME LIKE '%\_%'ESCAPE'\';
SQL> SELECT * FROM CUSTOMER WHERE CNAME LIKE '%\%%'ESCAPE'\';
EX:
to display employees whose name not starts with "S" character?
SQL> SELECT * FROM EMP WHERE ENAME NOT LIKE'S%';