Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL 5

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

logical operators:

waq to display those clerks having more than 2000 sal


from emp

SQL> select * from emp where job='CLERK' and sal>2000;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7934 MILLER CLERK 7782 23-JAN-82 2500
10

SQL> select * from emp where deptno=10 or deptno=20;

waq to display the employees who are belongs to


10th,20th,30th dept from emp table

SQL> select * from emp where deptno=10 or deptno=20 or deptno=30;

note:
in place of or operator we can use in operator,in operator
performance is very high compared to or operator

waq to display the employee who are not belongs to


deptno 10 and deptno 30 from emp table

SQL> select * from emp where deptno not in(10,20);

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1000 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30

null:

null is a undefined/unknown/unavliabel value it is not


same as zero

SQL> select null+100 from dual;

NULL+100
----------

SQL> select 0+0 from dual;

0+0
----------
0

SQL> select 0+null from dual;


0+NULL
----------

waq to display ename,sal,sal+comm of smith from emp


table

SQL> select ename,sal,comm,sal+comm from emp where ename='SMITH';

ENAME SAL COMM SAL+COMM


---------- ---------- ---------- ----------
SMITH 900

nvl:
nvl is orcale predefined function used in place of null
value,it always accepts two perameters

syntax:
nvl(exp1,exp2);

SQL> select nvl(10,20) from dual;

NVL(10,20)
----------
10

SQL> select nvl(null,20) from dual;

NVL(NULL,20)
------------
20

SQL> select ename,sal,comm,sal+nvl(comm,0) from emp where ename='SMITH';

ENAME SAL COMM SAL+NVL(COMM,0)


---------- ---------- ---------- ---------------
SMITH 900 900

SQL> select nvl(null,0) from dual;

NVL(NULL,0)
-----------
0

SQL> select 900+0 from dual;

900+0
----------
900

nvl2():
oracle 9i introduced nvl2(),this function accepts
3 perameters

syntax:
nvl2(exp1,exp2,exp3);

SQL> select nvl2(10,20,30) from dual;


NVL2(10,20,30)
--------------
20

SQL> select nvl2(null,20,30) from dual;

NVL2(NULL,20,30)
----------------
30

update the employee comm with 500 if the comm is null


otherwise add 500 to comm

cond1:
1.if comm is null then update comm to null
2.if comm is not null then update comm to comm+500

SQL> update emp


2 set comm=nvl2(comm,comm+500,500);

special operators:
in-not in
between-not between
is null-is not null
like-not like

in:
it is used to pick the values one by one
syntax:
select * from tablename where columnname in(list of values);

SQL> select * from emp where ename in('SMITH','ALLEN','MARTIN');

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1000 300
30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7369 SMITH CLERK 7902 17-DEC-80 900
20

note:
in all databases not in operator doesnot work with
null values

SQL> select * from emp where ename not in('SMITH','ALLEN',null);

no rows selected

You might also like