PL/SQL Interview Questions and Answers

1) What is PL/SQL ?

Ans: PL/SQL is a procedural language that has both interactive SQL and procedural programming language constructs such as iteration, conditional branching.


2) What is the basic structure of PL/SQL ?

Ans: PL/SQL uses block structure as its basic structure. Anonymous blocks or nested blocks can be used in PL/SQL.


3) What are the components of a PL/SQL block ?

Ans: A set of related declarations and procedural statements is called block.


4) What are the components of a PL/SQL Block ?

Ans: Declarative part, Executable part and Exception part.
Datatypes PL/SQL


5) What are the datatypes a available in PL/SQL ?

Ans:

  • Some scalar data types such as NUMBER, VARCHAR2, DATE, CHAR, LONG, BOOLEAN.
  • Some composite data types such as RECORD & TABLE.

6) What are % TYPE and % ROWTYPE ? What are the advantages of using these over datatypes?

Ans: 

  • % TYPE provides the data type of a variable or a database column to that variable.
  • % ROWTYPE provides the record type that represents a entire row of a table or view or columns selected in the cursor.

The Advantages are : 

  • Need not know about variable’s data type.
  • If the database definition of a column in a table changes, the data type of a variable changes accordingly.

7) What is difference between % ROWTYPE and TYPE RECORD ?

Ans: 

% ROWTYPE is to be used whenever query returns a entire row of a table or view.
TYPE rec RECORD is to be used whenever query returns columns of different
table or views and variables.
E.g. TYPE r_emp is RECORD (eno emp.empno% type,ename emp ename %type
);
e_rec emp% ROWTYPE
cursor c1 is select empno,deptno from emp;
e_rec c1 %ROWTYPE.


8) What is PL/SQL table ?

Ans: Objects of type TABLE are called “PL/SQL tables”, which are modeled as (but not the same as) database tables, PL/SQL tables use a primary PL/SQL tables can have one column and a primary key.


9) What is a cursor ? Why Cursor is required ?

Ans: Cursor is a named private SQL area from where information can be accessed. Cursors are required to process rows individually for queries returning multiple rows.


10) Explain the two type of Cursors ?

Ans: There are two types of cursors, Implicit Cursor and Explicit Cursor.
PL/SQL uses Implicit Cursors for queries.
User defined cursors are called Explicit Cursors. They can be declared and used.


11) What are the PL/SQL Statements used in cursor processing ?

Ans: DECLARE CURSOR cursor name, OPEN cursor name, FETCH cursor name INTO or Record types, CLOSE cursor name.


12) What are the cursor attributes used in PL/SQL ?

Ans:

%ISOPEN         – To check whether cursor is open or not
% ROWCOUNT– Number of rows fetched/updated/deleted.
% FOUND         – To check whether cursor has fetched any row. True if rows are fetched.
% NOT FOUND – To check whether cursor has fetched any row. True if no rows are fetched.
These attributes are proceeded with SQL for Implicit Cursors and with Cursor name for Explicit Cursors.


13) What is a cursor for loop ?

Ans: Cursor for loop implicitly declares %ROWTYPE as loop index, opens a cursor, fetches rows of values from active set into fields in the record and closes
when all the records have been processed.
E.g.: FOR emp_rec IN C1 LOOP
salary_total := salary_total +emp_rec sal;
END LOOP;


14) What will happen after commit statement ?

Ans: Cursor C1 is
Select empno,
ename from emp;
Begin
open C1; loop
Fetch C1 into
eno.ename;
Exit When
C1 %notfound;–
commit;
end loop;
end; The cursor having query as SELECT …. FOR UPDATE gets closed after COMMIT/ROLLBACK.
The cursor having query as SELECT…. does not get closed even after COMMIT/ROLLBACK.


15) Explain the usage of WHERE CURRENT OF clause in cursors ?

Ans: WHERE CURRENT OF clause in an UPDATE, DELETE statement refers to the latest row fetched from a cursor.


16) What is a database trigger ? Name some usages of database trigger ?

