30 Most Important PL/SQL Interview Questions and Answers In 2024

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 7, 2024

Top PL/SQL Interview Questions with Examples to Help You Prepare For The Upcoming Interview:

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, functions, modules, etc.

This article will discuss the top-most PL/SQL interview question and answers.

PL_SQL Interview Questions and Answers

PL/SQL Interview Questions and Answers

Let’s start!!

Q #1) Differentiate PL/SQL and SQL?

Answer: Difference between SQL and PL/SQL can be categorized as follows:

SQLPL/SQL
SQL is a natural language which is very useful for interactive processing.PL/SQL is a procedural extension of Oracle - SQL.
No procedural capabilities like condition testing, looping is offered by SQL.PL/SQL supports procedural capabilities as well as high language features such as conditional statements, looping statements, etc.
All SQL statements are executed by the database server one at a time, thus it is a time-consuming process.PL/SQL statements send the entire block of statements to the database server at the same time, thus network traffic is reduced considerably.
No error handling procedures are there in SQL.PL/SQL supports customized error handling.

Q #2) Enlist the characteristics of PL/SQL?

Answer:

Characteristics of PL/SQL are as follows:

  • PL/SQL allows access and sharing of the same subprograms by multiple applications.
  • It is known for the portability of code as code can be executed on any operating system provided that Oracle is loaded on it.
  • With PL/SQL users can write their own customized error handling routines.
  • Improved transaction performance with integration to Oracle data dictionary.

Q #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 range between 1 and 32,767 characters)
    • VARCHAR2 (variable length character value range 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 type 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)

Recommended Reading =>> PL SQL Data Types

Q #4) Explain the purpose of %TYPE and %ROWTYPE data types with the example?

Answer: PL/SQL uses the %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;

%ROWTYPE attribute is used to declare a variable to be a record having the same structure as a row in a table. The row is defined as a record and its fields have the same names and data types as the columns in the table or view.

For example:

dept_rec dept%ROWTYPE;

This declares a record that can store an entire row for the DEPT table.

Q #5) What do you understand by PL/SQL packages?

Answer: PL/SQL packages are schema objects that group functions, stored procedures, cursors and variables at one place.

Packages have 2 mandatory parts:

  • Package Specifications
  • Package body

Q #6) What do you understand by PL/SQL cursors?

Answer: PL/SQL requires a special capability to retrieve and process more than one row and that resource is known as Cursors. A cursor is a pointer to the context area, which is an area of memory containing SQL statements and information for processing the statements.

PL/SQL Cursor is basically a mechanism under which multiple rows of the data from the database are selected and then each row is individually processed inside a program.

Q #7) Explain cursor types.

Answer: There are two types of cursors.

They are explained as follows:

a) Explicit Cursors: For queries that return more than one row, an explicit cursor is declared and named by a programmer. In order to use explicit cursor in PL/SQL, 4 steps are followed

  • Declare the cursor

Syntax: CURSOR <cursor_name> is
SELECT statement;

Here, <cursor_name> is the name assigned to the cursor and SELECT statement is the query that returns rows to the cursor active set.

  • Open the cursor

Syntax: OPEN <cursor_nam>;

Where, <cursor_name> is the name of the previously defined cursor.

  • Fetch rows from the cursor

Syntax: FETCH <cursor_name> INTO <record_list>;

Here, <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.

  • Closing the cursor

Syntax: CLOSE <cursor_name>;

Here, <cursor_name> is the name of the cursor being closed.

b) Implicit cursors: When any SQL statement is executed, PL/SQL automatically creates a cursor without defining such cursors are known as implicit cursors.

For the following statements, PL/SQL employs implicit cursors

  • INSERT
  • UPDATE
  • DELETE
  • SELECT ( queries that return exactly one row)

Q #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 the 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 the table
  • Enforcing complex business rules
  • Preventing invalid transactions

Q #9) Explain the difference in the execution of triggers and stored procedures?

Answer: A stored procedure is executed explicitly by issuing a 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.

Q #10) Explain the difference between Triggers and Constraints?

Answer: Triggers are different from constraints in the following ways:

