SQL Server

Views in SQL Server: A Beginner Introduction

Learn what a SQL Server view stores, how to create and query one, and when a reusable view helps keep beginner queries consistent.

By JaviPublished 11 min readBeginner

A view gives a query a name so people and applications can ask for a familiar result without repeating the full SELECT statement each time. In SQL Server, a regular view stores a query definition. When you query the view, SQL Server uses that definition to produce rows from its underlying tables. The view is useful as a reusable interface, but it is not normally a separate copy of the table’s rows.

This lesson builds a small view over dbo.Customers and dbo.Orders in the synthetic JaviBuildsLab database. It assumes you know basic SELECT syntax, JOINs and table aliases. A view helps package that knowledge; it does not remove the need to understand the query inside it.

View versus table

A table stores rows and columns. A regular view stores a definition that describes a query. Here is a simple query that returns customer and order details:

SQL query: View versus tableT-SQL
SELECT c.CustomerId, c.FirstName, c.LastName,
       o.OrderId, o.OrderDate, o.Status, o.Amount
FROM dbo.Customers AS c
INNER JOIN dbo.Orders AS o
    ON o.CustomerId = c.CustomerId;
T-SQL5 linesRead-only query display. It is not connected to a database.

If several reports need the same relationship and column list, repeating this text creates opportunities for small differences. One report might join on the wrong columns, another might omit a needed field, and a third may use a different filter. A view can give this shared query a stable database name.

The rows are still based on the underlying tables. If an order is added, a later query of the view can include it. If a customer is updated, the view reflects the current data when it is queried. A normal view does not preserve a snapshot of prior results. That is a useful distinction when comparing a view with a table or a report extract.

This also means the view’s output can change from one execution to another as its source data changes. If a report needs a frozen point-in-time result, a normal view is not a replacement for an appropriately designed snapshot or reporting process.

Create a view

SQL Server views belong to a schema. The dbo schema is commonly used in examples, and vw_ is a naming convention some teams use to make views recognizable. The prefix is not required by SQL Server; follow the naming convention of your team.

Create dbo.vw_CustomerOrders with the query above:

SQL query: Create a viewT-SQL
CREATE VIEW dbo.vw_CustomerOrders
AS
SELECT c.CustomerId, c.FirstName, c.LastName,
       o.OrderId, o.OrderDate, o.Status, o.Amount
FROM dbo.Customers AS c
INNER JOIN dbo.Orders AS o
    ON o.CustomerId = c.CustomerId;
T-SQL7 linesRead-only query display. It is not connected to a database.

Run the definition in the intended database. CREATE VIEW must be the first statement in its query batch, so if the script has statements before it, separate batches with GO in SSMS or run the definition in its own window. A view definition does not include result data; it defines how SQL Server should retrieve the data when requested.

In Object Explorer, expand the database, then Views, and refresh the node if needed. You should find dbo.vw_CustomerOrders. The schema is part of the object’s name: dbo.vw_CustomerOrders identifies the view under dbo, while another schema could contain a different object with the same short name.

Query the view

Once created, select from it like a table:

SQL query: Query the viewT-SQL
SELECT CustomerId, FirstName, LastName,
       OrderId, OrderDate, Status, Amount
FROM dbo.vw_CustomerOrders
WHERE Status = N'Complete'
ORDER BY OrderDate DESC, OrderId DESC;
T-SQL5 linesRead-only query display. It is not connected to a database.

The WHERE clause and ORDER BY here apply to this use of the view. They do not permanently alter the view definition. You can also choose only some of the view’s columns. A view can simplify a query that readers use often, and the caller can still filter or sort the results for a particular question.

The order of rows is not stored in the view. Put ORDER BY in the query that reads the view when a particular display order matters. SQL Server does not allow ORDER BY in a view definition unless it is paired with clauses such as TOP or OFFSET; even where syntax permits an order during definition, callers should request their own ordering. A view is a relational query, not a saved sorted spreadsheet.

Change a view definition

When the query needs to change, use ALTER VIEW with the complete new definition. For example, adding the customer’s city changes the columns exposed by the view:

SQL query: Change a view definitionT-SQL
ALTER VIEW dbo.vw_CustomerOrders
AS
SELECT c.CustomerId, c.FirstName, c.LastName, c.City,
       o.OrderId, o.OrderDate, o.Status, o.Amount
