SQL Server Indexes, Views and Stored Procedures: A Beginner Guide

SQL Server 6 min readUpdated Nov 20, 2026
5 views0 comments

Professionals do not retype the same query for every report. They build reusable database objects: indexes that make queries fast, views that save a query under a name, and stored procedures that save logic with parameters. This guide introduces all three with working examples.

Part of the SQL Server for Beginners series. This article is the written version of lesson 6 of the Bigland Network video course, with the same sample data. The results shown were worked out by hand from that data, so run every example on a test database, not on production data.

Watch lesson 6 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.


Contents

1. Why indexes exist

Searching a table with a million rows without an index means reading every row: a scan. With an index SQL Server jumps straight to the matching rows: a seek. It works like the index at the back of a book. On a small table you will not notice; on a big one the difference is huge.

2. Clustered and nonclustered indexes

A clustered index sorts the table itself by the key, so a table has only one. A primary key creates one by default. A nonclustered index is a separate structure that points to the rows; you can create many. Create one with the prefix IX:

CREATE NONCLUSTERED INDEX IX_Employees_DeptId_Salary
ON Employees (DeptId, Salary);

-- INCLUDE adds columns without sorting by them, so a query can be answered from the index alone
CREATE NONCLUSTERED INDEX IX_Employees_DeptId
ON Employees (DeptId) INCLUDE (FirstName, Salary);

DROP INDEX IX_Employees_DeptId ON Employees;

The order of columns matters: this first index sorts by DeptId, then by Salary. For a deeper explanation see SQL Server Indexes Explained: Clustered, Nonclustered and Covering.

When an index helps, and what it costs

  • Helps on columns used in WHERE, JOIN and ORDER BY, and on foreign key columns.
  • Works best on columns with many different values, such as an email.
  • Useless on very small tables.
  • If you apply a function to the column (YEAR(HireDate) = 2026) SQL Server cannot use the index and falls back to a scan.
  • Costs: every INSERT, UPDATE and DELETE must also update each index, and indexes use disk space and memory. Create only the indexes your real queries need.

To check whether an index is used, press Ctrl+M in SSMS (include the actual execution plan) and look for an Index Seek instead of a Table Scan, or run SET STATISTICS IO ON; and compare the pages read. See How to Find and Fix Slow Queries in SQL Server for a full workflow.

3. Views: saved queries

A view is a saved SELECT with a name. It stores no data and runs its query every time you use it.

CREATE VIEW vw_EmployeeDepartment AS
SELECT e.FirstName, e.Salary, d.DeptName
FROM Employees e
INNER JOIN Departments d ON e.DeptId = d.DeptId;
GO
SELECT * FROM vw_EmployeeDepartment WHERE DeptName = 'IT';
FirstName Salary DeptName
Youssef 5200 IT
Nadia 4800 IT

Why use views: they simplify a complex join behind a simple name, add security (expose only some columns or rows, for example without Salary), keep business logic consistent, and let you change the tables underneath without breaking reports. Change a view with ALTER VIEW and remove it with DROP VIEW IF EXISTS. You cannot use ORDER BY inside a view unless you also use TOP.

4. Stored procedures

A stored procedure is saved logic you run by name. Parameters start with an at sign. Use the prefix usp.

CREATE PROCEDURE usp_EmployeesByDept
    @DeptId INT
AS
BEGIN
    SELECT FirstName, Salary FROM Employees WHERE DeptId = @DeptId;
END;
GO
EXEC usp_EmployeesByDept @DeptId = 2;
FirstName Salary
Youssef 5200
Nadia 4800

Parameters can have defaults:

CREATE PROCEDURE usp_RaiseSalary
    @DeptId  INT,
    @Percent INT = 10
AS
BEGIN
    UPDATE Employees
    SET Salary = Salary * (1 + @Percent / 100.0)
    WHERE DeptId = @DeptId;
END;
GO
EXEC usp_RaiseSalary @DeptId = 1;                 -- 10 percent
EXEC usp_RaiseSalary @DeptId = 1, @Percent = 5;   -- 5 percent

A procedure that changes data should use a transaction and TRY...CATCH as shown in Lesson 4: INSERT, UPDATE, DELETE and Transactions. An OUTPUT parameter returns a value:

CREATE PROCEDURE usp_CountByDept
    @DeptId INT,
    @Total  INT OUTPUT
AS
BEGIN
    SELECT @Total = COUNT(*) FROM Employees WHERE DeptId = @DeptId;
END;
GO
DECLARE @n INT;
EXEC usp_CountByDept @DeptId = 2, @Total = @n OUTPUT;
SELECT @n AS Total;    -- 2

Why use procedures: reuse from reports, applications and jobs; security (grant EXECUTE without table access, see SQL Server Logins, Users and Permissions); safety (parameters help prevent SQL injection); and plan reuse. Change one with ALTER PROCEDURE, remove it with DROP PROCEDURE, and never name your own procedures sp_: that prefix is reserved for system procedures, so SQL Server searches the master database first, which is slower and can cause conflicts.

5. Common mistakes and best practices

  • An index on every column: writes slow down and space is wasted.
  • SELECT * in a view: columns added later do not appear.
  • Calling EXEC with parameters in the wrong order. Use parameter names.
  • Naming a procedure sp_something.
  • Best practice: create indexes for real queries after checking the execution plan, index foreign key columns you join on, list columns explicitly, and name objects consistently (IX, vw, usp). Schedule maintenance with SQL Server Agent Jobs.

6. Practice

  • Create a nonclustered index on Salary.
  • Create a view vw_HighEarners with first name and salary of employees earning more than 4000.
  • Create a procedure usp_AddEmployee (first name, department, salary) and run it for Hassan, department 2, salary 4500.
CREATE NONCLUSTERED INDEX IX_Employees_Salary ON Employees (Salary);
GO
CREATE VIEW vw_HighEarners AS
SELECT FirstName, Salary FROM Employees WHERE Salary > 4000;
GO
CREATE PROCEDURE usp_AddEmployee @FirstName VARCHAR(50), @DeptId INT, @Salary INT AS
BEGIN
    INSERT INTO Employees (FirstName, DeptId, Salary) VALUES (@FirstName, @DeptId, @Salary);
END;
GO
EXEC usp_AddEmployee 'Hassan', 2, 4500;

Selecting from the view returns Amina, Youssef and Nadia.

Frequently asked questions

Does a view make a query faster?

No. A standard view stores no data and runs its query each time. It simplifies and secures access, but it does not add speed by itself.

How many indexes are too many?

There is no fixed number. Each index slows writes and uses space, so keep only the ones your real queries use and drop the rest.

Stored procedure or view?

Use a view for a reusable read-only query. Use a procedure when you need parameters, several statements or changes to data.

What to learn next

Next, make long queries readable with CTEs and rank rows with window functions in Lesson 7: CTEs and Window Functions. Previous lesson: Lesson 5: CREATE TABLE, Data Types and Constraints.

The whole series

Sources and further reading

  • Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for every statement used here.
  • Microsoft Learn, SQL Server documentation, for SQL Server Management Studio (SSMS) and the sample concepts.
Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

More tutorials like this

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top