Top 100 PL/SQL Interview Questions and Answers

These 100 PL/SQL interview questions and answers cover the Oracle concepts you’re most likely to explain or apply in a technical interview. Start with the fundamentals if you’re new to PL/SQL, or jump to the sections on transactions, bulk processing, and debugging for scenario-based practice.

PL/SQL Interview Questions: Fundamentals

  1. What is PL/SQL? PL/SQL is Oracle’s procedural extension to SQL. It adds variables, control flow, exception handling, and reusable program units.
  2. How does PL/SQL differ from SQL? SQL describes which data to retrieve or change. PL/SQL controls how and when statements run.
  3. What sections make up a PL/SQL block? A block has a required executable section and optional declaration and exception-handling sections.
  4. What is an anonymous block? It is an unnamed PL/SQL block submitted for execution rather than stored as a named database object.
  5. What is a stored program unit? It is a named PL/SQL unit, such as a standalone procedure or function, saved in the database for later use.
  6. Can you nest PL/SQL blocks? Yes. Inner blocks can access visible outer declarations, and each block can define its own exception handlers.
  7. What is variable scope? Scope defines where a variable name is visible. An inner declaration with the same name can hide an outer one.
  8. Can the executable section be empty? No. Use the NULL statement when a block needs a statement but deliberately does nothing.
  9. Does the slash belong to PL/SQL syntax? No. In tools such as SQL*Plus, a slash on its own line executes the current buffer.
  10. Does END commit a transaction? No. Ending a block does not itself commit pending changes.

Data Types, Records, and Collections

  1. What does %TYPE do? It declares an item using the data type of an existing column or previously declared item.
  2. What does %ROWTYPE do? It declares a record with fields corresponding to the columns of a table, view, or cursor result.
  3. Does column-based %TYPE inherit the column’s NOT NULL constraint? No. It takes the data type, not the column’s NOT NULL constraint or default value.
  4. How do you declare a constant? Use the CONSTANT keyword and provide an initial value. You cannot reassign the constant later.
  5. What values can a PL/SQL BOOLEAN hold? TRUE, FALSE, or NULL. Whether BOOLEAN is available in SQL depends on the Oracle Database release.
  6. How do you test for NULL? Use IS NULL or IS NOT NULL. Comparing a value to NULL with an equality operator does not test for missing data.
  7. What is a PL/SQL record? A record groups named fields into one variable. Its fields can have different data types.
  8. What collection types does PL/SQL support? Associative arrays, nested tables, and varrays. Each collection stores elements of a declared type.
  9. How does a varray differ from a nested table? A varray has a declared upper bound. A nested table can grow without a declared maximum and can become sparse.
  10. How do you traverse a sparse collection? Start with FIRST and use NEXT to move between existing indexes until no index remains.

SQL Statements and Control Flow

  1. What does SELECT INTO do? A scalar SELECT INTO fetches exactly one row into variables or a record.
  2. What if SELECT INTO returns zero or multiple rows? Zero rows raise NO_DATA_FOUND. More than one row raises TOO_MANY_ROWS.
  3. Does SELECT COUNT(*) INTO raise NO_DATA_FOUND for no matches? Without GROUP BY, COUNT(*) returns one row containing zero when nothing matches.
  4. What loop types are available in PL/SQL? Basic LOOP, WHILE LOOP, and FOR LOOP. A cursor FOR loop iterates through query results.
  5. How do EXIT and CONTINUE differ? EXIT leaves the loop. CONTINUE skips the rest of the current iteration and moves to the next one.
  6. What happens if an IF condition evaluates to NULL? The THEN branch runs only for TRUE, so a NULL condition does not enter that branch.
  7. How does a simple CASE differ from a searched CASE? A simple CASE compares one expression with alternatives. A searched CASE evaluates separate conditions.
  8. Can you run DDL directly in a PL/SQL block? Use dynamic SQL, typically EXECUTE IMMEDIATE. Account for the implicit commits associated with most Oracle DDL.
  9. When should you use dynamic SQL? Use it when statement structure is unknown until runtime. Prefer static SQL when the statement is known at compile time.
  10. Can a bind variable represent a table name? No. Bind variables represent data values. If an identifier must vary, choose it from a validated set of permitted names.

