← Back to Questions
SQL

What is the difference between procedure and function in SQL?

Learn What is the difference between procedure and function in SQL? with simple explanations, real-time examples, interview tips and practical use cases.

What is the Difference Between Procedure and Function in SQL?

Stored Procedures and Functions are database programs used to perform reusable operations in SQL.

In simple words:

  • Procedure performs actions and may or may not return value
  • Function always returns a value

Why Procedures and Functions are Important

Enterprise applications require:

  • Reusable database logic
  • Centralized business rules
  • Performance optimization
  • Code modularity

Procedures and functions help:

  • Reduce duplicate SQL code
  • Improve maintainability
  • Improve security
  • Improve performance

Simple Real-Life Example

Think about:

  • Calculator application

Function Example

Function:

  • Takes numbers
  • Returns result
add(10, 20) = 30

Procedure Example

Procedure:

  • Performs complete task
Generate Monthly Salary Report

Main Difference

Feature Procedure Function
Return Value Optional Mandatory
Purpose Perform operations Return calculated value
Can Modify Data Yes Usually No
Can Use SELECT Yes Yes
Can Use INSERT/UPDATE/DELETE Yes Restricted in many databases
Invocation CALL statement SELECT statement
Complex Business Logic Supported Limited

What is a Procedure?

A Procedure is:

A reusable SQL program used to perform operations or business workflows.


Procedure Features

  • Can return multiple values
  • Can modify database tables
  • Supports transactions
  • Supports loops and conditions

Procedure Syntax

CREATE PROCEDURE procedure_name()

BEGIN

    SQL statements;

END;

Procedure Example

CREATE PROCEDURE AddStudent(

    IN studentName VARCHAR(100)

)

BEGIN

    INSERT INTO students(name)

    VALUES(studentName);

END;

Execution

CALL AddStudent('Naresh');

What Happens?

Procedure:

  • Inserts data into table

What is a Function?

A Function is:

A reusable SQL program that always returns a value.


Function Features

  • Must return value
  • Used for calculations
  • Can be used inside queries

Function Syntax

CREATE FUNCTION function_name(parameters)

RETURNS datatype

BEGIN

    SQL statements;

    RETURN value;

END;

Function Example

CREATE FUNCTION CalculateBonus(

    salary DECIMAL(10,2)

)

RETURNS DECIMAL(10,2)

BEGIN

    RETURN salary * 0.10;

END;

Execution

SELECT CalculateBonus(50000);

Result

5000

Procedure Internal Architecture

Application Calls Procedure
            |
            v
Database Executes Business Logic
            |
            v
Modify Data / Return Results

Function Internal Architecture

Query Calls Function
          |
          v
Function Performs Calculation
          |
          v
Return Single Value

Procedure vs Function Query Flow

Procedure Flow

CALL Procedure
      |
      v
Execute SQL Logic
      |
      v
Modify Data / Return Output

Function Flow

SELECT Function()
        |
        v
Perform Calculation
        |
        v
Return Value

Procedure with Multiple Operations

CREATE PROCEDURE TransferMoney(

    IN fromAccount INT,
    IN toAccount INT,
    IN amount DECIMAL(10,2)

)

BEGIN

    UPDATE accounts
    SET balance = balance - amount
    WHERE account_id = fromAccount;

    UPDATE accounts
    SET balance = balance + amount
    WHERE account_id = toAccount;

END;

Why Procedure is Better Here?

Because:

  • Multiple database operations needed
  • Transaction management required

Function Example in Query

SELECT employee_name,
       CalculateBonus(salary)

FROM employees;

Why Function is Better Here?

Because:

  • Function returns calculated value

Procedure Can Return Multiple Values

Using:

  • OUT parameters

Example

CREATE PROCEDURE GetStudentCount(

    OUT totalStudents INT

)

BEGIN

    SELECT COUNT(*)
    INTO totalStudents

    FROM students;

END;

Function Always Returns One Value

Example:

RETURN totalStudents;

Can Functions Modify Tables?

In many databases:

  • Functions should avoid modifying tables

Why?

Because:

  • Functions are expected to behave predictably

Can Procedures Use Transactions?

Yes.


Example

START TRANSACTION;

UPDATE accounts ...

COMMIT;

Can Functions Use Transactions?

Usually:

  • Restricted

Real-Time Banking Example

Procedure Usage

  • Money transfer
  • Account creation
  • Transaction processing

Function Usage

  • Interest calculation
  • Tax calculation
  • EMI calculation

Example

CalculateInterest(balance)

Real-Time E-Commerce Example

Procedure Usage

  • Place order
  • Update inventory
  • Process payment

Function Usage

  • Calculate discount
  • Calculate tax
  • Calculate shipping charge

Real-Time Learning Platform Example

Procedure Usage

  • Student enrollment
  • Certificate generation
  • Course completion processing

Function Usage

  • Calculate progress percentage
  • Calculate grade
  • Calculate ranking score

Procedures and Functions in Microservices

Enterprise microservices use:

  • Procedures for transactional workflows
  • Functions for reusable calculations

Advantages of Procedures

  • Supports complex business logic
  • Supports transactions
  • Can modify database tables
  • Improves performance

Advantages of Functions

  • Reusable calculations
  • Can be used inside queries
  • Simple modular design

Disadvantages of Procedures

  • Complex maintenance
  • Difficult debugging

Disadvantages of Functions

  • Limited operations
  • Usually cannot modify tables

Performance Consideration

Procedures are generally better for:

  • Heavy database operations

Functions are better for:

  • Reusable calculations

Best Practices

  • Use procedures for workflows
  • Use functions for calculations
  • Keep functions lightweight
  • Use transactions carefully in procedures

Common Interview Mistake

Many developers think:

  • Procedure and function are same

Reality

Procedures:

  • Perform operations

Functions:

  • Return calculated values

Related Learning Topics


Professional Interview Answer

A Procedure and Function in SQL are reusable database programs, but they serve different purposes. A Procedure is mainly used to perform database operations and business workflows, and it may or may not return values. Procedures can modify database tables, support transactions, and execute complex business logic. A Function, on the other hand, always returns a value and is mainly used for calculations or reusable computations. Functions are commonly used inside SQL queries, whereas procedures are executed using the CALL statement. In enterprise applications, procedures are used for transactional workflows, while functions are used for calculations and reusable logic.


Why Interviewers Like This Answer

  • Clearly explains operational differences
  • Includes execution flow understanding
  • Shows enterprise-level usage knowledge
  • Provides real-world examples
  • Explains performance and design considerations

Frequently Asked Questions

What is difference between procedure and function?

Procedures perform operations, while functions return calculated values.

Can procedures return values?

Yes, procedures can return values using OUT parameters.

Can functions modify tables?

In many databases, functions are restricted from modifying tables.

How are procedures executed?

Procedures are executed using the CALL statement.

How are functions executed?

Functions are usually executed inside SELECT statements.

Why this SQL question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.