Вы находитесь на странице: 1из 12

Oracle Interview Questions and Answers : SQL

1.
select comm,empno ,
2.
NVL(comm,o)
Comm is null return o.
3.
NVL2(comm,'dsp','psd') ,
Comm is null return PSD not null DSP.
4.
NULLIF('a','b')
a <>b then return a if equal then return null.
5.
from emp
Sal in character format.
SELECT SAL, (TO_CHAR(TO_DATE(SAL,'j'), 'jsp')) FROM EMP;
Dens and desn_rank function.
RANK: When multiple rows share the same rank the next rank in the sequence is no
t consecutive.
The DENSE_RANK function acts like the RANK function except that it assigns conse
cutive ranks:
SELECT EMPNO,
DEPTNO,
SAL,
RANK() OVER (PARTITION BY DEPTNO ORDER BY SAL) "RANK",
DENSE_RANK() OVER (PARTITION BY DEPTNO ORDER BY SAL) "DENSRANK"
FROM EMP;
EMPNO
9999
7369
7876
7788
7902
7566
7900
7521
7654
7844
7499
7698

DEPTNO
10
20
20
20
20
20
30
30
30
30
30
30

SAL
2230
1900
4200
6100
6100
6101
3950
4250
4250
4500
4600
5850

RANK
1
1
2
3
3
5
1
2
2
4
5
6

DENSE_RANK
1
1
2
3
3
4
1
2
2
3
4
5

The FIRST and LAST functions can be used to return the first or last value from
an ordered sequence. Say we want to display the salary of each employee, along w
ith the lowest and highest within their department we may use something like:
SELECT EMPNO,
DEPTNO,
SAL,
MIN(SAL) KEEP (DENSE_RANK FIRST ORDER BY SAL) OVER (PARTITION BY DEPTNO)
"Lowest",
MAX(SAL) KEEP (DENSE_RANK LAST ORDER BY SAL) OVER (PARTITION BY DEPTNO) "
Highest"
FROM EMP
ORDER BY DEPTNO, SAL;
Both LAG and LEAD functions have the same usage, as shown below.
LAG (value_expression [,offset] [,default]) OVER ([query_partition_clause] orde
r_by_clause)
LEAD (value_expression [,offset] [,default]) OVER ([query_partition_clause] orde
r_by_clause)

value_expression - Can be a column or a built-in function, except for other anal


ytic functions.

offset - The number of rows preceeding/following the current row, from which the
data is to be retrieved. The default value is 1.

default - The value returned if the offset is outside the scope of the window. T

he default value is NULL.

SELECT EMPNO,

ENAME,

JOB,

SAL,

LAG(SAL, 1, 0) OVER (ORDER BY SAL ) AS SAL_PREV,

LEAD(SAL, 1, 0) OVER (ORDER BY SAL) AS SAL_NEXT

FROM EMP;
Diff between ROLLUP and CUBE.
SELECT DEPTNO,JOB,COUNT(*) CNT
FROM TEST_EMP
GROUP BY CUBE(DEPTNO,JOB)
SELECT DEPTNO,JOB,COUNT(*) CNT
FROM TEST_EMP
GROUP BY ROLLUP(DEPTNO,JOB)
Roll Up(N+1)
Cube(N*N)
DEPTNO JOB
CNT
DEPTNO
10
Designer
1
10
Manager 1
10
10
2
10
20
Designer
1
20
Helper 2
20
20
3
20
30
Designer
1
30
Tester 4
30
30
5
30
10

JOB
CNT
10
Designer
Manager 1
2
20
Designer
Helper 2
3
30
Designer
Tester 4
5
Designer
3
Helper 2
Manager 1
Tester 4
10

1
1
1

GROUPING /GROUPING_ID IN ROLLUP and CUBE