Cursor Interview Questions

  1. What is a cursor? A cursor is a handle to the context of a SQL statement, including its result set when applicable.
  2. How do implicit and explicit cursors differ? Oracle manages implicit cursors automatically. Developers declare explicit cursors for queries they want to process directly.
  3. How do you process an explicit cursor manually? Open it, fetch rows, check for exhaustion after each fetch, and close it when finished.
  4. What does a cursor FOR loop automate? It opens the cursor, fetches each row into a loop record, and closes the cursor when the loop ends.
  5. What are the explicit cursor attributes? %ISOPEN reports whether it is open, %FOUND and %NOTFOUND describe the last fetch, and %ROWCOUNT counts rows fetched.
  6. What does SQL%ROWCOUNT report after an UPDATE? It reports how many rows the UPDATE affected. Capture it before another SQL statement changes its value.
  7. What is a parameterized cursor? It accepts arguments when opened, so the same query definition can run with different values.
  8. What is a REF CURSOR? It is a cursor variable that can reference a query result and can be passed to another routine or a client.
  9. What is the difference between strong and weak REF CURSOR types? A strong type declares a return row type. A weak type does not; SYS_REFCURSOR is weak.
  10. What does SELECT FOR UPDATE do? It locks selected rows so other sessions cannot update them until the transaction releases those locks.

Exceptions and Error Handling

  1. What is a PL/SQL exception? It is a runtime condition that stops normal execution and transfers control to an applicable handler, if one exists.
  2. What are examples of predefined exceptions? NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, and DUP_VAL_ON_INDEX are predefined names for common Oracle errors.
  3. How do you raise a custom exception? Declare an exception in a visible scope, then raise it with RAISE followed by its name.
  4. What is RAISE_APPLICATION_ERROR? It returns an application-defined error to the caller using a message and a number from -20000 through -20999.
  5. What does PRAGMA EXCEPTION_INIT do? It associates a declared exception name with an Oracle error number so you can handle that error by name.
  6. What does WHEN OTHERS catch? It catches exceptions not matched by earlier handlers in the same block. It must be the last handler.
  7. Why is WHEN OTHERS THEN NULL risky? It hides unexpected failures, so the caller may treat incomplete work as successful.
  8. How do you re-raise the current exception? Use RAISE without an exception name inside the handler.
  9. What do SQLCODE and SQLERRM return? In an exception handler, SQLCODE returns the error number and SQLERRM returns the error message.
  10. Can execution resume at the statement that failed? No. To continue after a handled error, isolate the risky operation in an inner block.

Procedures, Functions, and Parameters

  1. How does a procedure differ from a function? A function returns a value through RETURN. A procedure is called as a statement and can pass results through output parameters.
  2. What are the three parameter modes? IN supplies input, OUT returns output, and IN OUT accepts an initial value that the routine can change.
  3. What is the default parameter mode? IN. The subprogram can read an IN parameter but cannot assign to it.
  4. Which parameters can have default values? IN parameters can have defaults, allowing the caller to omit those arguments.
  5. What is named parameter notation? It associates an argument with a parameter name using the => operator rather than relying on position.
  6. Can you mix positional and named notation? Yes. Pass positional arguments first, followed by named arguments.
  7. What is subprogram overloading? It defines multiple subprograms with the same name but distinguishable parameter lists. Return type alone does not distinguish overloads.
  8. What is a forward declaration? It declares a local subprogram’s signature before its body so another local subprogram can call it earlier.
  9. Can every PL/SQL function be called from SQL? No. A SQL-callable function must satisfy SQL rules for argument and return types and restrictions on side effects.
  10. What does NOCOPY do? It hints that Oracle can pass an OUT or IN OUT argument by reference. Oracle may ignore the hint.

Packages and Execution Privileges

  1. What is a PL/SQL package? It groups related routines, types, constants, and variables under one schema object name.
  2. How do the package specification and body differ? The specification declares the public interface. The body implements routines and can define private items.
  3. Does every package need a body? No. A package containing only type and constant declarations, for example, can use just a specification.
  4. How do you make a package helper private? Declare and implement it in the package body without exposing it in the specification.
  5. What is package state? It is the data held in package variables, constants, and cursors. For ordinary stateful packages, it generally persists across calls in a session.
  6. When does package initialization run? For an ordinary package, its initialization section runs when the package is first instantiated in a session.
  7. What does SERIALLY_REUSABLE change? It limits package state to a server call rather than retaining it across calls in the session.
  8. Can you replace a package body without changing its public interface? Yes. You can update the implementation while leaving public declarations in the specification unchanged.
  9. What is definer’s rights execution? AUTHID DEFINER is the default for stored procedures and functions. The unit runs under its owner’s privilege context.
  10. What is invoker’s rights execution? AUTHID CURRENT_USER makes runtime privilege checks use the invoking context rather than the unit owner’s context.

