---
title: "Difference between FUNCTION vs TRIGGER?"  
description: "Difference between FUNCTION vs TRIGGER?"  
author: "ICSM Computer"  
published: 2026-05-12  
updated: 2026-05-12  
canonical: https://www.mindstick.com/interview/34511/difference-between-function-vs-trigger  
category: "database"  
tags: ["database", "sql server"]  
reading_time: 6 minutes  

---

# Difference between FUNCTION vs TRIGGER?

In SQL, a **FUNCTION** and a **TRIGGER** are both database objects, but they serve very different purposes.

## Main difference

| FUNCTION | TRIGGER |
| --- | --- |
| Called manually | Runs automatically |
| Returns a value | Usually does not return value |
| Used for computation/business logic | Used for reacting to table events |
| Executed using SQL statements | Executed when INSERT/UPDATE/DELETE occurs |
| Can be reused anywhere | Attached to a table/event |

## 1. SQL FUNCTION

A function is a reusable block of SQL logic that you explicitly call.

## Example

```plaintext
CREATE FUNCTION add_numbers(a INT, b INT)
RETURNS INT
BEGIN
   RETURN a + b;
END;
```

Calling it:

```plaintext
SELECT add_numbers(5, 3);
```

Result:

```plaintext
8
```

## Characteristics of functions

- Accept parameters
- Return a value
- Called manually
- Reusable
- Used in queries

## Common uses

- Salary calculations
- Formatting data
- String operations
- Business rules
- Mathematical operations

## 2. SQL TRIGGER

A trigger automatically executes when an event happens on a table.

Events:

- INSERT
- UPDATE
- DELETE

## Example

```plaintext
CREATE TRIGGER user_created
AFTER INSERT ON users
FOR EACH ROW
BEGIN
   INSERT INTO logs(message)
   VALUES('New user added');
END;
```

- Now whenever a row is inserted into `users`,\ the trigger runs automatically.
- No manual call needed.

## Characteristics of triggers

- Event-driven
- Automatic execution
- Attached to a table
- Executes before/after events
- Used for automation

## Trigger timing

## BEFORE trigger

Runs before operation.

```plaintext
BEFORE INSERT
```

Use case:

- Validation
- Modifying values

## AFTER trigger

Runs after operation.

```plaintext
AFTER UPDATE
```

Use case:

- Logging
- Notifications
- Audit history

## Visual comparison

## FUNCTION flow

```plaintext
User/Query
    ↓
Calls function
    ↓
Function executes
    ↓
Returns value
```

## TRIGGER flow

```plaintext
INSERT/UPDATE/DELETE happens
           ↓
Trigger fires automatically
           ↓
Trigger logic executes
```

## Real-world examples

## FUNCTION example

Calculate bonus:

```plaintext
SELECT calculate_bonus(50000);
```

## TRIGGER example

Maintain audit log automatically:

```plaintext
Employee salary updated
        ↓
Trigger stores old salary in audit table
```

## Important differences

| Feature | FUNCTION | TRIGGER |
| --- | --- | --- |
| Manual execution | Yes | No |
| Automatic execution | No | Yes |
| Attached to table | No | Yes |
| Returns value | Yes | Usually no |
| Can use in SELECT | Yes | No |
| Event-based | No | Yes |

## Example together

Suppose you have:

## Function

```plaintext
CREATE FUNCTION tax(amount DECIMAL(10,2))
RETURNS DECIMAL(10,2)
BEGIN
   RETURN amount * 0.18;
END;
```

Used like:

```plaintext
SELECT tax(1000);
```

## Trigger

```plaintext
CREATE TRIGGER save_history
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
   INSERT INTO employee_history(emp_id, old_salary)
   VALUES(OLD.id, OLD.salary);
END;
```

Automatically stores old salary whenever update happens.

## Relationship between them

Sometimes triggers call functions internally.

Example:

```plaintext
UPDATE employees
       ↓
Trigger fires
       ↓
Trigger calls function
       ↓
Function calculates audit data
```

## When to use FUNCTION

Use functions when:

- You need reusable logic
- Need returned values
- Need calculations
- Need query support

