You are on page 1of 2

Decode Function:

--> It is a single row function.


--> The function works on the principle of "IF-THEN-ELSE".

Ex:

SQL> select
empno,ename,sal,deptno,decode(deptno,10,'ACCOUNTING',20,'RESEARCH',30,'SALES','OTHE
RS') Dname from emp;

Case Expression:

--> The case expression can be used to perform "IF-THEN-ELSE".


--> Case is similar to decode but it is ANSI-Based.

Case Expression Are of Two Types:


--> Simple Case expression
--> Searched Case expression

Simple Case:

SQL> select empno,ename,sal,deptno,case deptno when 10 then 'ACCOUNTING' when 20


then 'RESEARCH' when 30 then 'SALES' else 'OTHERS'
end dname from emp;

Searched case:

SQL> select empno,ename,sal,deptno,case when deptno = 10 then 'ACCOUNTING' when


deptno = 20 then 'RESEARCH'
when deptno = 30 then 'SALES' else 'OTHERS' end from emp;

Q) Nth highest salary by using dense_rank function?

SQL> select * from (select emp.*,dense_rank() over (order by sal desc) R from emp)
where r= &n;

In case of rank it will give same rank to any duplicate value but the value comes
after that will not start with next rank.

In case of dense_rank it will give same rank to any duplicate value but the value
comes after that will start with next rank.

Comment the line in SQL statement:

SQL> select * from emp where deptno=20/*only display dept 20 detail..*/; (In this
way we can comment part of a line)
SQL> select * from emp where deptno=20
2 --display dept 20 details..; (In this way we can comment whole line)

SQL> select * from emp where deptno=20


/*and sal > 2000
and job = 'ANALYST'*/; (In this way we can comment multiple lines)

Alter/Rename the table structure:


1) Add a new column:

SQL> alter table student add address varchar2(4);

2) Modify an existing column:

SQL> alter table student modify address varchar2(40);

3) Rename a column:

SQL> alter table student rename column address to location;

4) Remove a column:

SQL> alter table student drop column location;

5) Rename a table:

SQL> rename student to students;

6) Remove a table:

SQL> drop table <table_name>;

You might also like