Vous êtes sur la page 1sur 18

1. When do we prefer to create the materialized view?

2. In data warehouses, you can use materialized views to precompute and


store aggregated data such as the sum of sales. Materialized views in
these environments are often referred to as summaries, because they
store summarized data. They can also be used to precompute joins with or
without aggregations. A materialized view eliminates the overhead
associated with expensive joins and aggregations for a large or important
class of queries.
3.
4. How to convert the columns into rows? Write the query for the same?
5. select store_id, case pivot
6. when 1 then q1
7. when 2 then q2
8. when 3 then q3
9. when 4 then q4
10.end quarter, How to remove the duplicate records in table?
11.delete from <tablename> where rowid not in (select max(rowid) from
<tablename> group by col1,col2);
12.How to display the duplicate records in a table?
13.select * from <tablename> where rowid not in (select max(rowid) from
<tablename> group by col1,col2);
14.select deptno,count(*) from <tablename> group by deptno having
count(*)>1
15.select * from (select deptno,count(*) cnt from <tablename> group by
deptno) where cnt>1
16.How to take a table back up?
17.create table bkp_tab as select * from <tablename>
18.How to copy a structure of a table without loading the data?
19.create table <copy table> as select * from <tablename> where 1=2
20.How to rename table?
21.rename <old table name > to <new table name>
22.How to check whether a column has character data or not?
23.select decode(trim(translate(col1,-1234567890,.,
)),0,null,num,char)=char
24.How to display 1 to 100 numbers?

25.a) select * from <tablename> where rownum<=100


26.b)select * from (select e.*, rownum rno from <tablename>e) where rno
between 1 and 100
27.What is difference between primary key and unique key?
28.primary key: it is not allow duplicates and nulls
29.unique key: it is also not allow the duplicates but it allows nulls
30.What is difference between B-Tree and Bitmap index?
31.B-tree: column with highest cardinality
32.Bitmap: column with lowest cardinality
33.What is difference between clusters and non-clustered index
34.clusters: it is created on primary key column and only one cluster can be
create on one table
35.Non-clustered: it will be create more than one non-clustered index(appr
250)
36.What is the difference between truncate and delete?
37.truncate: 1)it is DDL command
38.

2) it is faster bcz it dont have back up

39.

3) it is delete the data permanently bcz it is auto save

40.delete: 1) it is DML command


41.

2) it is slow bcz it have back up

42.

3) it will delete the data conditionally

43.What is the case and decode?


44.case: we can use for range(<,>)
45.decode: we can not use for range
46.What is difference between Translate and Replace?
47.translate:it will translate set of the characters
48.replace:it will replace character by characters
49.What is the case and nested if statement?
50.What is difference between nvl and Coalesce?
51.nvl: it replace the null with zero
52.coalesce:it will display the first not null column
53.

54.What is check constraint?


55.it is used to check the range in a column
56.syn:create table tablename (id number,
57.
58.

name varchar2(20),
marks number(10) check(marks>=35));

59.What is default constraint? When do we use it?


60.What is referential integrity?
61.As for referential integrity foreign key in the child table must be primary
key in the parent table
62.What is normalization?
63.eliminating the data redundancy is called normalization
64.What are different normal forms? Give examples?
65.normal form-1
66.normal form-2
67. normal form-3
68.How to display alternative records in a table?
69.select * from (select e.*, rownum rno from <table name>e) where
mod(rno,2)=0
70.How to display top 5 salaries from emp table?
71.select * from (select e.*,dense_rank() over(order by sal desc) rnk from
<tablename>e) where rnk<=5
72.select * from (select e.*,rank() over(order by sal desc) rnk from
<tablename>e) where rnk<=5
73.select * from (select e.*,row_number() over(order by sal desc) rno from
<tablename>e) where rno<=5
74.How display top 3 salaries in each department?
75.select * from (select e.*,dense_rank() over(partition by deptno order by sal
desc) rnk from <tablename>e) where rnk<=3
76.select * from (select e.*,rank() over(partition by deptno order by sal desc)
rnk from <tablename>e) where rnk<=3
77.select * from (select e.*,row_number() over(partition by deptno order by
sal desc) rno from <tablename>e) where rno<=3
78.
79.How to display last 5 records from EMP?