Ans: Database trigger is stored PL/SQL program unit associated with a specific database table. Usages are Audit data modifications, Log events transparently, Enforce complex business rules Derive column values automatically, Implement complex security authorizations. Maintain replicate tables.


17) How many types of database triggers can be specified on a table ? What are they ?

Ans: Insert Update Delete
Before Row o.k. o.k. o.k.
After Row o.k. o.k. o.k.
Before Statement o.k. o.k. o.k.
After Statement o.k. o.k. o.k.
If FOR EACH ROW clause is specified, then the trigger for each Row affected by the statement.
If WHEN clause is specified, the trigger fires according to the returned boolean value.


18) Is it possible to use Transaction control Statements such a ROLLBACK or COMMIT in Database Trigger ? Why ?

Ans: It is not possible. As triggers are defined for each table, if you use COMMIT of ROLLBACK in a trigger, it affects logical transaction processing.


19) What are two virtual tables available during database trigger execution ?

Ans:

  • The table columns are referred as OLD.column_name and NEW.column_name.
  • For triggers related to INSERT only NEW.column_name values only available.
  • For triggers related to UPDATE only OLD.column_name NEW.column_name values only available.
  • For triggers related to DELETE only OLD.column_name values only available.

20) What happens if a procedure that updates a column of table X is called in a database trigger of the same table ?

Ans: Mutation of table occurs.


21) What is an Exception ? What are types of Exception ?

Ans: Exception is the error handling part of PL/SQL block. The types are Predefined and user_defined. Some of Predefined exceptions are.
CURSOR_ALREADY_OPEN
DUP_VAL_ON_INDEX
NO_DATA_FOUND
TOO_MANY_ROWS
INVALID_CURSOR
INVALID_NUMBER
LOGON_DENIED
NOT_LOGGED_ON
PROGRAM-ERROR
STORAGE_ERROR
TIMEOUT_ON_RESOURCE
VALUE_ERROR
ZERO_DIVIDE
OTHERS.


22) What is Pragma EXECPTION_INIT ? Explain the usage ?

Ans: The PRAGMA EXECPTION_INIT tells the complier to associate an exception with an oracle error. To get an error message of a specific oracle error.

E.g.: PRAGMA EXCEPTION_INIT (exception name, oracle error number)


23) What is Raise_application_error ?

Ans: Raise_application_error is a procedure of package DBMS_STANDARD which allows to issue an user_defined error messages from stored sub-program or database trigger.


24) What are the return values of functions SQLCODE and SQLERRM ?

Ans: SQLCODE returns the latest code of the error that has occurred.
SQLERRM returns the relevant error message of the SQLCODE.


25) Where the Pre_defined_exceptions are stored ?

Ans: In the standard package.


26) What is a stored procedure ?

Ans:  A stored procedure is a sequence of statements that perform specific function.


27) What is difference between a PROCEDURE & FUNCTION ?

Ans: A FUNCTION always returns a value using the return statement.
A PROCEDURE may return one or more values through parameters or may not return at all.


28) What are advantages fo Stored Procedures ?

Ans: Extensibility, Modularity, Reusability, Maintainability and one time compilation.


29) What are the modes of parameters that can be passed to a procedure ?

Ans: IN, OUT, IN-OUT parameters.


30) What are the two parts of a procedure ?

Ans: Procedure Specification and Procedure Body.


31) Give the structure of the procedure ?

Ans: PROCEDURE name (parameter list…..)
is local variable declarations
BEGIN
Executable statements.
Exception.
exception handlers
end;


32) Give the structure of the function ?

Ans:

FUNCTION name (argument list …..) Return datatype is
local variable declarations
Begin
executable statements
Exception
execution handlers
End;


33) Explain how procedures and functions are called in a PL/SQL block ?

Ans: Function is called as part of an expression.
sal : = calculate_sal (‘a822′);
procedure is called as a PL/SQL statement
calculate_bonus (‘A822′);


34) What is Overloading of procedures ?

Ans: The Same procedure name is repeated with parameters of different datatypes and parameters in different positions, varying number of parameters is called overloading of procedures.
E.g.: DBMS_OUTPUT put_line


