Answers
Last Updated:August 31, 2018
Top PL/SQL Interview Questions:
What is PL/SQL?
PL/SQL (Procedural Language/SQL) is basically a procedural extension of Oracle – SQL.
PL/SQL helps the user to develop complex database applications using control structures,
procedures, function, modules, etc.
This article will discuss the top-most PL/SQL interview question and answers.
Let’s start!!
Question #1) Differentiate PL/SQL and SQL?
Answer: Difference between SQL and PL/SQL can be categorized as follows
SQL PL/SQL
SQL is a natural language which is very useful PL/SQL is a procedural extension of Oracle - SQL.
for interactive processing.
No procedural capabilities like condition testing, PL/SQL supports procedural capabilities as well as high
looping is offered by SQL. language features such as conditional statements, looping
statements, etc.
All SQL statements are executed by the database PL/SQL statements send the entire block of statements to the
server one at a time, thus it is a time-consuming database server at the same time, thus network traffic is
process. reduced considerably.
No error handling procedures are there in SQL. PL/SQL supports customized error handling.
Question #2) Enlist the characteristics of PL/SQL?
Answer: Characteristics of PL/SQL are as follows
PL/SQL allows access and sharing of the same sub programs by multiple
applications.
PL/SQL is known for portability of code as code can be executed on any operating
system provided Oracle is loaded on it.
With PL/SQL user can write their own customized error handling routines.
Improved transaction performance with integration to Oracle data dictionary.
Question #3) What are the data types available in PL/SQL?
Answer: Data types define the ways to identify the type of data and their associated
operations. There are 4 types of predefined data types explained as follows
Scalar Data Types: A scalar data type is an atomic data type that does not have
any internal components.
For example
CHAR (fixed length character value between 1 and 32,767 characters)
VARCHAR2 (variable length character value between 1 and 32,767 characters)
NUMBER ( fixed-decimal, floating-decimal or integer values)
BOOLEAN ( logical data type for TRUE FALSE or NULL values)
DATE (stores date and time information)
LONG (character data of variable length)
Composite Data Types: A composite data type is made up of other data types and
internal components that can be easily used and manipulated. For
example RECORD, TABLE, and VARRAY.
Reference Data Types: A reference data types holds values, called pointers that
designate to other program items or data items. For example REF CURSOR.
Large Object Data Types: A Large Object datatype holds values, called locators,
that defines the location of large objects( such as video clips, graphic image, etc)
stored out of line.
For example
BFILE (Binary file)
BLOB (Binary large object)
CLOB ( Character large object)
NCLOB( NCHAR type large object)
Question #4) Explain the purpose of %TYPE and %ROWTYPE data types with the
example?
Answer: PL/SQL uses %TYPE declaration attribute for anchoring. This attribute provides
the datatype of a variable, constant or column. %TYPE attribute is useful while declaring a
variable that has the same datatype as a table column.
For example, the variable m_empno has the same data type and size as the
column empno in table emp.
m_empno emp.empno%TYPE;
Where <cursor_name> is the name assigned to the cursor and SELECT statement is the
query that returns rows to the cursor active set.
Where <cursor_name> refers to the name of the previously defined cursor from which rows
are being fetched.
<record_list> represents the list of variables that will receive the data being fetched.
2) Implicit cursors: When any SQL statement is executed, PL/SQL automatically creates a
cursor without defining such cursors are known as implicit cursors.
For following statements, PL/SQL employs implicit cursors
INSERT
UPDATE
DELETE
SELECT ( queries that return exactly one row)
Question #8) When do we use triggers?
Answer: The word ‘Trigger’ means to activate. In PL/SQL, the trigger is a stored procedure
that defines an action taken by the database when database related event is performed.
Triggers are mainly required for the following purposes
To maintain complex integrity constraints
Auditing table information by recording the changes
Signaling other program actions when changes are made to table
Enforcing complex business rules
Preventing invalid transactions
Question #9) Explain the difference in execution of triggers and stored procedures?
Answer: A stored procedure is executed explicitly by issuing procedure call statement from
another block via a procedure call with arguments.
The trigger is executed implicitly whenever any triggering event like the occurrence of DML
statements happens.
Only affect those rows added after the trigger is enabled. Affect all rows of the table including that alrea
exist when the constraint is enabled.
Triggers are used to implement complex business rules which Constraints maintain the integrity of the databa
cannot be implemented using integrity constraints.
Question #11) What is a PL/SQL block?
Answer: In PL/SQL, statements are grouped into units called Blocks. PL/SQL blocks can
include constants, variables, SQL statements, loops, conditional statements, exception
handling. Blocks can also build a procedure, a function or a package.
Broadly, PL/SQL blocks are two types
1) Anonymous blocks: PL/SQL blocks without header are known as anonymous blocks.
These blocks do not form the body of a procedure, function or triggers.
Example:
DECLARE
num NUMBER(2);
sq NUMBER(3);
BEGIN
num:= &Number1;
sq := num*num;
DBMS_OUTPUT.PUT_LINE(‘Square:’ ||sq);
END;
2) Named blocks: PL/SQL blocks having header or labels are known as Named blocks.
Named blocks can either be subprograms (procedures, functions, packages) or Triggers.
Example:
FUNCTION sqr (num IN NUMBER)
BEGIN
sq:= num*num;
RETURN sq;
END;
Syntax:
SAVEPOINT <savepoint_name>;
Question #14) What is the mutating table and constraining table?
Answer: A table which is currently being modified by a DML statement like defining triggers
in a table is known as a Mutating table.
A table that might need to be read from for a referential integrity constraint is known as
constraining table.
The variables that are declared in the procedure header and are referenced in the
procedure body are called as Formal parameters.
For example:
PROCEDURE raise_sal( emp_id INTEGER) IS
curr_sal REAL:
………..
BEGIN
SELECT sal INTO cur_sal FROM emp WHERE empno = emp_id;
…….
END raise_sal;
Here in the above example, emp_id acts as a formal parameter.
Question #17) Write a PL/SQL script to display the following series of numbers:
99,96,93……9,6,3?
Answer
SET SERVER OUTPUT ON
DECLARE
BEGIN
LOOP
IF Mod(i,3) = 0 THEN
DBMS_OUTPUT.PUT_LINE(i);
END IF;
END LOOP;
END;
n NUMBER;
BEGIN
n := &enter_a_number;
WHILE(n<>0)
LOOP
count := count+1;
sum := sum+n;
n := &enter_a_number;
END LOOP;
avg := sum/count;
END;