FROM dbo.Customers AS c
INNER JOIN dbo.Orders AS o
    ON o.CustomerId = c.CustomerId;
T-SQL7 linesRead-only query display. It is not connected to a database.

After the change, query the view again and check that its output matches the intended interface. Applications, reports, and other views may depend on a view’s column names and types. Removing or renaming a column can break those callers even though the view itself alters successfully. Coordinate definition changes when other people or systems rely on the object.

Why use a view?

Views can make common queries easier to reuse. They provide one named definition for a join or a carefully selected set of columns, which can reduce copy-and-paste drift. A view can also give users a simpler interface than the underlying tables: a report author may need the customer name and order total without needing to remember how the tables connect.

Views can contribute to security when permissions are designed deliberately. For example, an administrator can grant a user permission to query a view that exposes approved columns while withholding direct access to the base tables. This needs to be configured and reviewed by a database owner; creating a view alone does not automatically make data private. Avoid placing secrets or sensitive values into a view and assuming they are protected simply because the view has a friendly name.

Views also help present a stable contract. A team can keep a query’s public column names consistent while changing some details behind the interface. That can be useful for reports or shared SQL, but it requires care: a change to the view still needs testing against its consumers.

When you introduce a view to a team, document its owner, purpose, and expected columns. Give it a name that explains the result rather than a temporary report, and prefer explicit schema-qualified table names in its definition. Before changing a view that others use, check known report or application references and test representative calls. This is ordinary interface maintenance: even a small column change can be visible to a consumer that depends on the previous shape.

Limits and common misunderstandings

A regular view does not normally store a separate set of rows, so it does not automatically make a slow query fast. SQL Server still has to read the underlying data needed by the definition. A view can make SQL easier to organize, but query quality, table design, indexes, and data size still matter. Advanced indexed views have different storage and rules; they are outside this beginner lesson.

Views can also hide complexity. If one view reads from several other views and the definitions are not clear, troubleshooting becomes harder. Start with small, understandable definitions and use views where a shared interface has a real benefit. Do not create a view for every one-off query merely to add another object to maintain.

Another sign that a view may not help is when callers need unrelated result shapes. A view with dozens of columns can become a new SELECT * trap if consumers choose everything without thinking. Smaller views organized around a stable concept are easier to explain, while splitting one useful interface into many tiny objects can also make navigation harder. Choose the simplest definition that meets a repeated need.

Column names in a view should be clear and stable. If an expression has no useful name, assign an alias in the SELECT list. Avoid SELECT * in a view definition: the view’s exposed columns should be deliberate, and future table changes should not unexpectedly change its interface. When the underlying schema changes, review and alter dependent views as part of that change.

Administrative note: dropping a view

Dropping an object is an administrative action. Do not use the following as casual practice against a shared database. Confirm the database, object, dependencies, permissions, and change approval first. In a disposable learning database only, the syntax is:

SQL query: Administrative note: dropping a viewT-SQL
-- Administrative example only; do not run casually.
DROP VIEW dbo.vw_CustomerOrders;
T-SQL2 linesRead-only query display. It is not connected to a database.

For ordinary learning, create the view, query it, and alter it. You do not need to drop it to complete this lesson.

Practice and next step

Create the view in JaviBuildsLab, select a few columns from it, filter completed orders, and add an ORDER BY to the query that reads it. Then use ALTER VIEW to expose City and confirm that the result has changed. Think about which part is stored (the definition) and which part comes from the current base-table rows (the result).

  1. Customers and Orders
  2. View query definition
  3. SELECT from the view
A query becomes a view

Views package reusable SELECT statements, while stored procedures package operations that can accept parameters and perform a sequence of statements. The next lesson introduces that second database object: stored procedures in SQL Server. Return to the SQL Server Foundations roadmap to see the full learning path.

Check what you learned

2 quick questions

Question 1 of 2What is a SQL Server view?
Question 2 of 2What does a standard view usually store?

SQL Server Foundations

Lesson 21 of 25

Continue to next lesson

Stored Procedures in SQL Server: A Beginner Introduction

Continue to Lesson 22

Tags

  • SQL Server
  • SQL Fundamentals
  • SSMS
  • T-SQL