Advanced Reporting

Stored Procedures vs. Functions

Senior Data Analyst
March 25, 2026
5 min read

The Difference

  • **Function**: Returns a value. Can be used in SELECT statements.
  • **Stored Procedure**: Executes logic. Can have side effects (INSERT, UPDATE).
  • Example Function (PostgreSQL)

    CREATE FUNCTION get_customer_total(cust_id INT) RETURNS NUMERIC AS $$

    SELECT SUM(amount) FROM orders WHERE customer_id = cust_id;

    $$ LANGUAGE SQL;

    -- Usage

    SELECT name, get_customer_total(id) FROM customers;

    Pro Tip

    Keep your functions pure (no side effects) for easier testing and debugging.

    *Day 85: Triggers—The Automatic Reaction.*

    Ready to put your knowledge into practice?

    Join SQL Mastery and learn through interactive exercises.