80.a) select * from(select e.*,rownum rno from emp e) where rno>(select


count(*)-5 from emp)
81.b) select * from(select e.*,row_number over(order by rowid) rno from emp
e) where rno>(select count(*)-5 from emp)
82.
83.What is the difference between Rownum and Rowid?
84.rownum: it display number when ever a select query exhibits it generates
a sequence number start from 1
85.rowid: it will stored in data base uniquely in each records (it means no two
records will have the same rowid)
86.What are difference between Rank and Dense Rank?
87.rank:
88.What are different pseudo columns?
89.rownum,rowid,level,user,currval,nextval,timestamp,sysdate
90.How to find the 5th Max salary EMP without using the rank function?
91.select * from (select e.*, row_number() over(order by sal desc) rno from
emp e) where rno=5
92.select e.* from emp e where 5=(select count(distinct a.sal) from emp a
where e.sal<=a.sal)
93.How to remove the spaces in all columns and update?
94.a)update <tablename> set column=trim(column) where
length(column)<>length(trim(column))
95.b)update <tablename> set column=trim(column) where instr(column,
,1,1)>0
96.How to identify wheat her a column is character or not?
97.select decode(trim(translate(col1,-1234567890,.,
)),0,null,num,char)=char
98.How to provide select, insert, delete priviliges on table to user?
99.grant select,insert,delete on <tablename> to <tablename1>
100.

How to display from 5-10 rows in emp table?

101.
select * from (select e.*, rownum rno from emp e) where rno
between 5 and 10
102.

select * from emp where rownum<=10

103.

minus

104.

select * from emp where rownum<=4

105.
select * from (select e.*, rownum rno from emp e) where rno
in(5,6,7,8,9,10)
106.
107.

How to find the next month first date?

108.

select add_months(trunc(sysdate,mm),1) from dual;

109.

select trunc(sysdate,mm) + interval 1 month from dual

110.
How to convert the type3 dimension table to type2 dimension?
Write the query for the same?
111.

What are advantages and disadvantages of view?

112.

advantages:

113.

view the data without storing the data into the object

114.

restrict the view of a table i.e can hide some of columns in the table

115.

join two or more table show it has one object to user

116.
restrict the access of a table so that no body can insert into the
table
117.

disadvantages:

118.

dml operation not possible if that is more than one table

119.

when table is not there view will not work

120.

it is also data base object so it will occupies the space

121.

Difference between view and materialized view?

122.

view:

123.

it is a logical object ,it does nt contain the data

124.

we can nt perform dml operation on complex view

125.

when we do select * from view it will fetch the data from base tables

126.

in view we cant schedule to refresh

127.

materialized view:

128.

it has the physical existence

129.

we can perform dml operation on materialized view

130.
when we do select * from materialized view it fetches the data from
materialized view
131.
132.

materialized view we can schedule for refresh

133.
134.
135.
136.

case pivot

137.

when 1 then s.q1

138.

when 2 then s.q2

139.

when 3 then s.q3

140.

when 4 then s.q4

141.

end sales_val

142.

from sales s,

143.

(select rownum pivot from dual connect by level<=4) order by 1,2

144.

How to convert rows into columns? Give an example?

145.

select store_id,max(decode(rno,1,sales_val))q1,

146.

max(decode(rno,2,sales_val))q2,

147.

max(decode(rno,3,sales_val))q3,

148.

max(decode(rno,0,sales_val))q4 from

149.
(select store_id,quarter,sales_val,mod(rownum,4) rno from
<tablename> order by store_id,quarter) group by store_id
150.
How to display employees whose salary greater than average salary
of department?
151.

select e.* from emp e,

152.
(select deptno,avg(sal) avg_sal from emp group by deptno)a where
e.deptno=a.deptno and e.sal> a.avg_sal
153.
select o.* from emp o where o.sal >(select avg(p.sal) from emp p
where p.deptno=o.deptno group by p.deptno)
154.

When do you use where clause and having clause?

155.

To filter the data for row wise we will use where clause

156.

To filter the data for group wise we will use having clause

157.
When can we say two tables are identical data? Write a query for
the same?
158.

select * from A

159.

minus

160.

select * from B

161.
162.

select * from B

163.

minus

164.

select * from A

165.
Write a query to identify the employee records that have spaces in
ENAME field?
166.

select ename,instr(ename, ,1) from emp