TriggersConstraints
Only affect those rows added after the trigger is enabled.Affect all rows of the table including that already exist when the constraint is enabled.
Triggers are used to implement complex business rules which cannot be implemented using integrity constraints.Constraints maintain the integrity of the database.

Q #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:

(i) 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:= &amp;Number1;
sq := num*num;
DBMS_OUTPUT.PUT_LINE(‘Square:’ ||sq);
END;

(ii) 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.

Recommended Reading =>> PL SQL Procedures And Functions

Example:

FUNCTION sqr (num IN NUMBER)
RETURN NUMBER is sq NUMBER(2);
BEGIN
sq:= num*num;
RETURN sq;
END;

Q #12) Differentiate between syntax and runtime errors?

Answer:

Syntax errors are the one which can be easily identified by a PL/SQL compiler. These errors can be a spelling mistake, etc.

Runtime errors are those errors in PL/SQL block for which an exception handling section is to be included for handling the errors. These errors can be SELECT INTO statement which does not return any rows.

Q #13) What are COMMIT, ROLLBACK, and SAVEPOINT?

Answer: COMMIT, SAVEPOINT, and ROLLBACK are three transaction specifications available in PL/SQL.

COMMIT statement: When DML operation is performed, it only manipulates data in database buffer and the database remains unaffected by these changes. To save/store these transaction changes to the database, we need to COMMIT the transaction. COMMIT transaction saves all outstanding changes since the last COMMIT and the following process happens

  • Affected rows locks are released
  • Transaction marked as complete
  • Transaction detail is stored in the data dictionary.

Syntax: COMMIT;

ROLLBACK statement: When we want to undo or erase all the changes that have occurred in the current transaction so far, we require to be rolled back of the transaction. In other words, ROLLBACK erases all outstanding changes since the last COMMIT or ROLLBACK.

Syntax to rollback a transaction completely:

ROLLBACK;

SAVEPOINT statement: The SAVEPOINT statement gives a name and marks a point in the processing of the current transaction. The changes and locks that have occurred before the SAVEPOINT in the transaction are preserved while those that occur after the SAVEPOINT are released.

Syntax:

SAVEPOINT <savepoint_name>;

Q #14) What is the mutating table and constraining table?

Answer: A table that 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.

Q #15) What are actual parameters and formal parameters?

Answer: The variables or an expression referred to as parameters that appear in the procedure call statement is known as Actual parameters.

For example: raise_sal(emp_num, merit+ amount);

Here in the above example, emp_num and amount are the two actual parameters.

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.

Q #16) What is the difference between ROLLBACK and ROLLBACK TO statements?

Answer: The transaction is completely ended after ROLLBACK statement i.e. ROLLBACK command completely undo a transaction and release all locks.

On the other hand, a transaction is still active and running after ROLLBACK TO command as it undo only a part of the transaction up till the given SAVEPOINT.

Q #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
FOR i IN REVERSE 1..99
LOOP
IF Mod(i,3) = 0 THEN
DBMS_OUTPUT.PUT_LINE(i);
END IF;
END LOOP;
END;
/

Q #18) What are the 3 modes of parameter?

Answer: 3 modes of the parameter are IN, OUT, IN OUT.

These can be explained as follows:

  • IN parameters: IN parameters allow you to pass values to the procedure being called and can be initialized to default values. IN parameters acts like a constant and cannot be assigned any value.
  • OUT parameters: OUT parameters return value to the caller and they must be specified. OUT parameters act like an uninitialized variable and cannot be used in an expression.
  • IN OUT parameters: IN OUT parameters passes initial values to a procedure and return updated values to the caller. IN OUT parameters act like an initialized variable and should be assigned a value.

Q #19) Why is %ISOPEN always false for an implicit cursor?

Answer: An implicit cursor, SQL%ISOPEN attribute is always false because the implicit cursor is opened for a DML statement and is closed immediately after the execution of the DML statement.

Q #20) When a DML statement is executed, in which cursor attributes, the outcome of the statement is saved?

Answer: The outcome of the statement is saved in 4 cursor attributes.

These are:

  • SQL%FOUND
  • SQL%NOTFOUND
  • SQL%ROWCOUNT
  • SQL%ISOPEN

