Sei sulla pagina 1di 45

Oracle - Tips and Tricks

B.Bhuvaneswaran
Assistant Professor (SS)
Department of Computer Science & Engineering
Rajalakshmi Engineering College
Thandalam
Chennai 602 105
bhuvaneswaran@rajalakshmi.edu.in

Rename Column In Table

ALTER TABLE table_name


RENAME COLUMN old_name to
new_name;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Rename Table

ALTER TABLE table_name


RENAME TO new_table_name;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Create Table - By Copying All Columns


From Another Table

CREATE TABLE new_table AS


(SELECT * FROM old_table);

B.Bhuvaneswaran / AP (SS) / CSE / REC

Create Table - By Copying Selected


Columns From Another Table

CREATE TABLE new_table AS


(SELECT column_1, column2, ... column_n
FROM old_table);

B.Bhuvaneswaran / AP (SS) / CSE / REC

Create Table - By Copying Selected


Columns From Multiple Tables

CREATE TABLE new_table AS


(SELECT column_1, column2, ... column_n
FROM old_table_1, old_table_2, ...
old_table_n);

B.Bhuvaneswaran / AP (SS) / CSE / REC

Frequently Asked Questions


How can I create a SQL table from
another table without copying any values
from the old table?
CREATE TABLE new_table AS
(SELECT * FROM old_table WHERE 1=2);

B.Bhuvaneswaran / AP (SS) / CSE / REC

Selecting First 5 Records From A Table

SELECT * FROM table_name WHERE


ROWNUM <= 5;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Diff. B/w ROWNUM Pseudo Column


and ROW_NUMBER() Function
ROWNUM is a pseudo column present in
Oracle database returned result set prior
to ORDER BY being evaluated. So ORDER
BY ROWNUM does not work.
ROW_NUMBER() is an analytical function
which is used in conjunction to OVER()
clause wherein we can specify ORDER BY
and also PARTITION BY columns.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Example

SELECT name, sal, row_number()


over(order by sal desc) rownum_by_sal
FROM EMPLOYEE o

B.Bhuvaneswaran / AP (SS) / CSE / REC

Rank Function

The RANK function can be used two ways

as an Aggregate function or
as an Analytic function.

B.Bhuvaneswaran / AP (SS) / CSE / REC

As An Aggregate Function

RANK( expr1 [, expr2, ... expr_n ] )


WITHIN GROUP ( ORDER BY expr1
[, expr_2, ... expr_n ] )

B.Bhuvaneswaran / AP (SS) / CSE / REC

Example

select RANK(1000, 500) WITHIN GROUP


(ORDER BY salary, bonus) from
employees;

B.Bhuvaneswaran / AP (SS) / CSE / REC

As An Analytic Function

rank() OVER ( [ query_partition_clause]


ORDER BY clause )

B.Bhuvaneswaran / AP (SS) / CSE / REC

Example

select employee_name, salary, RANK()


OVER (PARTITION BY department ORDER
BY salary) from employees where
department = 'Marketing';

B.Bhuvaneswaran / AP (SS) / CSE / REC

Dense_rank Function

The DENSE_RANK function can be used


two ways

as an Aggregate function or
as an Analytic function.

B.Bhuvaneswaran / AP (SS) / CSE / REC

As An Aggregate Function

DENSE_RANK( expression1, ...


expression_n ) WITHIN GROUP ( ORDER
BY expression1, ... expression_n )

B.Bhuvaneswaran / AP (SS) / CSE / REC

Example

select DENSE_RANK(1000, 500) WITHIN


GROUP (ORDER BY salary, bonus) from
employees;

B.Bhuvaneswaran / AP (SS) / CSE / REC

As An Analytic Function

DENSE_RANK() OVER
( [ query_partition_clause] ORDER BY
clause )

B.Bhuvaneswaran / AP (SS) / CSE / REC

Example

select employee_name, salary,


DENSE_RANK() OVER (PARTITION BY
department ORDER BY salary) from
employees where department =
'Marketing';

B.Bhuvaneswaran / AP (SS) / CSE / REC

Decode Function

DECODE( expression , search , result


[, search , result]... [, default] )

B.Bhuvaneswaran / AP (SS) / CSE / REC

Example

SELECT supplier_name,
DECODE(supplier_id, 10000, 'IBM',
10001, 'Microsoft',
10002, 'Hewlett Packard', 'Gateway')
result FROM suppliers;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Regexp_like Condition