167.

What is the difference between substr and instr?

168.

substr will fetch the string and instr will find the position of string

169.

What are set commands? Give example?

170.

union,union all,intersect,minus

171.

How to null values last?

172.

select * from <tablename> order by column desc nulls last

173.

How to find the department wise second maximum salary?

174.
select * from (select e.*, dense_rank() over(partition by deptno
order by sal desc) rnk from emp e) where rnk=2
175.
select * from (select e.*, rank() over(partition by deptno order by sal
desc) rnk from emp e) where rnk=2
176.
select * from (select e.*, row_number() over(partition by deptno
order by sal desc) rno from emp e) where rno=2
177.

What is cross join?

178.

It will join the two table and multiply the two table rows

179.
ex: if A table as 5 rows and B table as 4 rows then if u do the cross
join it multiple the two table rows i.e 5*4=20
180.

What different types of Joins?

181.

1) left outer join

182.

2) right outer join

183.

3) cross join

184.

4) self join

185.

5) equi join

186.

What is difference left outer and Right outer and full outer joins?

187.

What is the difference between sub query and correlated sub query?

188.

How to find the nth maximum salary from EMP table?

189.
select e.* from emp e where n=(select count(distinct a.sal) from
emp a where e.sal<=a.sal
190.
How to find ENAME columns has a character A or not in the emp
table?
191.

select ename from emp where upper(ename) like %A%

192.

How to find how many A are there in ENAME field in EMP table?

193.

select ename, length(ename)-length(replace(ename,A))

194.

How to display departments having employee count greater than 3?

195.
select deptno,count(*) from emp group by deptno having
count(*)>3
196.

How to find the ENAME field has spaces or not?

197.

select * from emp where instr(ename, ,1)>0

198.

How to display middle record in table?

199.

select * from (

200.

select e.*, rownum rno from emp e) where rno in(

201.
emp

select decode(mod(count(*),2),0,count(*)/2,ceil(count(*)+1)/2) from

202.

union

203.

select decode(mod(count(*),2),0,(count(*)+2)/2) from emp)

204.

How to display employee and manager names from EMP table?

205.
select e.ename,m.ename from emp e,emp m where
e.mgr=m.empno
206.
select e.ename,m.ename from emp e left outer join emp m on
e.mgr=m.empno
207.

How to display prime numbers between 1 and 100?

208.

select l prime_num from

209.

(select rownum l from dual connect by level<=100),

210.

(select rownum m from dual connect by level<=100)

211.
where m<=l group by l having count(case l/m when trunk(l/m) then
y end)=2
212.
How to display O, OR, ORA, ORAC, ORACL, ORACLE in different rows
from ORACLE in the same order?
213.

select lpad(oracle,level,oracle) from dual connect by level<=6

214.
How to count the number of rows in table without using count
function?
215.

select max(rno) from (select e.*, rownum rno from emp e)

216.

How to display first 5 records in table?

217.

select * from emp where rownum<=5

218.

How to display employee records having same salary?

219.

How do you find the difference between 2 tables A and B?

220.

select * from A

221.

minus

222.

select * from B

223.
224.

select * from B

225.

minus

226.

select * from A

227.

How to find the employees who joined in the LAST 5 years?

228.
select ename from emp where to_char(sysdate,yyyy)to_char(hiredate,yyyy)<=5
229.

How to find the different jobs in each department?

230.

select deptno,job from emp group by deptno,job ;

231.
How do you find the Last 5 characters from ENAME column of EMP
table?
232.

select ename,substr(ename,-5,5) from emp;

233.

How do you find the 5th, 10th, 15th, 20th, rows from a table?

234.
select * from (select e.*, rownum rno from emp e) where
mod(rno,5)=0
235.
select * from (select e.*, row_number() over(order by rowid) rno
from emp e) where mod(rno,5)=0
236.
Write a query to load the first 10 records to table T1 and second 10
records to t2 and next 10 records to t3 table and next 10 records to table
t1 etc
237.

insert into T1

238.
select * from(select e.*, rownum rno from emp e) where
mod(rno,30) between 1 and 10;
239.

240.

insert into T2

241.
select * from(select e.*, rownum rno from emp e) where
mod(rno,30) between 11 and 20;
242.
243.

insert into T3

244.
select * from(select e.*, rownum rno from emp e) where
mod(rno,30) between 21 and 29 or mod(rno,30)=0;
245.
246.
A Table consists of 10 employees how to find the sum of 3,4,5,6
employees?
247.
select sum(sal) from(select e.*, rownum rno from emp e) where rno
between 3 and 6
248.

How can we select Last record from a table?

249.
select * from (select e.*, rownum rno from emp e) where
rno>(select count(*)-1 from emp)
250.

How to find the 2 min salaries from EMP table?

251.
select * from(select e.*, row_number() over(order by sal ) rno from
emp e) where rno<=2
252.
select * from (select e.*, dense_rank() over(order by sal) rnk from
emp e) where rnk<=2
253.
select * from (select e.*,rank() over(order by sal) rnk from emp e)
where rnk<=2
254.
255.
256.

How do you display the middle records from a table?

257.

select * from (

258.

select e.*, rownum rno from emp e) where rno in(

259.
emp

select decode(mod(count(*),2),0,count(*)/2,ceil(count(*)+1)/2) from

260.

union

261.

select decode(mod(count(*),2),0,(count(*)+2)/2) from emp)

262.
Without using the Rownum, how do you find the max and min
salaried employee details?
263.

select * fro emp where sal in

264.

(select max(sal) from emp

265.

union

266.

select min(sal) from emp)

267.

How to find the cumulative salaries in EMP table?

268.

select e.*, sum(sal) over(order by rowid) cum_sal from emp e

269.
Write a query to display employee details and average salary of
department without using the subquery (use analytical functions)
270.

select e.*, avg(sal) over(partition by deptno) avg_sal from emp e

271.

How to display the maximum no of employees department details?

272.
select * from dept where deptno=(select deptno from emp group by
deptno having count(*)=(select max(count(*)) from emp group by
deptno))
273.

What is the difference between Substr and Instr?

274.

substr will fetch the string and instr will find the position of string

275.
Write a query to increase the salary of employee by deptno
percentage?
276.

update emp set sal=

277.

What are the different analytical functions?

278.

RANK,DENSE_RANK,ROW_NUMBER,FIRST,LAST,LEAD,LAG

279.

How to rename a column in table?

280.
ALTER TABLE <TABLENAME> RENAME COLUMN (OLD COLUMN
NAME) TO (NEW COLUMN NAME)
281.
How to change the data type of column? Can we change the data
type when we have data in the table?
282.
we can nt change the data type when we have data in the table so
to change the data type of a column follow the steps
283.
take a data back up (ex: create table bkp_tab as select * from
<tablename>)
284.
then change the data type(ex: alter table <tablename> modify
column name data type size)
285.
then insert the data from bkp_tab(ex: insert into <tablename>
select * from bkp_tab)
286.

What are the different types of indexes?

287.

b-tree,bit map,cluster,non cluster,unique

288.
What is difference between clustered index and non-clustered
index?

289.
clusters: it is created on primary key column and only one cluster
can be create on one table
290.
Non-clustered: it will be create more than one non-clustered
index(appr 250)
291.

Why do we use indexes?

292.

we use indexs for to locate the data records very fast

293.

What are hints in oracle?

294.

ordered,leading,indexs,full,+,all_rows,first_rows

295.
296.
297.
298.

What is analyze statement?

299.

Use the ANALYZE statement to collect statistics, for example, to:

300.
1) Collect or delete statistics about an index or index partition, table
or table partition, index-organized table, cluster, or scalar object attribute.
301.
2)Validate the structure of an index or index partition, table or table
partition, index-organized table, cluster, or object reference (REF).
302.

