What is an SQL procedure?
An SQL procedure, also called a stored procedure, is a prepared SQL code that you can save so that it can be reused over and over again. It is very similar to the concept of functions in programming languages like C++ and Python.
If you have an SQL query that you write over and over again, save it as a stored procedure. That way, all you have to do to execute it is call it.
Syntax
--------CREATING A PROCEDURE----------CREATE PROCEDURE procedure_nameASsql_statementsGO;--------CREATING A PROCEDURE WITH PARAMETERS----------CREATE PROCEDURE procedure_name @attribute type(size), ...ASsql_statementsGO;--------EXECUTING A PROCEDURE----------EXEC procedure_name
Examples
Consider the following employees table.
| id | name | dept | age |
|---|---|---|---|
| 1 | John | Intern | 25 |
| 2 | David | Intern | 30 |
| 3 | Mike | Engineer | 29 |
| 4 | Alex | HR | 27 |
| 5 | Jake | Intern | 25 |
Creating a procedure to get the information of all employees
--------CREATING THE PROCEDURE----------CREATE PROCEDURE GetAllEmployeesASSELECT * FROM CustomersGO;--------EXECUTING THE PROCEDURE----------EXEC GetAllEmployees;
Creating a procedure to get the information of all interns
--------CREATING THE PROCEDURE----------CREATE PROCEDURE GetAllInterns @dept nvarchar(30)ASSELECT * FROM Customers WHERE dept = @deptGO;--------EXECUTING THE PROCEDURE----------EXEC GetAllInterns dept = "Intern";
Creating a procedure to get the information of all interns above the age of 27
--------CREATING THE PROCEDURE----------CREATE PROCEDURE GetAllInternsAbove @dept nvarchar(30), @age nvchar(10)ASSELECT * FROM Customers WHERE dept = @dept AND age > @ageGO;--------EXECUTING THE PROCEDURE----------EXEC GetAllInternsAbove dept = "Intern", age = 27;
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved