part 3.

set operators
The set operaotrs combine the result of two or more
component queries into one result.
queries containing set operators are called
compound queries.
different types of set operators are
union operator
union all operator
intersect operator
Minus operator
note: whenever these operators are used select
must have equal no of columns
similar data type columns.
the generic syntax:
<comonent query>
{union|union all|minus|intersect}
<component query>
union operator to return all rows from multiple
and eliminate any duplicate rows.
o/p stored in default ascending order
>select job from emp
where deptno=10
select job
from emp
where deptno=20;
union all:
combines the result of two select statement into
result set including the duplicates.
select deptno,job
from emp
where deptno=10
union all
select deptno,job
from emp
where deptno=20;
use the intersect operator to return all rows
common to multiple quires.
select job from emp
where deptno=10
select job
from emp
where deptno=20;
note: intersect does not ignore null values.
the minus operator returns rows from the first query
that are not present in the
second query.
select deptno,job
from emp
where deptno=10
select deptno,job
from emp
where deptno=20;
note: the quries are all executed independently but their output
is merged.
only final query ends with a semicolon.
privileges are the right to execute particular SQL Stmts.
system privileges
object privileges
a schema is a collection of objects, such as
tables,views,and sequences.
a role is a named group of related privileges
that cane be granted to the user.
working with synonyms
It is a database object, which acts as an alternate
for an existing object next to view.
Use: with synonym u can
easy referring to a table owned by another user.
simply access to objects by creating a synonym.
DML, Description, select allowed on synonyms
user_synonyms tables holds details of synonyms
Two types of synonyms:
Private synonym
public synonym
private synonym:
created by user
used by specific users which have permission
create synonym<synonym_name>for table name
create synonym empdept for emp
>desc empdept
>grant insert,update,delete on empdept to userb;
>insert into empdept values(
>select *from scott.empdept;
>delete from scott.empdept where deptno=20;
>update scott.empdate job='man' where
• select *from empdept;
• what are done in userb all are working here also.
• create synonym prabhu for scott.empdate
• >select *from userb.prabhu;
• >delete
• >update
Public synonym:
• created by database administrators
• w/o using owner name u can access the
permission is called Public.
• we should have create public synonym
privilige,and it can accessed by all users.
• syntax: create <public synonym> <synonym
name>for <schemaname.tablename>
• create public synonym empsdept for scott.emp;
• grant select,delete,update on empsdept to
• select *from empsdept
• insert,update,delete done
conn userc:
• same above work done.
diff b/n synonym and views:
• it is created for table, view
• procedure,function,pacage
be created on
view can
table and view.
• it is used for hiding the information
• of username,objectname
• it takes minimum space only.
• index is a schema object, which a pointer
locates the physical address of data.
• Index used by the oracle server to speed up the
retrieval, manipulated of rows.
• Data placed in hard disk Randomly where the
data is stored that address identified by index.
Index creation is Two types:
• index can be created explicitly or Automatically.
I) Automatic:
A unique index is created Automatically when you
define a primary key or unique constraint in a table
II) Manual;
• users can create nonunique indexes on columns to
speed up access to the rows.
when u drop the index column or table
corresponding indexes are also dropped.
and disable the pkey or drop the pkey the related
index will be dropped automatically.
one table more than one index can be created.
when to create an index:
• A column contains a wide range values.
• A column contains a large number of null values
• the column is used frequently in the where
clause or join condition.
when not to create an index:
• the table is small
• the table is updated frequently.
Types of indexes:
Normal indexes
Bitmap indexes
function based
unique index
Normal index:
• create index salidx on emp(sal);
Bitmap index:
• index has to be create with a bitmap for each distinct
• Bit map indexes store the rowids
• bitmap indexes should be used only when the data is
frequently updated
• the oracle optimizer can dynamically convert bitmap
indexes to rowid's during the query processing.
>create bitmap index bitmap job on emp(job);
note: cannot specify both unique and bitmap
Composite Index:
• if we define a index on more than one column,it is called
>create unique index eno_ename_cinx on emp(empno,ename);
Function Based Index:
• when we create index on column with function it is called
>create index upper_dept_name_idx on dept(upper (dname));
>select *from dept
where upper(dname)='sales';
creating unique indexes
>create unique index eno_unq_idx on emp
Pseudo columns:
• pseudo columns behave like a Table column but is not
actual stored in a table.
• psudo columns only select can be implemented but
insert,update,delete cannot perform.
Available pseudo columns:
currval: Returns the Current value of sequence
Nextval: Increment the sequence and Returns the next value.
• Therse two clause can be used only
select stmt,insert stmt and update stmt.
cannot used in sub query
group by
order by
set operators,
syntax: sequencename.<currvalue>
Sequence :
• A sequence is a schema object that can be genarate unique sequential
• the sequence values are often used for primary key's and unique keys.
• purpose: can be used to generate pkey values automatically.
• create sequence sequnce name
increment by n
Start with n
[maxvalue n/no maxvalue n]
[minvalue/no minvalue]
[cycle/no cycle]
order/no order;
eg: create table tdept
• (
deptno number(4) constraint tdeptno_pk primary key,
dname varchar2(14),
dloc varchar2(10)
• create sequence tdept_deptno_seq
increment by 10
Start wirh 10
maxvalue 1000
minvalue 0
• insert into tdept
• select *from tdept;
note : when ever using cycle u must mention the no cycle.
• sequence is not depends on the table.
modifying sequence:
• the alter command can be used to change the present status of
• except startwith remaining all in syntax can be eaisly modified.
note: maxvalue is less than the current sequence number is not
how to see the current sequence
• select sequence .currval from dual;
Removing sequence:
• drop sequence<seqname>
• Drop any sequence privilige to remove it of owner
• Grant Drop any sequence to user a
Full information about sequences
• Desc user_sequences
information of sequences are stored is
• desc user_objects
hierarchical queries:
• these queries that are executed upon tables that
contain data.
Start with: it specifies the root of the hierarchical.
Connect by: it is used to specify the relation ship b/n
parent rows and child rows of the hierarchy.
where: it is used to restrict the rows returned by the
eg: select ename,empno,mgr,job from emp
startwith sal=5000
connect by prior empno=mgr
Pseudo columns:
• pseudo columns behave like a Table column but is not actual
stored in a table.
• psudo columns only select can be implemented but
insert,update,delete cannot perform.
Available psudo columns:
currval: Returns the Current value of sequence
Nextval: Increment the sequence and Returns the next value.
• Therse two clause can be used only
select stmt, insert stmt and update stmt.
cannot used in subquery
group by
order by
set operators,viwes.
syntax: sequencename.<currvalue>;
• we cannot use level pseudo column w/o connect by clause.
select nth highest:
• select level, max(sal)
from emp
where level=&levelno
connect by prior sal>sal
Group by level;
select nth lowest:
The level pseudo column returns Root row child of root
• select level,ename,empno from emp
startwith job='president'
connect by prior empno=mgr
order by level;
• the oracle engine assign a rownum value once retrive
data from a table
• The first rows select has rownum of 1,--• it is temporaly.
• it can be used at select
group function
eg: select rownum from emp
• select rownum,empno,ename,sal,deptno from emp;
Top salaries:
• select Rownum,empno,ename,sal
from(select *from emp order by sal desc)
group by Rownum,empno,ename,sal
having Rownum='&n';
• having rownum <='&n'
• having Rownum=1
direct 2nd max sal
• select max(sal) from emp
where sal<(select max(sal) from emp);
direct max sal
• select ename,job,sal
from emp
where sal=(select max(sal)
from emp);
direct min sal:
Rowid is an exact physical address of row.
• Rowid is used by oracle to locate any row
• Rowid 's are unique
• Rowid can never be inserted,updated,deleted
• Rowid used in select stmt,where clause, group
by clause.
Select rowid from emp
select rownum,rowid,ename,sal,deptno from
Decode function:
• the decode function can be used to expand any
abbreviation used in the table.
• the function wors on the same priciple as the
syntax: select decode
(<colmn name/value>,<coded value>,<decoded
from<table name>;
Eg:>select decode (deptno
10, 'accounting',
'other') departments from emp;
Working with case:
• the case expression can be used to perform if_then_else logic in sql
• it can be used even for executing conditions on range based comperision
>case search_expr
when expr1 then result 1
when expr 2 then result 2
else default_result
Eg:>select ename,sal,
when sal>=800 and sal<=2000 then 'lowest pay'
when sal>=2001 and sal<=4000 then 'moderatepay'
else 'highpay'
from emp;
nulls first/nulls last:
null values behave take highest values priority compare to not null value
• eg:select ename,comm from emp
order by comm;
note: the default is nulls last
nulls first:
• select comm,rank()
over(order by comm desc nulls first)
from emp;
nulls last:
• select comm,rank()
over(order by comm desc nulls last)
from emp
where comm is not null;
Ranking with partition:
• select ename,deptno,sal
over(partition by deptno order by sal desc)
from emp;
• select deptno,ename,sal ,sum(sal)
over(partition by deptno o
order by ename
rows 1 preceding) "sliding total"
from emp;