3)Identify migrated and chained rows of a table or cluster.

303.

What is explain plan in oracle?

304.

What the difference is between ceil and floor?

305.
ceil: it will display the next value. ex: select ceil(9.3) from dual; it
will display 10
306.
floor: it will display the before value. ex: select floor(9.3) from dual;
it will display 9
307.

What is round function?

308.

round function will round the fraction no

309.

ex: select round(8.5) from dual; it display 9

310.

select round(8.4) from dual; it display 8

311.

How to get the Last 2 characters in string?

312.

select substr(sunil,-2) from dual

313.

How to delete the parent records without deleting the child records?

314.

How to find the second column in table without querying the table?

315.

How to add a column in the existing table?

316.

alter table <tablename> add column name data type(size)

317.

How to find the number of months between two dates?

318.

select months_between(sysdate,01-01-1990) from dual;

319.

How to find the age of a person?

320.

select months_between(sysdate,12-04-1989)/12 from dual

321.

How to get the next year first date?

322.

select add_months(trunk(sysdate,yyyy),12) from dual;

323.

select trunk(sysdate,yyyy) + interval 1 year from dual;

324.

How to generate sequence numbers without using rownum?

325.

select level from dual connect by level<=20

326.
How to get the 5th Maximum salary? With and without using the
rank function?
327.
select * from (select e.*, dense_rank() over (order by sal desc) rnk
from emp e) where rnk=5
328.
select * from (select e.*, row_number() over (order by sal desc) rno
from emp e) where rno<5