Triggers and Data Integrity

  1. What is a database trigger? It is a stored unit that fires automatically when its defined DML, DDL, or database event occurs.
  2. How do row-level and statement-level triggers differ? A row-level trigger fires for each affected row. A statement-level trigger fires once per triggering statement.
  3. What are :OLD and :NEW? They are row-trigger references to column values before and after the change, where those values exist.
  4. When can you change :NEW values? A BEFORE row trigger can assign to :NEW fields for INSERT or UPDATE. An AFTER row trigger cannot.
  5. What is an INSTEAD OF trigger? It runs in place of the requested DML, commonly to translate changes on a non-updatable view into changes on base tables.
  6. What is a mutating-table error? It can occur when a row-level trigger queries or modifies the table its triggering statement is changing.
  7. How can a compound trigger avoid a mutating-table error? Collect row information in a row section, then query the table in the AFTER STATEMENT section.
  8. What does a trigger’s WHEN clause do? On a row trigger, it filters which affected rows cause the trigger body to execute.
  9. Can an ordinary DML trigger issue COMMIT? No. It runs within the triggering transaction and cannot commit or roll back that transaction.
  10. When should you prefer a constraint over a trigger? Use a declarative constraint whenever it expresses the rule directly, such as uniqueness or referential integrity.

Transactions and Bulk Processing

  1. How do COMMIT and ROLLBACK differ? COMMIT makes pending changes permanent. ROLLBACK undoes uncommitted changes.
  2. What does a SAVEPOINT do? It marks a point you can roll back to without undoing earlier changes in the transaction.
  3. What is an autonomous transaction? It runs independently of the calling transaction and can commit or roll back its own changes separately.
  4. Why avoid COMMIT inside a reusable procedure? It can commit the caller’s earlier work. Keep transaction control with the caller unless the procedure explicitly owns the transaction.
  5. What is a SQL-to-PL/SQL context switch? It is a transfer between the SQL and PL/SQL engines. Repeating it for each row can add overhead.
  6. What does BULK COLLECT do? It fetches multiple rows into collections at once, reducing the overhead of row-by-row fetching.
  7. Why use LIMIT with FETCH BULK COLLECT? It caps each fetch batch, keeping collection memory usage manageable for large result sets.
  8. What does FORALL do? It sends a DML statement to the SQL engine in batches using collection values. It is not a general-purpose loop.
  9. What does SAVE EXCEPTIONS do in FORALL? It lets remaining iterations run after individual failures. Inspect SQL%BULK_EXCEPTIONS afterward for failed iterations and error codes.
  10. When is plain SQL better than bulk PL/SQL? Prefer one set-based statement when it expresses the operation clearly. Bulk processing is useful when procedural work is necessary.

Practical PL/SQL Interview Questions

  1. How do you investigate a compilation error? Use SHOW ERRORS in a supported client or query USER_ERRORS for the stored object’s error text, line, and position.
  2. How do you locate the original line of a runtime error? Capture DBMS_UTILITY.FORMAT_ERROR_BACKTRACE inside the handler. The error message alone may not identify the originating line.
  3. Why might DBMS_OUTPUT messages not appear? DBMS_OUTPUT writes to a buffer. The client must enable and read it, for example with SET SERVEROUTPUT ON in SQL*Plus.
  4. How do you diagnose a slow procedure? Measure where time is spent, inspect execution plans for expensive SQL, and check for queries or DML repeated inside loops.
  5. How would you update thousands of rows efficiently? Try a single set-based UPDATE first. If you need procedural logic, consider bounded fetches and FORALL.
  6. How do you handle a lookup that may find no row? Catch NO_DATA_FOUND around the lookup if absence is expected. Do not treat TOO_MANY_ROWS as the same condition.
  7. How do you select one row when several qualify? Define the selection rule with ORDER BY, include a stable tie-breaker, and apply a row limit.
  8. How can an UPDATE detect a missing target row? Check SQL%ROWCOUNT immediately afterward. Zero means no rows were affected; it does not raise NO_DATA_FOUND.
  9. How do you prevent concurrent inserts from creating duplicates? Enforce uniqueness with a database constraint and handle a duplicate-key error when appropriate. A pre-insert check alone can race.
  10. How would you test a procedure that changes data? Check typical inputs, boundary cases, expected errors, and the resulting table changes. Verify who controls commit and rollback.

For a mock interview, answer aloud before reading each explanation. For scenario questions, state your assumptions and explain what happens when no rows match, a statement fails, or two sessions act on the same data.