GROUPING It can be quite easy to visually identify subtotals generated by rollup
s and cubes, but to do it programmatically you really need something more accura
te than the presence of null values in the grouping columns. This is where the G
ROUPING function comes in. It accepts a single column as a parameter and returns
"1" if the column contains a null value generated as part of a subtotal by a RO
LLUP or CUBE operation or "0" for any other value, including stored null values.
The GROUPING_ID function provides an alternate and more compact way to identify
subtotal rows. Passing the dimension columns as arguments, it returns a number i
ndicating the GROUP BY level.
SELECT DEPTNO, JOB, COUNT (*), SUM (SAL),GROUPING (DEPTNO) FLAG1,GROUPING (JOB
) FLAG2,
GROUPING_ID (DEPTNO, JOB) AS GROUPING_ID
FROM EMP
GROUP BY ROLLUP (DEPTNO, JOB)
ORDER BY DEPTNO
DEPTNO
10
10
10
10
20
20

JOB
ANALYST
CLERK
MANAGER

COUNT(*)
1
3000
2
1900
2
5425
5
10325
CLERK 1
1300
MANAGER 1
2975

SUM(SAL)
0
0
0
0
0
0
0
1
0
0
0
0

FLAG1
0
0
0
1
0
0

FLAG2

GROUPING_ID

20
20
20
30
30
30
30
40
40
40

PRESIDENT
rrrr
1
4
CLERK 1
MANAGER 1
SALESMAN
6
CLERK 1
MANAGER 1
2
17

1
3000
12275
950
2850
4
9400
1100
5000
6100
38100

5000
0
0
0
0
5600
0
0
0
0
1

0
0
1
0
0
0
1
0
0
1
1

0
0
1
0
0
0
1
0
0
1
3

To get the top 2 highest paid employees for each dept.


SELECT *
FROM (SELECT deptno, ename, sal,
ROW_NUMBER () OVER (PARTITION BY deptno ORDER BY sal DESC) rnk
FROM emp)
WHERE rnk <= 2;
------------------------------------------------------------------------SELECT ename, deptno, sal,
SUM (sal) OVER (ORDER BY deptno, ename) AS running_total,
SUM (sal) OVER (PARTITION BY deptno ORDER BY ename) AS dept_total,
ROW_NUMBER () OVER (PARTITION BY deptno ORDER BY ename)
AS sequence_no
FROM emp
ORDER BY deptno, ename;
PIVOT
PIVOT is a SQL operation, introduced in Oracle 11g, that lets you write cross t
abulation (also called transposed, crosstab and matrix) queries that rotate rows
into columns while aggregating data in the rotation process.
SELECT *
FROM (SELECT job, deptno, sum(sal) sal FROM emp GROUP BY job, deptno)
PIVOT ( sum(sal) FOR deptno IN (10, 20, 30, 40) );
JOB
10
20
30
40
--------- ---------- ---------- ---------- ---------CLERK
1300
1900
950
SALESMAN
5600
PRESIDENT
5000
MANAGER
2450
2975
2850
ANALYST
6000
Primary vs Unique key + Not Null
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:8743855576462
How do I eliminate the duplicate rows ?
SQL> delete from table_name where rowid not in (select max(rowid) from table gr
oup by duplicate_values_field_name);
or
SQL> delete duplicate_values_field_name dv from table_name ta where rowid <(sele
ct min(rowid) from table_name tb where ta.dv=tb.dv);
5.
How do I display row number with records?
To achive this use rownum pseudocolumn with query, like SQL> SQL> select rownum,
ename from emp;
Output:

1
Scott
2
Millor
3
Jiyo
4
Smith
6.
Display the records between two range
select rownum, empno, ename from emp where rowid in
(select rowid from emp where rownum <=&upto
minus
select rowid from emp where rownum<&Start);
Enter value for upto: 10
Enter value for Start: 7
ROWNUM
EMPNO ENAME
--------- --------- ---------1
7782 CLARK
2
7788 SCOTT
3
7839 KING
4
7844 TURNER
7.
I know the nvl function only allows the same data type(ie. number or cha
r or date Nvl(comm, 0)), if commission is null then the text Not Applicable want
to display, instead of blank space. How do I write the query?
SQL> select nvl(to_char(comm.),'NA') from emp;
Output :
NVL(TO_CHAR(COMM),'NA')
----------------------NA
300
500
NA
1400
NA
NA
8.
Oracle cursor : Implicit & Explicit cursors
Oracle uses work areas called private SQL areas to create SQL statements.
PL/SQL construct to identify each and every work are used, is called as Cursor.
For SQL queries returning a single row, PL/SQL declares all implicit cursors.
For queries that returning more than one row, the cursor needs to be explicitly
declared.
9.
Explicit Cursor attributes
There are four cursor attributes used in Oracle
cursor_name%Found,
cursor_name%NOTFOUND,
cursor_name%ROWCOUNT,
cursor_name%ISOPEN
10. Implicit Cursor attributes
Same as explicit cursor but prefixed by the word SQL
SQL%Found,
SQL%NOTFOUND,
SQL%ROWCOUNT,
SQL%ISOPEN
Tips : 1. Here SQL%ISOPEN is false, because oracle automatically closed the impl
icit cursor after executing SQL statements.
: 2. All are Boolean attributes.
11. Find out nth highest salary from emp table
SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal))
FROM EMP B WHERE a.sal<=b.sal);
Enter value for n: 2
SAL
--------3700
select *

from (select
from emp
order by sal
)
where rownum
minus
select *
from (select
from emp
order by sal
where rownum

rownum rnk,sal

<&n+1
rownum rnk,sal
)
<&n

select *
from (
select rownum aa, sal
from (select distinct sal from emp order by sal))
where aa =&n
12. To view installed Oracle version information
SQL> select banner from v$version;
13. Display the number value in Words
SQL> select sal, (to_char(to_date(sal,'j'), 'jsp'))
from emp;
the output like,
SAL (TO_CHAR(TO_DATE(SAL,'J'),'JSP'))
--------- ----------------------------------------------------800 eight hundred
1600 one thousand six hundred
1250 one thousand two hundred fifty
If you want to add some text like,
Rs. Three Thousand only.
SQL> select sal "Salary ",
(' Rs. '|| (to_char(to_date(sal,'j'), 'Jsp'))|| ' only.'))
"Sal in Words" from emp
/
Salary Sal in Words
------- -----------------------------------------------------800 Rs. Eight Hundred only.
1600 Rs. One Thousand Six Hundred only.
1250 Rs. One Thousand Two Hundred Fifty only.
14. Display Odd/ Even number of records
Odd number of records:
select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp);
1
3
5
Even number of records:
select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp)
2
4
6
15. Which date function returns number value?
months_between
16. Any three PL/SQL Exceptions?
Too_many_rows, No_Data_Found, Value_Error, Zero_Error, Others
17. What are PL/SQL Cursor Exceptions?
Cursor_Already_Open, Invalid_Cursor
18. Other way to replace query result null value with a text
SQL> Set NULL N/A
to reset SQL> Set NULL
19. What are the more common pseudo-columns?

SYSDATE, USER , UID, CURVAL, NEXTVAL, ROWID, ROWNUM