## When to use TRIGGER

Use triggers when:

- You need automatic actions
- Need audit logs
- Need history tracking
- Need data validation
- Need synchronization

## Answers

### Answer by ICSM Computer

In SQL, a **FUNCTION** and a **TRIGGER** are both database objects, but they serve very different purposes.

## Main difference

| FUNCTION | TRIGGER |
| --- | --- |
| Called manually | Runs automatically |
| Returns a value | Usually does not return value |
| Used for computation/business logic | Used for reacting to table events |
| Executed using SQL statements | Executed when INSERT/UPDATE/DELETE occurs |
| Can be reused anywhere | Attached to a table/event |

## 1. SQL FUNCTION

A function is a reusable block of SQL logic that you explicitly call.

## Example

```plaintext
CREATE FUNCTION add_numbers(a INT, b INT)
RETURNS INT
BEGIN
   RETURN a + b;
END;
```

Calling it:

```plaintext
SELECT add_numbers(5, 3);
```

Result:

```plaintext
8
```

## Characteristics of functions

- Accept parameters
- Return a value
- Called manually
- Reusable
- Used in queries

## Common uses

- Salary calculations
- Formatting data
- String operations
- Business rules
- Mathematical operations

## 2. SQL TRIGGER

A trigger automatically executes when an event happens on a table.

Events:

- INSERT
- UPDATE
- DELETE

## Example

```plaintext
CREATE TRIGGER user_created
AFTER INSERT ON users
FOR EACH ROW
BEGIN
   INSERT INTO logs(message)
   VALUES('New user added');
END;
```

- Now whenever a row is inserted into `users`,\ the trigger runs automatically.
- No manual call needed.

## Characteristics of triggers

- Event-driven
- Automatic execution
- Attached to a table
- Executes before/after events
- Used for automation

## Trigger timing

## BEFORE trigger

Runs before operation.

```plaintext
BEFORE INSERT
```

Use case:

- Validation
- Modifying values

## AFTER trigger

Runs after operation.

```plaintext
AFTER UPDATE
```

Use case:

- Logging
- Notifications
- Audit history

## Visual comparison

## FUNCTION flow

```plaintext
User/Query
    ↓
Calls function
    ↓
Function executes
    ↓
Returns value
```

## TRIGGER flow

```plaintext
INSERT/UPDATE/DELETE happens
           ↓
Trigger fires automatically
           ↓
Trigger logic executes
```

## Real-world examples

## FUNCTION example

Calculate bonus:

```plaintext
SELECT calculate_bonus(50000);
```

## TRIGGER example

Maintain audit log automatically:

```plaintext
Employee salary updated
        ↓
Trigger stores old salary in audit table
```

## Important differences

| Feature | FUNCTION | TRIGGER |
| --- | --- | --- |
| Manual execution | Yes | No |
| Automatic execution | No | Yes |
| Attached to table | No | Yes |
| Returns value | Yes | Usually no |
| Can use in SELECT | Yes | No |
| Event-based | No | Yes |

## Example together

Suppose you have:

## Function

```plaintext
CREATE FUNCTION tax(amount DECIMAL(10,2))
RETURNS DECIMAL(10,2)
BEGIN
   RETURN amount * 0.18;
END;
```

Used like:

```plaintext
SELECT tax(1000);
```

## Trigger

```plaintext
CREATE TRIGGER save_history
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
   INSERT INTO employee_history(emp_id, old_salary)
   VALUES(OLD.id, OLD.salary);
END;
```

Automatically stores old salary whenever update happens.

## Relationship between them

Sometimes triggers call functions internally.

Example:

```plaintext
UPDATE employees
       ↓
Trigger fires
       ↓
Trigger calls function
       ↓
Function calculates audit data
```

## When to use FUNCTION

Use functions when:

- You need reusable logic
- Need returned values
- Need calculations
- Need query support

## When to use TRIGGER

Use triggers when:

- You need automatic actions
- Need audit logs
- Need history tracking
- Need data validation
- Need synchronization


---

Original Source: https://www.mindstick.com/interview/34511/difference-between-function-vs-trigger

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
