Anda di halaman 1dari 5

Nth salary : FOR DELETING DUPLICATE ROWS

select sal
from emp a delete from <table_name> a
where &n=(select count(sal)
where rowid not in(select max(rowid)
from emp b from <table_name> b
where a.sal<=b.sal);
where a.column_name=b.column_name;

Max two salaries : count dulicate vales:


select sal
from emp a select sal,count(sal)
where &n >= (select count ( sal) from emp
from emp b group by sal
where a.sal <= b.sal); having count(sal)>1 ;

Max or min average salary with second highest max salary


depatment ID:
select max(sal)
select deptno,avg(sal) from emp
from emp where sal < (select max(sal)
group by deptno from emp);
having avg(sal)= (select min(avg(sal))
from emp
group by deptno);

Details of highest salary earner Details of lowest salary earner

select *
from emp select *
where sal = (select max(sal) from emp
from emp); where sal = (select min(sal)
from emp)

Using Group by Using Order by

select deptno,sum(sal) select * from emp


from emp order by sal;
group by deptno;
DISTINCT Counts

select distinct(job) select job, count(job)


from emp; from emp
group by job;

Table Structure Creation of table

desc orders create table orders (sal number,lastname


varchar2(10));

Insertion of records Adding Column to Table

insert into orders values (&sal,'&lastname'); alter table orders add (job varchar2(10) );

To Modify Column Definition To Drop a Column from a Table

alter table orders modify (job number); alter table orders drop column job;

To Unused a Column Using NVL Function (Replaces Null


value with given value)
alter table orders set unused (firstname);
select empno, ename, sal,nvl (comm, 200)
from emp;

select ename, sal,


nvl(to_char(comm),'commission not payed')
from emp;
SELECT ABS(10) FROM DUAL; SELECT LEAST(4,7,3,5,9,2) FROM DUAL;

ABS(10) LEAST(4,7,3,5,9,2)
---------- ------------------
10 2
SELECT GREATEST(4,7,3,5,9,2) FROM SELECT TRUNC(1.23456,3) FROM DUAL;
DUAL;
GREATEST(4,7,3,5,9,2) TRUNC(1.23456,3)
--------------------- ----------------
9 1.234
SELECT TRUNC(1.23456) FROM DUAL; SELECT SQRT(9) FROM DUAL;

TRUNC(1.23456) SQRT(9)
-------------- ----------
1 3

SELECT ROUND(1.23456,3) FROM SELECT ROUND(1.23456) FROM DUAL;


DUAL;
ROUND(1.23456)
ROUND(1.23456,3) --------------
---------------- 1
1.235
SELECT POWER(10,2) FROM DUAL; SELECT MOD(10,3) FROM DUAL;

POWER(10,2) MOD(10,3)
----------- ----------
100 1

SELECT LOG(100,10) FROM DUAL; SELECT LN(10) FROM DUAL;


LOG(100,10) LN(10)
----------- ----------
.5 2.30258509
SQL> SELECT LOG(10,100) FROM DUAL;
LOG(10,100)
-----------
2
SELECT FLOOR(123.456) FROM DUAL; SELECT ABS(-10) FROM DUAL;
ABS(-10)
FLOOR(123.456) ----------
-------------- 10
123
SELECT CEIL(123.456) FROM DUAL; To diplay those departments which have more
than 5 employees in them we can write.
CEIL(123.456) SELECT DEPTNO, COUNT(*)
------------- FROM EMP
124 GROUP BY DEPTNO
HAVING COUNT(*) > 5 ;
SELECT ASCII('A') FROM DUAL; SELECT TRANSLATE('JOHN','H','N') FROM
DUAL;
ASCII('A')
---------- TRAN
65 ----
JONN
SELECT LTRIM('GEORGE BUSH', SELECT RTRIM('TONY BLAIR', 'AIR') FROM
'GEORGE') FROM DUAL; DUAL;

LTRIM RTRIM('
----- -------
BUSH TONY BL
SELECT LPAD('BCD',5,'A') FROM DUAL; SELECT RPAD('BILL ' , 12 , 'CLINTON') FROM
LPAD( DUAL;
-----
AABCD RPAD('BILL',
------------
BILL CLINTON
SELECT * FROM DUAL; SELECT LPAD('BCD',4,'A') FROM DUAL;

DUMMY LPAD
- ----
X ABCD
DESC DUAL
SELECT INSTR('TRYING TO KEEP THE
THINGS AS SIMPLE AS Name Null? Type
POSSIBLE','AS',1,2) FROM DUAL; ----------------------------------------- --------
INSTR( DUMMY VARCHAR2(1)
------------------------
37
SELECT SUBSTR('ABCDEF',1,3) FROM SELECT INSTR('TRYING TO KEEP THE
DUAL; THINGS AS SIMPLE AS POSSIBLE','AS') FROM
SUB DUAL;
--- 27
ABC
SELECT LENGTH('AB CD') FROM DUAL; SELECT SUBSTR('ABCDEFG',2,3) FROM DUAL
SUB
LENGTH('ABCD') ---
-------------- BCD
5
SELECT INITCAP('ABCDEF') FROM SELECT LENGTH('ABCD') FROM DUAL;
DUAL;
LENGTH('ABCD')
INITCA --------------
------ 4
Abcdef
SELECT LOWER('ABCDEfg') FROM DUAL;
SELECT UPPER('abcDEfg') FROM DUAL; LOWER('
UPPER(' -------
------- abcdefg
ABCDEFG
SELECT USER FROM DUAL; SELECT SYSDATE FROM DUAL;

USER SYSDATE
------- ---------
SCOTT 1-JAN-05

SELECT TO_DATE('MAR 05 01','MON YY SELECT TO_DATE('01/01/05',


DD') FROM DUAL; 'DD/MM/YY') FROM DUAL;

TO_DATE(' TO_DATE('
--------- ---------
01-MAR-05 01-JAN-05

SELECT NEXT_DAY('01-JAN-05','FRI') SELECT TO_DATE('01 JANUARY


FROM DUAL; 2005','DD MONTH YYYY') FROM DUAL;

NEXT_DAY( (Next TO_DATE('


Friday after 1-jan-05 is on 7-jan-05) ---------
--------- 01-JAN-05
07-JAN-05
SELECT MONTHS_BETWEEN('31-DEC- SELECT MONTHS_BETWEEN('31-JUL-
05','1-JAN-05') FROM DUAL 05','1-JUL-05') FROM DUAL

MONTHS_BETWEEN('31-DEC-05','1-JAN- MONTHS_BETWEEN('31-JUL-05','1-JUL-05')
05') --------------------------------------
-------------------------------------- .967741935
11.9677419
SELECT ADD_MONTHS('1-JAN-05',5) SELECT LAST_DAY('1-JAN-05') FROM
FROM DUAL; DUAL;

ADD_MONTHS LAST_DAY(
------------------ ---------
01-JUN-05 31-JAN-05