20. What is the output of SIGN function?
1 for positive value,
0 for Zero,
-1 for Negative value.
21. What is the maximum number of triggers, can apply to a single table?
12 triggers.
Q.What is the purpose of database links in Oracle?
Database links are created to establish communication between different database
s or different environments such as development, test and production of the same
database. The database links are usually designed to be read-only to access oth
er database information . They are also useful when you want to copy production
data into test environment for testing.
Q. What is Oracle s data dictionary used for?
Data dictionary in Oracle contains information about all database objects such a
s tables, triggers, stored procedures, functions, indexes, constraints, views, u
sers, roles, monitoring information, etc.
Q. Which data dictionary objects are used to retrieve the information about the
following objects from a given schema?
1) tables
2) views
3) triggers
4) procedures
5) constraints
6) all of the above mentioned objects
The objects used are:
a> user_tables or tabs
b> user_views
c> user_triggers
d> user_procedures
e> user_constraints
f> user_objects
fferent SQL queries in the same PL/SQL program vs. design time declared explicit
cursors with an association to only one query.
Q. You want to view top 50 rows from Oracle table. How do I this?
Use ROWNUM, the pseudo column in where clause as follows:
Where rownum < 51
After complete execution of query and before displaying output of SQL query to t
he user oracle internally assigns sequential numbers to each row in the output.
These numbers are held in the hidden column or pseudo column that is a ROWNUM co
lumn. Now it is so simple to apply the above logical condition, as you would hav
e done to any other column of the table.
Q. How do you reference column values in BEFORE and AFTER insert and delete trig
gers?
The BEFORE and AFTER insert triggers can reference column values by new collecti
on using keyword :new.column name. The before and after delete triggers can refere

nce column values by old collection using keyword :old. column name.
Q. Can you change the inserted value in one of the columns in AFTER insert trigg
er code?
This is not possible as the column values supplied by the insert SQL query are a
lready inserted into the table. If you try to assign new value to the column in
AFTER insert trigger code then oracle error would be raised. To alter any values
supplied by insert SQL query create BEFORE insert trigger.
Q. Explain use of SYSDATE and USER keywords.
SYSDATE is a pseudo column and refers to the current server system date. USER is
a pseudo column and refers to the current user logged onto the oracle session.
These values come handy when you want to monitor changes happening to the table.

Q. What is the difference between explicit cursor and implicit cursor?


When a single insert, delete or update statement is executed within PL/SQL progr
am then oracle creates an implicit cursor for the same, executes the statement,
and closes the cursor. You can check the result of execution using SQL%ROWCOUNT
function.
Explicit cursors are created programmatically. The cursor type variable is decla
red and associated with SQL query. The program then opens a cursor, fetches colu
mn information into variables or record type variable, and closes cursor after a
ll records are fetched. To check whether cursor is open or not use function SQL%
ISOPEN and to check whether there are any records to be fetched from the cursor
use function SQL%FOUND.
Q. Why does a query in Oracle run faster when ROWID is used as a part of the whe
re clause?
ROWID is the logical address of a row - it is not a physical column. It is compo
sed of file number, data block number and row number within data block. Therefor
e I/O time is minimized retrieving the row, resulting in a faster query.
Q. What type of exception will be raised in the following situations:
a> select..into statement returns more than one row.
b> select..into statement does not return any row.
c> insert statement inserts a duplicate record.
The errors returned are:
a> TOO_MANY_ROWS
b> NO_DATA_FOUND
c> DUP_VAL_ON_INDEX
11.
What are the various types of queries ?
Answer: The types of queries are:

Normal Queries

Sub Queries

Co-related queries

Nested queries