79) at is the difference between stored procedure and Function?


stored procedure
A stored procedure or in simple a proc is a
named PL/SQL block which performs one or
more specific task. This is similar to a
procedure in other programming languages.

Function
A function is a named PL/SQL Block which is
similar to a procedure. The major difference
between a procedure and a function is, a
function must always return a value, but a
procedure may or may not return a value.

A procedure has a header and a body. The


header consists of the name of the procedure
and the parameters or variables passed to the
procedure. The body consists or declaration
section, execution section and exception
section similar to a general PL/SQL Block.
A procedure is similar to an anonymous
PL/SQL Block but it is named for repeated
usage.
CREATE [OR REPLACE] PROCEDURE
proc_name [list of parameters]
IS
Declaration section

CREATE [OR REPLACE] FUNCTION


function_name [parameters]
RETURN return_datatype;
IS

BEGIN
Execution section
EXCEPTION
Exception section
END;

Declaration_section
BEGIN
Execution_section
Return return_variable;
EXCEPTION
exception section
Return return_variable;
END;

80) What is cursor? When we use cursor?


A cursor is a temporary work area created in the system memory when a SQL
statement is executed. A cursor contains information on a select statement and the
rows of data accessed by it.This temporary work area is used to store the data
retrieved from the database, and manipulate this data. A cursor can hold more than
one row, but can process only one row at a time. The set of rows the cursor holds
is called the active set.
There are 2 types of cursors: Implicitcursor
Explicitcursor
81) What is difference between implicit and explicit cursor?
Implicit cursors
These are created by default when DML statements like, INSERT, UPDATE, and
DELETE statements are executed. They are also created when a SELECT
statement that returns just one row is executed.
Explicit cursors
They must be created when you are executing a SELECT statement that returns
more than one row. Even though the cursor stores multiple records, only one
record can be processed at a time, which is called as current row. When you fetch
a row the current row position moves to next row.
Both implicit and explicit cursors have the same functionality, but they differ in
the way they are accessed.
82) What cursor attributes?
The status of the cursor for each of these attributes are defined in the below table.

Attributes

%FOUND

Return Value

Example

The return value is TRUE, if the DML


statements like INSERT, DELETE and
UPDATE affect at least one row and if SELECT
.INTO statement return at least one row.

SQL%FOUND

The return value is FALSE, if DML statements


like INSERT, DELETE and UPDATE do not
affect row and if SELECT.INTO statement
do not return a row.

%NOTFOUND The return value is FALSE, if DML statements


like INSERT, DELETE and UPDATE at least
one row and if SELECT .INTO statement
return at least one row.

SQL%NOTFOUND

The return value is TRUE, if a DML statement


like INSERT, DELETE and UPDATE do not
affect even one row and if SELECT .INTO
statement does not return a row.

%ROWCOUNT Return the number of rows affected by the


SQL%ROWCOUNT
DML operations INSERT, DELETE, UPDATE,
SELECT

For Example: Consider the PL/SQL Block that uses implicit cursor attributes as
shown below:
DECLARE var_rows number(5);
BEGIN
UPDATE employee
SET salary = salary + 1000;
IF SQL%NOTFOUND THEN
dbms_output.put_line('None of the salaries where updated');
ELSIF SQL%FOUND THEN
var_rows := SQL%ROWCOUNT;
dbms_output.put_line('Salaries for ' || var_rows || 'employees are updated');
END IF;
END;
In the above PL/SQL Block, the salaries of all the employees in the employee
table are updated. If none of the employees salary are updated we get a message
'None of the salaries where updated'. Else we get a message like for example,
'Salaries for 1000 employees are updated' if there are 1000 rows in employee
table.

