A stored procedure is a set of SQL statements that are stored in the database and can be executed by calling the procedure. It is used to perform a specific task or set of tasks.
When students first start working with databases, they usually write SQL queries one by one—sometimes very long queries, and sometimes the same queries again and again.
To make this process easier, more organized, and more efficient, SQL gives us a powerful feature called a stored procedure.
A stored procedure is simply a group of SQL statements that is saved (or “stored”) inside the database, so you can run it whenever you need with just one call.
Instead of writing the same logic repeatedly, you store it once and reuse it.
I often tell my students to think of it like a recipe stored in the kitchen.
Whenever you need that dish, you don’t rewrite the recipe from scratch—you just use the stored instructions.
A stored procedure works exactly the same way for the database.
Key Points About a Stored Procedure
It is a named, saved SQL program inside the database
It can contain multiple SQL statements, loops, conditions, and logic
It can accept parameters (like inputs)
It returns results or performs actions such as insert, update, delete
It improves performance, security, and code reusability
Simple Example
Now, instead of writing that SELECT query every time, you can simply call:
And the database will return all employees from department 30.
Why Do We Use Stored Procedures?
To avoid repeating long SQL queries
To speed up execution because procedures are precompiled
To secure data by controlling what users can run
To organize database operations in a clean, modular way
To keep all important logic where it belongs — inside the database
In short,
a stored procedure is a reusable program written in SQL that helps you run complex database operations quickly, safely, and efficiently.