This post, “what is a View in SQL Server: Simplifying Data Access” with Views is part of series I’m writing called “An Introduction to SQL Server”. In this post we are going to discuss a database object called Views
What is a View?
In SQL Server, a view is essentially a stored query that provides an abstraction layer over the base tables. It simplifies data access by allowing users to focus on the data they need without worrying about the complexities of the underlying query. Views can be used to:
- Simplify complex queries by encapsulating them.
- Provide a level of security by restricting access to a predefined set of rows and columns.
- Abstract and encapsulate business logic within the database.
Example: Using Views in SQL Server
Let’s illustrate this concept with an example using the AdventureWorks database. We will start by writing a complex multi-join query, and then we will encapsulate this query into a view.
SQL Server stored query – Multi-Join Query
Suppose we want to retrieve information about sales orders, including details about the products, customers, and salespersons involved in the orders. This would require joining several tables: SalesOrderHeader, SalesOrderDetail, Product, Customer, and SalesPerson.
Here is the SQL query:
SELECT
soh.SalesOrderID,
soh.OrderDate,
soh.TotalDue,
sod.ProductID,
p.Name AS ProductName,
c.CustomerID,
per.FirstName + ' ' + per.LastName AS CustomerName,
sp.BusinessEntityID AS SalesPersonID
FROM
Sales.SalesOrderHeader AS soh
JOIN
Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID
JOIN
Production.Product AS p ON sod.ProductID = p.ProductID
JOIN
Sales.Customer AS c ON soh.CustomerID = c.CustomerID
JOIN
Person.Person AS per ON c.PersonID = per.BusinessEntityID
JOIN
HumanResources.Employee AS sp ON soh.SalesPersonID = sp.BusinessEntityID;;
Creating a View in SQL Server.
To simplify access to this data, we can create a view. A view will encapsulate the complexity of the above query and allow users to retrieve the same data with a simple SELECT statement.
Here is how you can create a view in SQL Server:
CREATE VIEW SalesOrderSummary AS
SELECT
soh.SalesOrderID,
soh.OrderDate,
soh.TotalDue,
sod.ProductID,
p.Name AS ProductName,
c.CustomerID,
c.FirstName + ' ' + c.LastName AS CustomerName,
sp.SalesPersonID
FROM
Sales.SalesOrderHeader AS soh
JOIN
Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID
JOIN
Production.Product AS p ON sod.ProductID = p.ProductID
JOIN
Sales.Customer AS c ON soh.CustomerID = c.CustomerID
JOIN
Sales.SalesPerson AS sp ON soh.SalesPersonID = sp.BusinessEntityID;
Using the View
Once the view is created, you can use it just like a regular table. Here’s an example of how to retrieve data from the view:
SELECT
SalesOrderID,
OrderDate,
TotalDue,
ProductID,
ProductName,
CustomerID,
CustomerName,
SalesPersonID
FROM
SalesOrderSummary;
Benefits of Using Views
- Simplicity: Users can retrieve complex datasets with simple SELECT statements.
- Reusability: The same view can be used in multiple queries or applications.
- Security: Views can restrict user access to specific columns and rows, enhancing data security.
- Maintainability: Changes to the underlying query logic only need to be made in one place (the view), rather than in multiple queries.
Conclusion
In conclusion, views are a powerful feature in SQL Server that can help simplify complex queries, enhance security, and improve the maintainability of your database applications. By using views, you can create a more user-friendly and secure database environment.

0 Comments