The most effective way to prepare for PL/SQL interview questions is to practice writing blocks that combine cursors, exception handling, procedures, and functions—because interviewers rarely ask for a single query in isolation. Instead, they test your ability to solve a business problem using PL/SQL’s procedural features, such as loops, conditional logic, and error trapping. This article covers the most common PL/SQL interview questions, with explanations and example code you can adapt.
What Interviewers Actually Test
PL/SQL interview questions are designed to assess three things: your grasp of SQL (the data manipulation), your understanding of PL/SQL’s procedural constructs (the logic), and your awareness of performance and best practices. A typical question might be “Write a block to update salaries for all employees in department 10, increasing them by 10%, and log any errors.” That single question tests your ability to use a cursor, a loop, an UPDATE statement, and exception handling.
Interviewers also look for familiarity with bulk operations (BULK COLLECT and FORALL) because they show you care about performance. They may ask you to compare implicit and explicit cursors, or to explain when to use a function versus a procedure. Expect questions about the difference between SQL and PL/SQL, the structure of a PL/SQL block, and the purpose of the data dictionary views like USER_TABLES or ALL_OBJECTS.
Core PL/SQL Block Structure Questions
The foundation of any PL/SQL answer is the block structure: DECLARE, BEGIN, EXCEPTION, END. You should be able to write a simple anonymous block from memory. For example:
DECLARE
v_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found.');
END;
/
Common follow-up questions include: What is the difference between %TYPE and %ROWTYPE? (%TYPE declares a variable with the same data type as a column; %ROWTYPE declares a record that matches an entire table row.) What is the difference between a procedure and a function? (A function returns a value; a procedure does not.) What are the four attributes of a cursor? (%FOUND, %NOTFOUND, %ISOPEN, %ROWCOUNT).
Be ready to explain the difference between an implicit cursor (created automatically for single-row SELECTs and DML) and an explicit cursor (declared for multi-row queries). Interviewers often ask you to write a simple loop using an explicit cursor:
DECLARE
CURSOR c_emp IS SELECT employee_id, salary FROM employees WHERE department_id = 10;
v_emp c_emp%ROWTYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO v_emp;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp.employee_id || ' ' || v_emp.salary);
END LOOP;
CLOSE c_emp;
END;
/
Exception Handling Questions
Exception handling is a favorite topic because it shows you can write robust code. Know the predefined exceptions: NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, DUP_VAL_ON_INDEX, and OTHERS. Be prepared to write a block that catches a specific exception and logs it. For instance, if you try to insert a duplicate primary key, you’d catch DUP_VAL_ON_INDEX.
A common question: “What is the difference between SQLCODE and SQLERRM?” SQLCODE returns the numeric error code (e.g., -1 for duplicate), and SQLERRM returns the error message text. You can use them in an exception handler like this:
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error ' || SQLCODE || ': ' || SQLERRM);
END;
Interviewers also ask about user-defined exceptions. You can declare one and raise it explicitly:
DECLARE
e_salary_too_high EXCEPTION;
v_salary employees.salary%TYPE := 50000;
BEGIN
IF v_salary > 30000 THEN
RAISE e_salary_too_high;
END IF;
EXCEPTION
WHEN e_salary_too_high THEN
DBMS_OUTPUT.PUT_LINE('Salary exceeds limit.');
END;
/
Remember that exceptions propagate to the calling block if not handled, so you should also understand the concept of nested blocks and exception propagation.
Procedures, Functions, and Packages
You’ll likely be asked to write a stored procedure or function. A typical question: “Create a function that returns the full name of an employee given an employee ID.” Here’s a correct answer:
CREATE OR REPLACE FUNCTION get_emp_name (p_emp_id IN employees.employee_id%TYPE)
RETURN VARCHAR2 IS
v_full_name VARCHAR2(100);
BEGIN
SELECT first_name || ' ' || last_name INTO v_full_name
FROM employees WHERE employee_id = p_emp_id;
RETURN v_full_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 'Not Found';
END;
/
Know the difference between IN, OUT, and IN OUT parameters. IN is the default (read-only), OUT passes a value back to the caller, and IN OUT allows both. Interviewers might ask you to write a procedure that uses an OUT parameter to return a count:
CREATE OR REPLACE PROCEDURE count_emp (p_dept_id IN NUMBER, p_count OUT NUMBER) IS
BEGIN
SELECT COUNT(*) INTO p_count FROM employees WHERE department_id = p_dept_id;
END;
/
Packages group related procedures and functions. They have two parts: specification (public declarations) and body (implementation). Be ready to explain why packages are useful: encapsulation, better performance (the whole package loads into memory), and global variables.
Cursors and Bulk Operations
Beyond basic cursors, interviewers love to test your knowledge of FOR loops with cursors (which implicitly open, fetch, and close) and cursor FOR loops with subqueries. For example:
BEGIN
FOR rec IN (SELECT employee_id, salary FROM employees WHERE department_id = 10) LOOP
DBMS_OUTPUT.PUT_LINE(rec.employee_id || ' ' || rec.salary);
END LOOP;
END;
/
More advanced questions involve BULK COLLECT and FORALL. These are used to improve performance by reducing context switches. A typical question: “How would you update thousands of rows efficiently?” The answer is to use BULK COLLECT to fetch all rows into a collection, then use FORALL to update them in one statement. Example:
DECLARE
TYPE t_emp_ids IS TABLE OF employees.employee_id%TYPE;
v_ids t_emp_ids;
BEGIN
SELECT employee_id BULK COLLECT INTO v_ids FROM employees WHERE department_id = 20;
FORALL i IN v_ids.FIRST..v_ids.LAST
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = v_ids(i);
COMMIT;
END;
/
Be prepared to discuss the difference between BULK COLLECT and FORALL: BULK COLLECT fetches multiple rows into a collection; FORALL sends a batch of DML statements to SQL engine. Also know that you can use LIMIT clause in BULK COLLECT to avoid memory issues when fetching huge datasets.
Triggers and Dynamic SQL
Triggers are another common topic. You might be asked to write a trigger that automatically logs changes to a table. For example, a simple AFTER INSERT trigger:
CREATE OR REPLACE TRIGGER trg_emp_audit
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO emp_audit (emp_id, action, change_date)
VALUES (:NEW.employee_id, 'INSERT', SYSDATE);
END;
/
Know the difference between statement-level and row-level triggers, and the use of :NEW and :OLD. Also be aware of the mutating table error—a common trap—where a row-level trigger tries to query the same table it’s on.
Dynamic SQL is used when you need to build a query at runtime. The native dynamic SQL uses EXECUTE IMMEDIATE. A typical question: “How would you write a procedure that drops a table whose name is passed as a parameter?”
CREATE OR REPLACE PROCEDURE drop_table (p_table_name IN VARCHAR2) IS
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE ' || p_table_name;
END;
/
Be careful with SQL injection—mention that you should use bind variables where possible, but for object names you must concatenate (and validate).
Performance Tuning and Best Practices
Interviewers often ask how to improve PL/SQL performance. The key answers: use bulk operations instead of row-by-row processing, avoid implicit conversions, use bind variables, and minimize context switches. Also, know that using %TYPE and %ROWTYPE makes code more maintainable.
A common question is “What is the difference between COMMIT and ROLLBACK?” or “When should you use COMMIT inside a loop?” The answer is to avoid committing inside a loop because it can cause performance issues and leave partial updates. Instead, commit after the loop or use a savepoint.
Another frequent question: “What is the difference between SQL and PL/SQL?” SQL is set-based and declarative; PL/SQL is procedural and can handle loops, conditions, and exceptions. They work together: PL/SQL embeds SQL statements.
Know the difference between a cursor FOR loop and a simple loop with explicit cursor. The FOR loop is easier and less error-prone because it manages the open/fetch/close automatically.
Frequently Asked Questions
What is the difference between a procedure and a function? A function must return a value, and it can be used in a SQL statement (if it’s a pure function). A procedure does not return a value and cannot be used directly in a SELECT. Functions are typically used for calculations; procedures for actions.
How do you handle errors in PL/SQL? You use the EXCEPTION block. Predefined exceptions like NO_DATA_FOUND and TOO_MANY_ROWS are caught by name. You can also define your own exceptions and raise them. Use WHEN OTHERS to catch any unhandled error, but it’s better to catch specific ones.
What is a cursor and why use it? A cursor is a pointer to a result set. You use it to fetch rows one at a time when you need to process each row individually. Implicit cursors are used for single-row SELECTs; explicit cursors are for multi-row queries.
What is BULK COLLECT and FORALL? BULK COLLECT fetches multiple rows into a collection in one go, reducing context switches. FORALL sends a batch of DML operations to the SQL engine in one statement. Together they can dramatically speed up loops that process many rows.
What is the mutating table error? It occurs when a row-level trigger tries to query or modify the same table that fired the trigger. Oracle restricts this to avoid inconsistent data. You can work around it by using a compound trigger or a temporary table, but the best practice is to avoid such triggers.
How do you write a PL/SQL block that updates salaries and logs errors? You would use a cursor to loop through employees, update each one, and catch exceptions for individual rows. For better performance, you’d use FORALL with a collection of employee IDs and a single UPDATE, but you’d need to handle errors with SAVE EXCEPTIONS.
Related
- Shortcut to Go to Cell in Excel (Jump Quickly)
- How to Start Data Entry: A Beginner’s Guide
- Google Sheets Percentages: How to Calculate & Format
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Business Template for Invoice: How to UseThe best Excel business template for an invoice is a pre-built spreadsheet that automatically calculates totals, applies tax,…
- Excel Spreadsheet Sharing: How to Send or CollaborateYes—an Excel spreadsheet can be shared as a OneDrive or SharePoint link, or sent as a file attachment.…
- Excel Business Template for Profit and LossThe fastest way to get a profit and loss statement in Excel is to use the built-in Profit…
- Excel Calendar Template for Content PlanningYes, you can build a content planning calendar in Excel using a simple grid layout with columns for…
- Java String Reverse: Methods and ExamplesJava’s String class has no built-in reverse() method. For ordinary text, the standard solution is to create a…
- How to Archive Every Email in Outlook Step by StepTo archive every email in Outlook, open each mail folder, select its messages, and choose Archive; repeat for…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.