35) What is a package ? What are the advantages of packages ?

Ans: Package is a database object that groups logically related procedures. The advantages of packages are Modularity, Easier Application Design, Information. Hiding,. reusability and Better Performance.


36) What are two parts of package ?

Ans: 

  • The two parts of package are PACKAGE SPECIFICATION & PACKAGE BODY.
  • Package Specification contains declarations that are global to the packages and local to the schema.
  • Package Body contains actual procedures and local declaration of the procedures and cursor declarations.

37) What is difference between a Cursor declared in a procedure and Cursor declared in a package specification ?

Ans: A cursor declared in a package specification is global and can be accessed by other procedures or procedures in a package.
A cursor declared in a procedure is local to the procedure that can not be accessed by other procedures.


38) Name the tables where characteristics of Package, procedure and functions are stored ?

Ans: User_objects, User_Source and User_error


39) Would you say you use modularization as a best practice when producing PL/SQL code?

Ans: Of course, you will say yes as soon as you hear this question. Nevertheless, I, like many others, have seen way too much spaghetti code in my life that the question begs for an answer on why you do code this way, assuming you do.

Just remind yourself that with modularized code we are able to separate by task/function so that we can improve things such as maintainability, reusability, readability, reliability, and actually enforce boundaries between key programming components.

I don’t know how many times, but it has been many, that I’ve separated components that were nothing more than a few lines of code. Often times it is these few lines of code that allow me to drastically simplify not only calls to those lines of code but also the components I’d broken them out from.


40) What is the difference between a procedure and a function?

Ans: This is one of those questions that, depending on your interview, you may get right or wrong depending on how you answer it. In the past, I’ve often stated that the difference between a procedure and a function is that a procedure is a program that performs one or more actions while a function’s main purpose is to return a value.

This is true but since a procedure can also return (interestingly with a RETURN clause) values, there is something else missing. The difference, that should get you some brownie points, is in the way procedures and functions are called. A procedure is called as an executable PL/SQL statement while a function is called like a PL/SQL expression.


41) Explain the difference between IN and OUT parameters.

Ans: An IN parameter allows us to pass values into PL/SQL code while the OUT parameter allows us to pass values back out of PL/SQL code. Also, remember that a parameter can be specified as both IN and OUT with the IN OUT declaration.


42) What is module overloading and why might you use it?

Ans: Overloading modules is nothing more than a mechanism that allows the coder to reuse the same name for different programs that are within the same scope. Overloading is probably one of my favorite mechanisms to share and increase usability within code.


43) Describe read consistency

Ans: It has been my experience that, while this is an easy concept, many fall short of understanding it in practice. Read consistency is nothing more than Oracle’s way of quarantining that the data / result set you request at a specific time will be available until the request is complete.

This means that if I issue a SQL statement at 7:00am, and it takes 10 minutes to produce the result set, and someone were to alter the data at 7:05am, the data I get back will look like it did at 7:00am as if no one had modified it.


44) What is an autonomous transaction and how does it affect the commit process?

Ans: An autonomous transaction creates, when called from another transaction, an independent and separate transaction that can issue commits without affecting the calling transaction.


45) What are packages?

Ans: A package is nothing more than a way to consolidate/group/organize/etc., common elements of PL/SQL code into a single named entity. While packages do help improve things like portability and maintainability of code, packages can also help improve the performance of the code.


46) How might you hide/protect your source code if distributed to customers?

Ans: Oracle’s wrap utility provides a good way of hiding PL/SQL source code; protecting it and making it difficult for others to view. You can either wrap source code with the wrap utility or use the DBMS_DDL subprograms.


47) Name two PL/SQL conditional control statements.

Ans: Very easily, the conditional statements are the IF and the CASE statements.


48) Name two loop control statements.

Ans: The loop control statements consist of LOOP, FOR LOOP, and WHILE LOOP.

Arun Gandham

Arun Gandham

Author

Hola peeps! A fitness freak, a lover of games, I catch a flick on the weekends and write for you about current trends.