Compound queries
12.
What is a transaction ?
Answer: A transaction is a set of SQL statements between any two COMMIT and ROLL
BACK statements.
13.
What is implicit cursor and how is it used by Oracle ?
Answer: An implicit cursor is a cursor which is internally created by Oracle.It
is created by Oracle for each individual SQL.
14.
Which of the following is not a schema object : Indexes, tables, public
synonyms, triggers and packages ?
Answer: Public synonyms
15.
What is PL/SQL?
Answer: PL/SQL is Oracle s Procedural Language extension to SQL.The language inc
ludes object oriented programming techniques such as encapsulation, function ove
rloading, information hiding (all but inheritance), and so, brings state-of-theart programming to the Oracle database server and a variety of Oracle tools.
16.
Is there a PL/SQL Engine in SQL*Plus?
Answer: No.Unlike Oracle Forms, SQL*Plus does not have a PL/SQL engine.Thus, all
your PL/SQL are send directly to the database engine for execution.This makes i
t much more efficient as SQL statements are not stripped off and send to the dat
abase individually.
17.
Is there a limit on the size of a PL/SQL block?
Answer: Currently, the maximum parsed/compiled size of a PL/SQL block is 64K and
the maximum code size is 100K.You can run the following select statement to que
ry the size of an existing package or procedure. SQL> select * from dba_object_s
ize where name = procedure_name
18.
Can one read/write files from PL/SQL?
Answer: Included in Oracle 7.3 is a UTL_FILE package that can read and write fil
es.The directory you intend writing to has to be in your INIT.ORA file (see UTL_
FILE_DIR=...parameter).Before Oracle 7.3 the only means of writing a file was to
use DBMS_OUTPUT with the SQL*Plus SPOOL command.
DECLARE
fileHandler UTL_FILE.FILE_TYPE;
BEGIN
fileHandler := UTL_FILE.FOPEN( /home/oracle/tmp , myoutput , W );
UTL_FILE.PUTF(fileHandler, Value of func1 is %sn , func1(1));
UTL_FILE.FCLOSE(fileHandler);
END;
19.
How can I protect my PL/SQL source code?
Answer: PL/SQL V2.2, available with Oracle7.2, implements a binary wrapper for P
L/SQL programs to protect the source code.This is done via a standalone utility
that transforms the PL/SQL source code into portable binary object code (somewha
t larger than the original).This way you can distribute software without having
to worry about exposing your proprietary algorithms and methods.SQL*Plus and SQL
*DBA will still understand and know how to execute such scripts.Just be careful,
there is no "decode" command available. The syntax is: wrap iname=myscript.sql
oname=xxxx.yyy
20.
Can one use dynamic SQL within PL/SQL? OR Can you use a DDL in a procedu
re ? How ?
Answer: From PL/SQL V2.1 one can use the DBMS_SQL package to execute dynamic SQL
statements.
Eg: CREATE OR REPLACE PROCEDURE DYNSQL AS
cur integer;
rc integer;
BEGIN
cur := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(cur, CREATE TABLE X (Y DATE) , DBMS_SQL.NATIVE);
rc := DBMS_SQL.EXECUTE(cur);
DBMS_SQL.CLOSE_CURSOR(cur);
END;
21.
What are the various types of Exceptions ?

Answer: User defined and Predefined Exceptions.


22.
Can we define exceptions twice in same block ?
Answer: No.
23.
What is the difference between a procedure and a function ?
Answer: Functions return a single variable by value whereas procedures do not re
turn any variable by value.Rather they return multiple variables by passing vari
ables by reference through their OUT parameter.
24.
Can you have two functions with the same name in a PL/SQL block ?
Answer: Yes.
25.
Can you have two stored functions with the same name ?
Answer: Yes.
26.
Can you call a stored function in the constraint of a table ?
Answer: No.
27.
What are the various types of parameter modes in a procedure ?
Answer: IN, OUT AND INOUT.
28.
What is Over Loading and what are its restrictions ?
Answer: OverLoading means an object performing different functions depending upo
n the no.of parameters or the data type of the parameters passed to it.
29.
Can functions be overloaded ?
Answer: Yes.
30.
Can 2 functions have same name & input parameters but differ only by ret
urn datatype
Answer: No.
41.
What are the advantages of clusters ?
Answer: Access time reduced for joins.
42.
What are the disadvantages of clusters ?
Answer: The time for Insert increases.
43.
Can Long/Long RAW be clustered ?
Answer: No.
41.
What are mutating triggers ?
Answer: A trigger giving a SELECT on the table on which the trigger is written.
42.
What are constraining triggers ?
Answer: A trigger giving an Insert/Updat e on a table having referential integri
ty constraint on the triggering table.
51.
What is the significance of the & and && operators in PL SQL ?
Answer: The & operator means that the PL SQL block requires user input for a var
iable.
The && operator means that the value of this variable should be the same as inpu
tted by the user previously for this same variable
61.
How many rows will the following SQL return :
Select * from emp Where rownum < 10;
Answer: 9 rows
62.
How many rows will the following SQL return :
Select * from emp Where rownum = 10;
Answer: No rows
It return all rows.
Select * from emp where rownum >=1
Select * from emp where rownum =1
it return 1 row.
Select * from emp where rownum >=2