83) How to find the sum of salaries of employees for the departments has more than 3
employees?
Select deptno, count(*),sum(sal) deptno from emp group by deptno
Having count(*)>3;
84) How to find the department wise employee count?
Select deptno,count(*)from emp group by deptno;
select d.deptno,count(e.empno) from dept d left outer join emp e on
e.deptno=d.deptno group by d.deptno;
85) How to find the names starting with letter s and 6 characters in the name?
Select * from emp where ename like 'S____%';
86) How to find the cumulative salary?
Select e.*,sum(sal)over(order by rowid)cum_sal from emp_e;
Within the dept:

Select e.*,sum(sal)over (partition by deptno order byrowid)cum_sal from


emp e;
87) What are acid (Atomicity, consistency, isolation and Durability) properties?
ACID (atomicity, consistency, isolation, and durability) is an acronym
and mnemonicdevice for learning and remembering the four primary attributes
ensured to any transaction by a transaction manager (which is also called a
transaction monitor). These attributes are:
Atomicity. In a transaction involving two or more discrete pieces of information,
either all of the pieces are committed or none are.
Consistency. A transaction either creates a new and valid state of data, or, if any
failure occurs, returns all data to its state before the transaction was started.
Isolation. A transaction in process and not yet committed must remain isolated
from any other transaction.
Durability. Committed data is saved by the system such that, even in the event of
a failure and system restart, the data is available in its correct state.
The ACID concept is described in ISO/IEC 10026-1:1992 Section 4. Each of these
attributes can be measured against a benchmark. In general, however, a transaction
manager or monitor is designed to realize the ACID concept. In a distributed system,
one way to achieve ACID is to use a two-phase commit (2PC), which ensures that all

involved sites must commit to transaction completion or none do, and the transaction
is rolled back (see rollback)
88) When we are executing the sub queries which query will execute first?
In case of subquery , inner query will executed first. output of inner query
Will be pass to the input to the outer query.
89) What is the difference between union and Union all?
UNION
It is used to combine the data with
multiple statement.It will remove the
duplicates

UNION ALL
It will not remove the duplicates.

90) What is difference between IN and Exist?


IN
We can pass the list of values for each value it
does the full data sacn
Used in subquery

EXISTS
Exist is faster because it pass the values on
finding the first match
Used in correlated subquery.
Exist will return the value on finding the first
match.

329.
330.

What is the difference between stored procedure and Function?

331.

What is cursor? When we use cursor?

332.

What is difference between implicit and explicit cursor?

333.

What cursor attributes?

334.
How to find the sum of salaries of employees for the departments has
more than 2 employees?
335.

How to find the department wise employee count?

336.
How to find the Names starting with letter S and 6 characters in the
name?
337.

How to find the cumulative salary?

338.
What are acid (Atomicity, consistency, isolation and Durability)
properties?
339.
When we are executing the sub queries which query will execute
first?
340.

What is the difference between union and Union all?

341.

What is difference between IN and Exist?

342.

How to display nth column details without looking into table?

343.

How to display 1st day of current year?

344.

How to get the first day of year and month?

345.

How to get the last day of year?

346.

How to display first and last records in table?

347.

What is dead lock? How to kill the query currently running?

348.

What is hint? What are different types of hints?

349.

How can we improve the query performance using hints?

350.

What are sub Queries and Correlated sub queries?

351.

How to convert the number into text format (use JSP Function)

352.

How to display the previous records salary in the current record?

353.

How to display current records salary in the next record?

354.

What is on delete cascade option in oracle?

355.

Give me an example of for 1st,2nd and 3rd Normal form?

356.

Write a query to display 5th table?

357.

Display the departments which have more than 5 employees?

358.

Can you employees who are not managers?

359.

How do you find the when is the next Sunday?

360.
How do you find the employee details with count of employees from
his department without using the subquery?
361.
What are all the set operators? What is the pre-requisite to use set
operators?
362.
Why Query will not result any rows when we write the query like
where rownum=5
363.

What is Ref Cursor?

364.

What are orphan records in oracle?

365.

How to generate the Sequence numbers in oracle?