REGEXP_LIKE ( expression, pattern


[, match_parameter ] )

B.Bhuvaneswaran / AP (SS) / CSE / REC

Match On More Than One Alternative

SELECT last_name FROM contacts


WHERE REGEXP_LIKE (last_name,
'Anders(o|e|a)n');

B.Bhuvaneswaran / AP (SS) / CSE / REC

Match On Beginning

SELECT last_name FROM contacts


WHERE REGEXP_LIKE (last_name,
'^A(*)');

B.Bhuvaneswaran / AP (SS) / CSE / REC

Match On End

SELECT last_name FROM contacts


WHERE REGEXP_LIKE (last_name,
'(*)n$');

B.Bhuvaneswaran / AP (SS) / CSE / REC

Sequences (Autonumber)

CREATE SEQUENCE sequence_name


MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Example

CREATE SEQUENCE supplier_seq


MINVALUE 1
MAXVALUE
999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Example

INSERT INTO suppliers


(supplier_id, supplier_name)
VALUES
(supplier_seq.NEXTVAL, 'Kraft Foods');

B.Bhuvaneswaran / AP (SS) / CSE / REC

Dual Table
The DUAL is special one row, one column
table present by default in all Oracle
databases.
The owner of DUAL is SYS (SYS owns the
data dictionary, therefore DUAL is part of
the data dictionary.) but DUAL can be
accessed by every user.
The table has a single VARCHAR2(1)
column called DUMMY that has a value of
'X'.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Examples
DESC DUAL;
SELECT * FROM DUAL;
SELECT COUNT(*) FROM DUAL;
SELECT 'ABCDEF12345' FROM DUAL;
SELECT 123792.52 FROM DUAL;
SELECT sysdate FROM DUAL ;
SELECT 15+10-5*5/5 FROM DUAL;
SELECT level FROM DUAL CONNECT BY
level <=10;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Select TOP 2 salary from employee table

select * from (select * from employee


order by SALARY desc) where rownum
<=2;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Select 2nd Highest salary from employee


table

select min(salary) from (select * from


(select * from employee order by SALARY
desc) where rownum <= 2)

B.Bhuvaneswaran / AP (SS) / CSE / REC

To fetch ALTERNATE records from a


table. (EVEN NUMBERED)

select * from emp where rowid in (select


decode(mod(rownum,2),0,rowid, null)
from emp);

B.Bhuvaneswaran / AP (SS) / CSE / REC

To select ALTERNATE records from a


table. (ODD NUMBERED)

select * from emp where rowid in (select


decode(mod(rownum,2),0,null ,rowid)
from emp);

B.Bhuvaneswaran / AP (SS) / CSE / REC

Find the 3rd MAX salary in the emp


table

select distinct sal from emp e1 where 3 =


(select count(distinct sal) from emp e2
where e1.sal <= e2.sal);

B.Bhuvaneswaran / AP (SS) / CSE / REC

Find the 3rd MIN salary in the emp


table.

select distinct sal from emp e1 where 3 =


(select count(distinct sal) from emp
e2where e1.sal >= e2.sal);

B.Bhuvaneswaran / AP (SS) / CSE / REC

Select FIRST n records from a table

select * from emp where rownum <= &n;

B.Bhuvaneswaran / AP (SS) / CSE / REC

Select LAST n records from a table

select * from emp minus select * from


emp where rownum <= (select count(*) &n from emp);

B.Bhuvaneswaran / AP (SS) / CSE / REC

Select DISTINCT RECORDS from


emp table.

select * from emp a where rowid =


(select max(rowid) from emp b where
a.empno=b.empno);

B.Bhuvaneswaran / AP (SS) / CSE / REC

How to delete duplicate rows in a table?

delete from emp a where rowid != (select


max(rowid) from emp b where
a.empno=b.empno);

B.Bhuvaneswaran / AP (SS) / CSE / REC

B.Bhuvaneswaran / AP (SS) / CSE / REC

References

IBM DB2 9.7 - Academic Workshop Course Workbook.

B.Bhuvaneswaran / AP (SS) / CSE / REC

References

http://www.techonthenet.com

B.Bhuvaneswaran / AP (SS) / CSE / REC

B.Bhuvaneswaran / AP (SS) / CSE / REC

Potrebbero piacerti anche