it return No row.

71.
If all the values from a cursor have been fetched and another fetch is i
ssued, the output will be : error, last record or first record ?
Answer: Last Record
81.
When to create indexes ?
Answer: To be created when table is queried for less than 2% or 4% to 25% of the
table rows.
82.
How can you avoid indexes ?
Answer: To make index access path unavailable
91.
For which relational operators in where clause, index is not used ?
Answer: <> , like %... is NOT functions, field +constant, field||

91.
Answer:
92.
ot ?
Answer:
93.

Can dual table be deleted, dropped or altered or updated or inserted ?


Yes
If content of dual is updated to some value computation takes place or n
Yes
If any other table same as dual is created would it act similar to dual?

Answer: Yes
101.
What is a package cursor ?
Answer: A package cursor is a cursor which you declare in the package specificat
ion without an SQL statement.The SQL statement for the cursor is attached dynami
cally at runtime from calling procedures.

Use FULL hint to optimizer for full table scan

Use INDEX or AND-EQUAL hint to optimizer to use one index or set to indexes inst
ead of another.

Use an expression in the Where Clause of the SQL.


83.
What is the difference between foreign key and reference key ?
Answer: Foreign key is the key i.e.attribute which refers to another table prima
ry key. Reference key is the primary key of table referred by another table.
91.
What are actual and formal parameters ?
Answer: Actual Parameters : Subprograms pass information using parameters.The va
riables or expressions referenced in the parameter list of a subprogram call are
actual parameters.For example, the following procedure call lists two actual pa
rameters named emp_num and amount:
Eg.raise_salary(emp_num, amount);
Formal Parameters : The variables declared in a subprogram specification and ref
erenced in the subprogram body are formal parameters.For example, the following
procedure declares two formal parameters named emp_id and increase:
Eg.PROCEDURE raise_salary (emp_id INTEGER, increase REAL) IS current_salary REAL
;
What are the constructs of a procedure, function or a package ?
Answer: The constructs of a procedure, function or a package are :

variables and constants

cursors

exceptions
32.
Why Create or Replace and not Drop and recreate procedures ?
Answer: So that Grants are not dropped.
33.
Can you pass parameters in packages ? How ?
Answer: Yes.You can pass parameters to procedures or functions in a package.
34.
What are the parts of a database trigger ?
Answer: The parts of a trigger are:

A triggering event or statement

A trigger restriction

A trigger action
35.
What are the various types of database triggers ?
Answer: There are 12 types of triggers, they are combination of :

Insert, Delete and Update Triggers.

Before and After Triggers.

Row and Statement Triggers.


36.
What is the advantage of a stored procedure over a database trigger ?
Answer: We have control over the firing of a stored procedure but we have no con
trol over the firing of a trigger.
37.
What is the maximum no.of statements that can be specified in a trigger
statement ?
Answer: One.
38.
Can views be specified in a trigger statement ?
Answer: No

