Stored Procedures & Functions: Reusable SQL Logic in the Database
Stored procedures and functions are named, reusable blocks of SQL and PL/SQL logic stored in the
database. They encapsulate business logic, reduce network traffic, and improve performance. This
guide covers procedures, functions, parameters, cursors, exception handling, packages, dynamic
SQL, and the 25 interview questions that actually come up — with deep, descriptive theory aligned
to the topic.
🎯 Key Takeaways
Stored procedures are reusable SQL/PL/SQL blocks
Functions return a value and can be used in SQL
Parameters can be IN, OUT, or IN OUT
Cursors enable row-by-row processing
Exception handling catches and handles errors
Packages group related procedures and functions
25 Interview QuestionsBeginner to AdvancedOracle, PL/SQL, SQL ServerUpdated Dec 3, 2026
KW
Kailas Warade
✓ Verified Author
Data Engineer · Oracle Certified · SQL & PL/SQL
Kailas works with SQL, PL/SQL, Python, and PySpark to build data pipelines.
He writes the SQL interview guides at Sankalan Data Tech, focused on
practical, real-world answers.
🏆 Oracle Certified💼 Data Engineer📝 SQL & PL/SQL Guides📍 Pune, India
Stored procedures and functions are named, reusable blocks of SQL and PL/SQL logic stored
in the database. They encapsulate business logic, reduce network traffic, and improve
performance.
The core idea is simple: instead of sending multiple SQL statements from the application
to the database, you send one call to a stored procedure that does all the work inside the
database.
Stored procedures are precompiled, which means the database has already parsed and
optimized them. This makes them faster than sending ad-hoc SQL every time.
Stored procedures are the foundation of database-centric application design. They keep
business logic close to the data, where it can be enforced consistently.
Why Stored Procedures Matter
Stored procedures matter because they solve real problems in application development.
Performance. Stored procedures are precompiled and executed on the database
server, reducing network traffic and improving performance.
Reusability. Write once, call many times. Business logic is centralized
and consistent across applications.
Security. Applications can be granted execute permission on procedures
without direct table access. This is a security best practice.
Maintainability. Business logic changes only need to be made in one place
— the procedure — not in multiple application code paths.
The trade-off is that business logic in the database can be harder to version control and
test than application code. But for data-centric operations, the benefits usually outweigh
the costs.
Stored Procedures in Depth
A stored procedure is a named PL/SQL block that performs an action. It can accept
parameters, perform complex logic, and return results via OUT parameters.
CREATE OR REPLACE PROCEDURE get_employee_salary (
p_employee_id IN NUMBER,
p_salary OUT NUMBER
) AS
BEGIN
SELECT salary INTO p_salary
FROM employees
WHERE employee_id = p_employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_salary := NULL;
END;
/
This procedure takes an employee ID as input and returns the salary as output. It handles
the case where the employee does not exist.
Stored procedures can contain any PL/SQL logic: loops, conditionals, cursors, DML
statements, and calls to other procedures.
Procedures are called using EXEC or CALL, depending on the database.
Functions in Depth
A function is a named PL/SQL or SQL block that performs a calculation and returns a single
value. It can be used in SQL expressions like SELECT, WHERE, and HAVING.
CREATE OR REPLACE FUNCTION get_annual_salary (
p_monthly_salary IN NUMBER
) RETURN NUMBER IS
BEGIN
RETURN p_monthly_salary * 12;
END;
/
This function takes a monthly salary and returns the annual salary. It can be used in SQL
like this:
SELECT employee_name, get_annual_salary(salary) AS annual_salary
FROM employees;
Functions must return a value using the RETURN clause. This is the key difference from
procedures.
Functions can be used in SQL expressions, but procedures cannot. This makes functions
ideal for calculations and transformations.
Parameters: IN, OUT, IN OUT
PL/SQL supports three types of parameters: IN, OUT, and IN OUT.
-- IN: input only (read-only)
PROCEDURE get_salary(p_employee_id IN NUMBER)
-- OUT: output only (write-only)
PROCEDURE get_salary(p_employee_id IN NUMBER, p_salary OUT NUMBER)
-- IN OUT: both input and output
PROCEDURE update_salary(p_employee_id IN NUMBER, p_salary IN OUT NUMBER)
IN parameters pass a value into the procedure. They are read-only inside the procedure.
OUT parameters return a value from the procedure. They are write-only inside the
procedure.
IN OUT parameters do both — they pass a value in and return a (possibly different) value
out.
Choosing the right parameter type is important for procedure design. IN for inputs, OUT
for outputs, IN OUT for values that need to be modified.
Cursors in Depth
A cursor is a pointer to a result set that allows row-by-row processing. There are
implicit cursors (automatic) and explicit cursors (user-defined).
-- Implicit cursor (automatic)
SELECT salary INTO v_salary FROM employees WHERE employee_id = 101;
-- Explicit cursor (user-defined)
CURSOR c_employees IS
SELECT employee_id, employee_name FROM employees;
FOR emp IN c_employees LOOP
DBMS_OUTPUT.PUT_LINE(emp.employee_name);
END LOOP;
Implicit cursors are automatically created by Oracle for single-row queries. They are
simpler but limited to one row.
Explicit cursors are user-defined for multi-row queries requiring row-by-row processing.
They give more control but require more code.
Cursor FOR loops are the most common way to process explicit cursors — they handle open,
fetch, and close automatically.
Exception Handling
Exception handling is the mechanism to catch and handle errors in PL/SQL using the
EXCEPTION block with WHEN clauses.
BEGIN
SELECT salary INTO v_salary
FROM employees
WHERE employee_id = p_employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
Common exceptions include NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, and OTHERS.
Exception handling is essential for robust PL/SQL code. Without it, errors propagate to
the caller and can crash the application.
Always handle specific exceptions before OTHERS. OTHERS is a catch-all and should be used
sparingly.
Packages
A package is a group of related procedures, functions, variables, and cursors stored
together. It has a specification (public interface) and a body (implementation).
-- Package specification
CREATE OR REPLACE PACKAGE emp_pkg AS
PROCEDURE get_salary(p_employee_id IN NUMBER, p_salary OUT NUMBER);
FUNCTION get_annual_salary(p_monthly_salary IN NUMBER) RETURN NUMBER;
END emp_pkg;
/
-- Package body
CREATE OR REPLACE PACKAGE BODY emp_pkg AS
PROCEDURE get_salary(p_employee_id IN NUMBER, p_salary OUT NUMBER) IS
BEGIN
SELECT salary INTO p_salary
FROM employees
WHERE employee_id = p_employee_id;
END;
FUNCTION get_annual_salary(p_monthly_salary IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN p_monthly_salary * 12;
END;
END emp_pkg;
/
Packages group related logic together, making code more organized and maintainable. They
also allow private (hidden) procedures and variables.
Packages are the preferred way to organize PL/SQL code in Oracle.
25 Stored Procedures & Functions Interview Questions and Answers
These are the stored procedure and function questions that come up most often in SQL and
PL/SQL interviews. Every answer stands on its own — read them in any order, or work
through them in sequence to build a complete understanding of stored procedures,
functions, parameters, cursors, exception handling, and packages.
/
🔍
No matching questions found
Try a keyword like procedure, function, cursor, or exception.
A stored procedure is a precompiled set of SQL statements stored in the database
that can be executed by name. It can accept parameters, perform complex logic, and
return results via OUT parameters.
CREATE OR REPLACE PROCEDURE get_employee_salary (
p_employee_id IN NUMBER,
p_salary OUT NUMBER
) AS
BEGIN
SELECT salary INTO p_salary
FROM employees
WHERE employee_id = p_employee_id;
END;
/
Stored procedures are precompiled, which means the database has already parsed and
optimized them. This makes them faster than sending ad-hoc SQL every time.
They encapsulate business logic and reduce network traffic between application and
database.
A function is a named PL/SQL or SQL block that performs a calculation and returns a
single value. It can be used in SQL expressions like SELECT, WHERE, and HAVING.
CREATE OR REPLACE FUNCTION get_annual_salary (
p_monthly_salary IN NUMBER
) RETURN NUMBER IS
BEGIN
RETURN p_monthly_salary * 12;
END;
/
Functions must return a value using the RETURN clause. This is the key difference
from procedures.
Functions can be used in SQL expressions, but procedures cannot.
A procedure performs an action and may or may not return a value (via OUT
parameters). A function must return a value and can be used in SQL expressions.
Aspect
Procedure
Function
Return value
Optional (via OUT)
Required (RETURN)
Use in SQL
No
Yes
Purpose
Perform action
Compute value
Use a procedure for actions (insert, update, delete). Use a function for
calculations.
There are three types: IN (input only), OUT (output only), and IN OUT (both input
and output).
-- IN: input only
PROCEDURE get_salary(p_employee_id IN NUMBER)
-- OUT: output only
PROCEDURE get_salary(p_employee_id IN NUMBER, p_salary OUT NUMBER)
-- IN OUT: both
PROCEDURE update_salary(p_employee_id IN NUMBER, p_salary IN OUT NUMBER)
IN parameters are read-only inside the procedure. OUT parameters are write-only.
IN OUT parameters do both.
A cursor is a pointer to a result set that allows row-by-row processing.
-- Explicit cursor
CURSOR c_employees IS
SELECT employee_id, employee_name FROM employees;
FOR emp IN c_employees LOOP
DBMS_OUTPUT.PUT_LINE(emp.employee_name);
END LOOP;
There are implicit cursors (automatic, single-row) and explicit cursors
(user-defined, multi-row).
Cursor FOR loops handle open, fetch, and close automatically.
Exception handling is the mechanism to catch and handle errors in PL/SQL using the
EXCEPTION block with WHEN clauses.
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Not found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
Common exceptions include NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, and OTHERS.
Always handle specific exceptions before OTHERS.
Implicit cursors are automatically created by Oracle for single-row queries.
Explicit cursors are user-defined for multi-row queries.
-- Implicit: single row
SELECT salary INTO v_salary FROM employees WHERE employee_id = 101;
-- Explicit: multi-row
CURSOR c_employees IS
SELECT employee_id, employee_name FROM employees;
Implicit cursors are simpler but limited to one row. Explicit cursors give more
control but require more code.
A REF CURSOR is a pointer to a result set that can be passed between programs.
CREATE OR REPLACE PROCEDURE get_employees (
p_result OUT SYS_REFCURSOR
) AS
BEGIN
OPEN p_result FOR
SELECT * FROM employees;
END;
/
REF CURSORs allow dynamic queries and are useful for returning result sets from
procedures.
They are commonly used in application-database interfaces.
A stored procedure is called explicitly by name. A trigger fires automatically in
response to DML events on a table.
-- Procedure: called explicitly
EXEC get_employee_salary(101, :salary);
-- Trigger: fires automatically
CREATE TRIGGER trg_audit AFTER UPDATE ON employees ...
Procedures are for intentional actions. Triggers are for automatic responses to
data changes.
Yes, using OUT or IN OUT parameters. Unlike a function, it does not have a RETURN
clause for a single value.
CREATE OR REPLACE PROCEDURE get_salary (
p_employee_id IN NUMBER,
p_salary OUT NUMBER
) AS
BEGIN
SELECT salary INTO p_salary
FROM employees
WHERE employee_id = p_employee_id;
END;
/
The OUT parameter p_salary returns the value to the caller.
IN passes a value into the procedure (read-only). OUT returns a value from the
procedure (write-only). IN OUT does both.
-- IN: read-only
-- OUT: write-only
-- IN OUT: read and write
IN for inputs, OUT for outputs, IN OUT for values that need to be modified.
A package is a group of related procedures, functions, variables, and cursors
stored together. It has a specification (public interface) and a body
(implementation).
CREATE OR REPLACE PACKAGE emp_pkg AS
PROCEDURE get_salary(...);
FUNCTION get_annual_salary(...) RETURN NUMBER;
END emp_pkg;
/
Packages group related logic together, making code more organized and maintainable.
They also allow private (hidden) procedures and variables.
A procedure is a single standalone block. A package groups multiple procedures,
functions, and variables together with a public interface.
-- Procedure: standalone
CREATE PROCEDURE get_salary(...)
-- Package: groups multiple
CREATE PACKAGE emp_pkg AS ... END;
Packages are preferred for organizing PL/SQL code in Oracle.
Dynamic SQL is SQL built and executed at runtime, rather than being hardcoded.
EXECUTE IMMEDIATE 'SELECT * FROM employees WHERE employee_id = :1'
INTO v_salary USING p_employee_id;
Dynamic SQL is useful when the query structure is not known until runtime.
It requires careful handling to avoid SQL injection.
A stored procedure is a program that performs actions. A view is a stored SQL query
that acts like a virtual table.
-- Procedure: performs actions
CREATE PROCEDURE update_salary(...)
-- View: stored query
CREATE VIEW v_employees AS SELECT * FROM employees;
Procedures can contain any PL/SQL logic. Views are limited to a single SELECT
statement.
A local variable is a variable declared inside a PL/SQL block, procedure, or
function. It is only visible within that block.
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 101;
END;
Local variables are used to hold intermediate values during processing.
A procedure is a named, stored block that can be called by name. A PL/SQL block is
an anonymous block that runs once and is not stored.
-- Anonymous block
DECLARE
v_salary NUMBER;
BEGIN
...
END;
/
-- Named procedure
CREATE PROCEDURE get_salary(...)
Anonymous blocks are for one-time logic. Procedures are for reusable logic.
In the database context, a function is a standalone PL/SQL block that returns a
value. A method is a function associated with an object type.
-- Standalone function
CREATE FUNCTION get_salary(...) RETURN NUMBER
-- Method (object type)
CREATE TYPE employee AS OBJECT (
MEMBER FUNCTION get_salary RETURN NUMBER
);
Methods are part of object-oriented PL/SQL. Functions are standalone.
In the database context, a procedure is a stored PL/SQL block. A macro is not a
standard database concept — it is more common in application languages.
The closest database equivalent to a macro is dynamic SQL or a template.
This question is more relevant in application development than database
development.
A stored procedure is a named, stored block in the database. A prepared statement is
a precompiled SQL statement in the application.
-- Stored procedure: in database
CREATE PROCEDURE get_salary(...)
-- Prepared statement: in application
PreparedStatement ps = conn.prepareStatement("SELECT * FROM employees WHERE id = ?");
Both improve performance by avoiding re-parsing. Procedures live in the database;
prepared statements live in the application.
A procedure is a named, stored block. A batch is a group of SQL statements sent
together as one unit.
-- Batch: multiple statements sent together
INSERT INTO ...;
UPDATE ...;
DELETE ...;
-- Procedure: named, reusable
CREATE PROCEDURE do_something(...)
Batches are for one-time execution. Procedures are for reusable logic.
A procedure is a named, stored block in the database. A script is a file of SQL
statements executed as a unit.
-- Script: file of SQL statements
-- script.sql
INSERT INTO ...;
UPDATE ...;
-- Procedure: named, stored
CREATE PROCEDURE do_something(...)
Scripts are for one-time execution. Procedures are for reusable logic.
A procedure is a named, stored block. A job is a scheduled task that runs a
procedure or script at a specific time.
Jobs schedule procedures to run automatically. Procedures contain the logic.
A procedure is a named, stored block in the database. An API is an interface for
program-to-program communication.
In the database context, procedures can be exposed as APIs through REST or SOAP
services.
This question is more relevant in application development than database
development.
Well-designed stored procedures follow a few key principles.
Use meaningful names — makes code easier to understand.
Use parameters instead of hardcoded values — makes procedures flexible.
Handle exceptions — prevents crashes and provides better error messages.
Use packages for organization — groups related logic together.
Avoid side effects — procedures should do one thing well.
Use bind variables — prevents SQL injection and improves performance.
-- Well-designed procedure
CREATE OR REPLACE PROCEDURE get_employee_salary (
p_employee_id IN NUMBER,
p_salary OUT NUMBER
) AS
BEGIN
SELECT salary INTO p_salary
FROM employees
WHERE employee_id = p_employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_salary := NULL;
END;
/
This procedure has a meaningful name, uses parameters, handles exceptions, and does
one thing well.
Mistakes I Keep Seeing in Interviews
These come up repeatedly. None of them are exotic, and every one of them is easy to avoid
once you know it is coming.
Mistake one: confusing procedure and function. Candidates often mix up
which returns a value. Function must return; procedure may or may not.
Mistake two: not knowing the parameter types. IN, OUT, IN OUT are
fundamental and come up in almost every PL/SQL interview.
Mistake three: confusing implicit and explicit cursors. Know the
difference: automatic vs user-defined.
Mistake four: not handling exceptions. This is a basic but critical skill
that separates good candidates from great ones.
Mistake five: confusing procedure with trigger. Procedures are called
explicitly; triggers fire automatically.
Mistake six: not understanding packages. Packages are the preferred way to
organize PL/SQL code in Oracle.
Follow-Up Questions Interviewers Ask
Getting the first answer right is only half the interview. What usually decides the
outcome is what happens next. Here is what to prepare for on the stored procedures and
functions topic.
After “What is a stored procedure?” — expect
“How does it differ from a function?”
“What are the parameter types?”
“Can it return a value?”
“What is a package?”
After “What is a cursor?” — expect
“What is the difference between implicit and explicit?”
“What is a REF CURSOR?”
“How do you use a cursor FOR loop?”
“When would you use a cursor?”
After “Exception handling” — expect
“What are common exceptions?”
“What is OTHERS?”
“How do you raise a custom exception?”
“What happens if an exception is not handled?”
After “Packages” — expect
“What is a package specification?”
“What is a package body?”
“What are the benefits of packages?”
“What is a private procedure?”
For every question above, prepare a second-level answer. The candidates who pass are not
the ones who memorized the first answer — they are the ones who kept going for another
thirty seconds and said something useful about why it matters.
Quick Quiz
Five questions to check whether the topics above have landed.
1. Which must return a value using the RETURN clause?
2. Which parameter type is read-only inside a procedure?
3. Which cursor is automatically created for single-row queries?
4. Which exception is a catch-all for unhandled errors?
5. What groups related procedures, functions, and variables together?