Q #21) What are the ways of commenting in a PL/SQL code?

Answer: Comments are the text which is included with the code to enhance readability and for the understanding of the reader. These codes are never executed.

There are two ways to comment in PL/SQL:

1) Single line comment: This comment starts with double –.

Example:
DECLARE
num NUMBER(2);        — it is a local variable.
BEGIN

2) Multi-line comment: This comment starts with /* and ends with */.

Example:
BEGIN
num := &p_num;         /* This is a host variable used in program body */
……….
END

Q #22) What do you understand by Exception handling in PL/SQL?

Answer: When an error occurs in PL/SQL, the exception is raised. In other words, to handle undesired situations where PL/SQL scripts terminated unexpectedly, an error handling code is included in the program. In PL/SQL, all exception handling code is placed in an EXCEPTION section.

There are 3 types of EXCEPTION:

  • Predefined Exceptions: Common errors with predefined names.
  • Undefined Exceptions: Less common errors with no predefined names.
  • User-defined Exceptions: Do not cause runtime error but violate business rules.

Q #23) Enlist some predefined exceptions?

Answer:

Some of the predefined exceptions are:

  • NO_DATA_FOUND: Single row SELECT statement where no data is returned.
  • TOO_MANY_ROWS: Single row SELECT statement where more than one rows are returned.
  • INVALID_CURSOR: Illegal cursor operation occurred.
  • ZERO_DIVIDE: Attempted to divide by zero.

Q #24) What are PL/SQL cursor exceptions?

Answer:

The exceptions related to PL/SQL cursors are:

  • CURSOR_ALREADY_OPEN
  • INVALID_CURSOR

Q #25) Explain the difference between cursor declared in procedures and cursors declared in the package specification?

Answer: The cursor declared in the procedure is treated as local and thus cannot be accessed by other procedures.

The cursor declared in the package specification is treated as global and thus can be accessed by other procedures.

Q #26) What are INSTEAD OF triggers?

Answer: The INSTEAD OF triggers are the triggers written especially for modifying views, which cannot be directly modified through SQL DML statements.

Q #27) What are expressions?

Answer: Expressions are represented by a sequence of literals and variables that are separated by operators. In PL/SQL, operations are used to manipulate, compare and calculate some data. An expression is a composition of ‘Operators’ and ‘Operands’.

  • Operands: These are an argument to the operators. Operands can be a variable, function call or constant.
  • Operators: These specify the actions to be performed on operators. Example: ‘+’, ‘*’, etc.

Q #28) List different type of expressions with the example.

Answer: Expressions can be as mentioned below:

  • Numeric or Arithmetic expressions : Example: 20* 10+ 15
  • Boolean expressions: Example: ‘spot’ LIKE ‘sp%t’
  • String expressions: Example: LENGTH (‘NEW YORK’|| ‘NY’)
  • Date expressions: Example: SYSDATE>TO_DATE(’15-NOV-16’, “dd-mm-yy”)

Q #29) Write a program that shows the usage of the WHILE loop to calculate the average of user entered numbers and entry of more numbers are stopped by entering number 0?

Answer

DECLARE
n NUMBER;
avg NUMBER :=0 ;
sum NUMBER :=0 ;
count NUMBER :=0 ;
BEGIN
n := &amp;enter_a_number;
WHILE(n&lt;&gt;0)
LOOP
count := count+1;
sum := sum+n;
n := &amp;enter_a_number;
END LOOP;
avg := sum/count;
DBMS_OUTPUT.PUT_LINE(‘the average is’||avg);
END;

Q #30) What do you understand by PL/SQL Records?

Answer: A PL/SQL records can be referred as a collection of values or say, a group of multiple pieces of information, each of which is of simpler types and can be related to one another as fields.

There are three types of records supported in PL/SQL:

  • Table based records
  • Programmer based records
  • Cursor based records

Conclusion

PL/SQL is very vast when it comes to learning and application. Hope these interview question and answers will help you go through.

To learn more about PL SQL read our comprehensive PL/SQL Tutorial Series.

Happy Learning!!

Was this helpful?

Thanks for your feedback!

Leave a Comment