39.
What are the values of :new and :old in Insert/Delete/Update Triggers ?
Answer: INSERT : new = new value, old = NULL
DELETE : new = NULL, old = old value
UPDATE : new = new value, old = old value
40.
What are cascading triggers? What is the maximum no of cascading trigger
s at a time?
Answer: When a statement in a trigger body causes another trigger to be fired, t
he triggers are said to be cascading.Max = 32.
PL-SQL Interview Questions with Answers
1. Describe the difference between a procedure, function and anonymous pl/sql bl
ock.
Level: Low
Expected answer : Candidate should mention use of DECLARE statement, a function
must return a value while a procedure doesn t have to.
2. What is a mutating table error and how can you get around it?
Level: Intermediate
Expected answer: This happens with triggers. It occurs because the trigger is tr
ying to update a row it is currently using. The usual fix involves either use of
views or temporary tables so the database is selecting from one while updating
the other.
3. Describe the use of %ROWTYPE and %TYPE in PL/SQL
Level: Low
Expected answer: %ROWTYPE allows you to associate a variable with an entire tabl
e row.
The %TYPE associates a variable with a single column type.
4. What packages (if any) has Oracle provided for use by developers?
Expected answer: Oracle provides the DBMS_ series of packages. There are many
which developers should be aware of such as DBMS_SQL, DBMS_PIPE, DBMS_TRANSACTIO
N,
DBMS_LOCK, DBMS_ALERT, DBMS_OUTPUT, DBMS_JOB, DBMS_UTILITY, DBMS_DDL, UTL_FILE.
If they can mention a few of these and describe how they used them, even better.
If they include the SQL routines provided by Oracle, great, but not really what
was asked.
5. Describe the use of PL/SQL tables
Expected answer: PL/SQL tables are scalar arrays that can be referenced by a
binary integer. They can be used to hold values for use in later queries
or calculations. In Oracle 8 they will be able to be of the %ROWTYPE designation
, or RECORD.
6. When is a declare statement needed ?
The DECLARE statement is used in PL/SQL anonymous blocks such as with stand alon
e, non-stored PL/SQL procedures. It must come first in a PL/SQL stand alone file
if it is used.
7. In what order should a open/fetch/loop set of commands in a PL/SQL block be i
mplemented if you use the NOTFOUND cursor variable in the exit when statement? W
hy?
Expected answer: OPEN then FETCH then LOOP followed by the exit when. If not spe
cified in this order will result in the final return being done twice because of
the way the %NOTFOUND is handled by PL/SQL.
8. What are SQLCODE and SQLERRM and why are they important for PL/SQL developers
?
Expected answer: SQLCODE returns the value of the error number for the last erro
r encountered. The SQLERRM returns the actual error message for the last error e

ncountered. They can be used in exception handling to report, or, store in an er


ror log table, the error that occurred in the code. These are especially useful
for the WHEN OTHERS exception.
9. How can you find within a PL/SQL block, if a cursor is open?
Expected answer: Use the %ISOPEN cursor status variable.
10. How can you generate debugging output from PL/SQL?
Expected answer: Use the DBMS_OUTPUT package. Another possible method is to just
use the SHOW ERROR command, but this only shows errors. The DBMS_OUTPUT package
can be used to show intermediate results from loops and the status of variables
as the procedure is executed. The new package UTL_FILE can
also be used.
11. What are the types of triggers?
Expected Answer: There are 12 types of triggers in PL/SQL that consist of
combinations of the BEFORE, AFTER, ROW, TABLE, INSERT, UPDATE, DELETE and
ALL key words:
BEFORE ALL ROW INSERT
AFTER ALL ROW INSERT
BEFORE INSERT
AFTER INSERT etc.
. What is a database link? What is the difference between a public and a private
database link? What is a fixed user database link?
A database link allows you to make a connection with a remote database, Oracle o
r not, and query tables from it, even incorporating those accesses with joins to
local tables.
A private database link only works for, and is accessible to the user/schema tha
t owns it. A global one can be accessed by any user in the database.
A fixed user link specifies that you will connect to the remote db as one and on
ly one user that is defined in the link. Alternatively, a current user database
link will connect as the current user you are logged in as.
Database management interview questions Database
1. What is a Cartesian product? What causes it?
Expected answer:
A Cartesian product is the result of an unrestricted join of two or more tables.
The result set of a three table Cartesian product will have x * y * z number of
rows where x, y, z correspond to the

Вам также может понравиться