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
- What is PL/SQL? PL/SQL is Oracle’s procedural extension to SQL. It adds variables, control flow, exception handling, and reusable program units.
- How does PL/SQL differ from SQL? SQL describes which data to retrieve or change. PL/SQL controls how and when statements run.
- What sections make up a PL/SQL block? A block has a required executable section and optional declaration and exception-handling sections.
- What is an anonymous block? It is an unnamed PL/SQL block submitted for execution rather than stored as a named database object.
- 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.
- Can you nest PL/SQL blocks? Yes. Inner blocks can access visible outer declarations, and each block can define its own exception handlers.
- What is variable scope? Scope defines where a variable name is visible. An inner declaration with the same name can hide an outer one.
- Can the executable section be empty? No. Use the NULL statement when a block needs a statement but deliberately does nothing.
- 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.
- Does END commit a transaction? No. Ending a block does not itself commit pending changes.
Data Types, Records, and Collections
- What does %TYPE do? It declares an item using the data type of an existing column or previously declared item.
- What does %ROWTYPE do? It declares a record with fields corresponding to the columns of a table, view, or cursor result.
- 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.
- How do you declare a constant? Use the CONSTANT keyword and provide an initial value. You cannot reassign the constant later.
- What values can a PL/SQL BOOLEAN hold? TRUE, FALSE, or NULL. Whether BOOLEAN is available in SQL depends on the Oracle Database release.
- 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.
- What is a PL/SQL record? A record groups named fields into one variable. Its fields can have different data types.
- What collection types does PL/SQL support? Associative arrays, nested tables, and varrays. Each collection stores elements of a declared type.
- 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.
- 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
- What does SELECT INTO do? A scalar SELECT INTO fetches exactly one row into variables or a record.
- What if SELECT INTO returns zero or multiple rows? Zero rows raise NO_DATA_FOUND. More than one row raises TOO_MANY_ROWS.
- Does SELECT COUNT(*) INTO raise NO_DATA_FOUND for no matches? Without GROUP BY, COUNT(*) returns one row containing zero when nothing matches.
- What loop types are available in PL/SQL? Basic LOOP, WHILE LOOP, and FOR LOOP. A cursor FOR loop iterates through query results.
- How do EXIT and CONTINUE differ? EXIT leaves the loop. CONTINUE skips the rest of the current iteration and moves to the next one.
- 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.
- How does a simple CASE differ from a searched CASE? A simple CASE compares one expression with alternatives. A searched CASE evaluates separate conditions.
- 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.
- 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.
- 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
- What is a cursor? A cursor is a handle to the context of a SQL statement, including its result set when applicable.
- How do implicit and explicit cursors differ? Oracle manages implicit cursors automatically. Developers declare explicit cursors for queries they want to process directly.
- How do you process an explicit cursor manually? Open it, fetch rows, check for exhaustion after each fetch, and close it when finished.
- 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.
- What are the explicit cursor attributes? %ISOPEN reports whether it is open, %FOUND and %NOTFOUND describe the last fetch, and %ROWCOUNT counts rows fetched.
- 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.
- What is a parameterized cursor? It accepts arguments when opened, so the same query definition can run with different values.
- 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.
- 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.
- 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
- 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.
- 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.
- How do you raise a custom exception? Declare an exception in a visible scope, then raise it with RAISE followed by its name.
- What is RAISE_APPLICATION_ERROR? It returns an application-defined error to the caller using a message and a number from -20000 through -20999.
- 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.
- What does WHEN OTHERS catch? It catches exceptions not matched by earlier handlers in the same block. It must be the last handler.
- Why is WHEN OTHERS THEN NULL risky? It hides unexpected failures, so the caller may treat incomplete work as successful.
- How do you re-raise the current exception? Use RAISE without an exception name inside the handler.
- What do SQLCODE and SQLERRM return? In an exception handler, SQLCODE returns the error number and SQLERRM returns the error message.
- 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
- 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.
- What are the three parameter modes? IN supplies input, OUT returns output, and IN OUT accepts an initial value that the routine can change.
- What is the default parameter mode? IN. The subprogram can read an IN parameter but cannot assign to it.
- Which parameters can have default values? IN parameters can have defaults, allowing the caller to omit those arguments.
- What is named parameter notation? It associates an argument with a parameter name using the => operator rather than relying on position.
- Can you mix positional and named notation? Yes. Pass positional arguments first, followed by named arguments.
- What is subprogram overloading? It defines multiple subprograms with the same name but distinguishable parameter lists. Return type alone does not distinguish overloads.
- What is a forward declaration? It declares a local subprogram’s signature before its body so another local subprogram can call it earlier.
- 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.
- 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
- What is a PL/SQL package? It groups related routines, types, constants, and variables under one schema object name.
- How do the package specification and body differ? The specification declares the public interface. The body implements routines and can define private items.
- Does every package need a body? No. A package containing only type and constant declarations, for example, can use just a specification.
- How do you make a package helper private? Declare and implement it in the package body without exposing it in the specification.
- 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.
- When does package initialization run? For an ordinary package, its initialization section runs when the package is first instantiated in a session.
- What does SERIALLY_REUSABLE change? It limits package state to a server call rather than retaining it across calls in the session.
- 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.
- 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.
- 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
- What is a database trigger? It is a stored unit that fires automatically when its defined DML, DDL, or database event occurs.
- 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.
- What are :OLD and :NEW? They are row-trigger references to column values before and after the change, where those values exist.
- When can you change :NEW values? A BEFORE row trigger can assign to :NEW fields for INSERT or UPDATE. An AFTER row trigger cannot.
- 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.
- What is a mutating-table error? It can occur when a row-level trigger queries or modifies the table its triggering statement is changing.
- 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.
- What does a trigger’s WHEN clause do? On a row trigger, it filters which affected rows cause the trigger body to execute.
- Can an ordinary DML trigger issue COMMIT? No. It runs within the triggering transaction and cannot commit or roll back that transaction.
- 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
- How do COMMIT and ROLLBACK differ? COMMIT makes pending changes permanent. ROLLBACK undoes uncommitted changes.
- What does a SAVEPOINT do? It marks a point you can roll back to without undoing earlier changes in the transaction.
- What is an autonomous transaction? It runs independently of the calling transaction and can commit or roll back its own changes separately.
- 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.
- 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.
- What does BULK COLLECT do? It fetches multiple rows into collections at once, reducing the overhead of row-by-row fetching.
- Why use LIMIT with FETCH BULK COLLECT? It caps each fetch batch, keeping collection memory usage manageable for large result sets.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.