Paramclasses Academy - Blog

Tutorials Details

←
Post 25 of 40
→

SQL Server Views Tutorial: How to Create, Alter and Use Views with Practical Examples

SQL Server Views Tutorial: How to Create, Alter and Use Views with Practical Examples

November 18, 2026

 

???? SQL Server Views Tutorial: Create, Alter and Use Views

???? Introduction

In Microsoft SQL Server, a View is a virtual table that displays data using a stored SELECT query. It allows database developers to simplify complex queries, display selected columns, combine information from multiple tables, and control how users access database information.

Instead of writing the same SELECT query repeatedly, you can create a View and use it whenever required.

For example, an E-Commerce application may contain separate tables for Customers, Products, and Orders. If you frequently need to display customer names along with their order details, you can create a View that combines this information.

In this tutorial, we will learn how to CREATE, ALTER, USE, and DROP Views in SQL Server with practical examples.


???? What You Will Learn

After completing this tutorial, you will understand:

  • What a View is in SQL Server.

  • Why Views are used in database development.

  • Difference between a Table and a View.

  • How to create a View using CREATE VIEW.

  • How to retrieve data from a View.

  • How to modify a View using ALTER VIEW.

  • How to delete a View using DROP VIEW.

  • How to create Views using multiple tables and JOIN.

  • How to create filtered Views using WHERE.

  • How to use Views for security and simplified reporting.

  • Important SQL Server View limitations and best practices.


???? 1. What Is a View in SQL Server?

A View is a virtual table whose rows and columns are defined by a SELECT statement.

A View does not normally store a separate copy of the result data. Instead, SQL Server executes the underlying query when the View is referenced.

Example

Suppose we have an Employees table containing:

EmployeeID EmployeeName Department Salary
1 Rahul IT 35000
2 Priya HR 30000
3 Amit Sales 40000

If an application needs only EmployeeName and Department, we can create a View that displays those columns.

CREATE VIEW vw_EmployeeDetails
AS
SELECT
    EmployeeName,
    Department
FROM Employees;

Now we can retrieve the information using:

SELECT * FROM vw_EmployeeDetails;

The View displays only the selected columns.


???? 2. Why Do We Use Views in SQL Server?

Views are useful for several database development tasks.

✅ 1. Simplify Complex Queries

If a query contains multiple JOIN operations, you can save it as a View and reuse it.

✅ 2. Improve Data Security

You can give users permission to access a View without giving them direct permission to access the underlying base tables.

✅ 3. Display Selected Columns

A View can show only the columns required by a particular user or application.

✅ 4. Reuse SQL Queries

Instead of writing the same SELECT statement repeatedly, users can query the View.

✅ 5. Support Reporting

Views can combine information from multiple tables to provide a convenient reporting interface.

???? Important Note

A standard View is not automatically a performance optimization or a separate data-storage mechanism. SQL Server generally retrieves the underlying data when the View is queried.


???? 3. Difference Between a Table and a View

Table View
Stores data in rows and columns Presents data through a stored query
Data is stored in the table Standard View normally does not store a separate result copy
Can be used to insert records Some Views support data modifications
Contains its own table structure Uses columns from its SELECT definition
Can have Primary Keys and constraints Does not have ordinary table-style Primary Key constraints

Example: Customers is a table, while vw_CustomerDetails can be a View created from the Customers table.


???? 4. Create a Database and Sample Table

Before creating a View, let's create a sample database and Employees table.

Step 1: Create Database

CREATE DATABASE ViewDemoDB;
GO

USE ViewDemoDB;
GO

Step 2: Create Employees Table

CREATE TABLE Employees
(
    EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
    EmployeeName NVARCHAR(100) NOT NULL,
    Department VARCHAR(50),
    Salary DECIMAL(10,2),
    City VARCHAR(50)
);

Step 3: Insert Sample Records

INSERT INTO Employees
(EmployeeName, Department, Salary, City)
VALUES
('Rahul Sharma', 'IT', 35000, 'Raipur'),
('Priya Verma', 'HR', 30000, 'Delhi'),
('Amit Kumar', 'Sales', 40000, 'Mumbai'),
('Neha Singh', 'IT', 45000, 'Raipur'),
('Rohit Patel', 'Marketing', 32000, 'Bhopal');

Step 4: Display Table Records

SELECT * FROM Employees;

Now the Employees table contains sample employee information.


???? 5. How to Create a View Using CREATE VIEW

The CREATE VIEW statement is used to create a new View in SQL Server.

Syntax

CREATE VIEW ViewName
AS
SELECT Column1, Column2
FROM TableName;

Practical Example

Let's create a View that displays employee names and departments.

CREATE VIEW vw_EmployeeDetails
AS
SELECT
    EmployeeID,
    EmployeeName,
    Department
FROM Employees;
GO

Explanation

  • CREATE VIEW: Creates a new View.

  • vw_EmployeeDetails: Name of the View.

  • AS: Introduces the SELECT query.

  • SELECT: Specifies the columns to display.

  • FROM Employees: Defines the source table.

The View is now available in the database.


???? 6. How to Retrieve Data from a View

Once a View has been created, you can query it just like a table.

Example 1: Display All View Records

SELECT * FROM vw_EmployeeDetails;

Example 2: Display Selected Columns

SELECT
    EmployeeName,
    Department
FROM vw_EmployeeDetails;

Example 3: Filter View Records

SELECT *
FROM vw_EmployeeDetails
WHERE Department = 'IT';

Example 4: Sort View Records

SELECT *
FROM vw_EmployeeDetails
ORDER BY EmployeeName ASC;

Remember: Use ORDER BY in the query that reads the View when you need a particular result order. A View definition does not guarantee the order of returned rows.


???? 7. Create a View with WHERE Clause

You can use a WHERE condition inside a View to display only records that match a specific condition.

Example: Create an IT Employees View

CREATE VIEW vw_ITEmployees
AS
SELECT
    EmployeeID,
    EmployeeName,
    Department,
    Salary
FROM Employees
WHERE Department = 'IT';
GO

Retrieve Records

SELECT * FROM vw_ITEmployees;

This View displays only employees whose department is IT.

Another Example: Employees with Salary Greater Than 35000

CREATE VIEW vw_HighSalaryEmployees
AS
SELECT
    EmployeeName,
    Department,
    Salary
FROM Employees
WHERE Salary > 35000;
GO
SELECT * FROM vw_HighSalaryEmployees;

???? 8. Create a View Using Multiple Tables and JOIN

One of the most useful features of SQL Server Views is that they can combine data from multiple tables.

Let's create a Customers table and an Orders table.

Step 1: Create Customers Table

CREATE TABLE Customers
(
    CustomerID INT PRIMARY KEY,
    CustomerName NVARCHAR(100),
    City VARCHAR(50)
);

Step 2: Create Orders Table

CREATE TABLE Orders
(
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderAmount DECIMAL(10,2),

    FOREIGN KEY (CustomerID)
    REFERENCES Customers(CustomerID)
);

Step 3: Insert Sample Records

INSERT INTO Customers
(CustomerID, CustomerName, City)
VALUES
(1, 'Rahul Sharma', 'Raipur'),
(2, 'Priya Verma', 'Delhi'),
(3, 'Amit Kumar', 'Mumbai');
INSERT INTO Orders
(OrderID, CustomerID, OrderAmount)
VALUES
(101, 1, 5000),
(102, 2, 7500),
(103, 1, 3000),
(104, 3, 9000);

Step 4: Create View Using INNER JOIN

CREATE VIEW vw_CustomerOrders
AS
SELECT
    C.CustomerID,
    C.CustomerName,
    C.City,
    O.OrderID,
    O.OrderAmount
FROM Customers C
INNER JOIN Orders O
ON C.CustomerID = O.CustomerID;
GO

Step 5: Retrieve Customer Order Details

SELECT * FROM vw_CustomerOrders;

Expected Output

CustomerName City OrderID OrderAmount
Rahul Sharma Raipur 101 5000
Priya Verma Delhi 102 7500
Rahul Sharma Raipur 103 3000
Amit Kumar Mumbai 104 9000

This View combines information from two different tables into one convenient result.


???? 9. How to Modify a View Using ALTER VIEW

The ALTER VIEW statement is used to change an existing View definition.

Suppose we created vw_EmployeeDetails earlier, but now we also want to display the City column.

Syntax

ALTER VIEW ViewName
AS
SELECT Column1, Column2
FROM TableName;

Practical Example

ALTER VIEW vw_EmployeeDetails
AS
SELECT
    EmployeeID,
    EmployeeName,
    Department,
    City
FROM Employees;
GO

Retrieve Updated View

SELECT * FROM vw_EmployeeDetails;

Now the View also displays the City column.

???? Important

ALTER VIEW modifies the existing View definition. It does not require dropping and recreating the View, so permissions associated with the View can be preserved.


???? 10. How to Create or Update a View Using CREATE OR ALTER VIEW

SQL Server supports CREATE OR ALTER VIEW starting with SQL Server 2016 SP1.

It creates the View if it does not exist, or modifies it if it already exists.

Example

CREATE OR ALTER VIEW vw_EmployeeDetails
AS
SELECT
    EmployeeID,
    EmployeeName,
    Department,
    Salary,
    City
FROM Employees;
GO

This statement is useful when deploying database scripts repeatedly.


???? 11. How to Delete a View Using DROP VIEW

If a View is no longer required, you can remove it using DROP VIEW.

Syntax

DROP VIEW ViewName;

Practical Example

DROP VIEW vw_ITEmployees;

This removes the vw_ITEmployees View from the database.

Safe Alternative: Check Before Dropping

DROP VIEW IF EXISTS vw_ITEmployees;

This avoids an error if the View does not exist.

Note: Dropping a View does not delete the underlying table records.


???? 12. How to Create a View Using SQL Server Management Studio (SSMS)

You can create Views using the graphical interface of SQL Server Management Studio.

Step-by-Step Process

Step 1: Open SQL Server Management Studio.

Step 2: Connect to your SQL Server instance.

Step 3: Expand the required database.

Step 4: Expand the Views folder.

Step 5: Right-click Views.

Step 6: Select New View.

Step 7: Select the required tables, Views, or other supported objects.

Step 8: Add the required columns and conditions using the designer.

Step 9: Save the View with a meaningful name.

Step 10: Execute a SELECT query to verify the View.

Example

After creating vw_EmployeeDetails, execute:

SELECT * FROM vw_EmployeeDetails;

Microsoft SQL Server Management Studio also allows you to view and modify the View definition.


???? 13. How to View Existing Views in SQL Server

You can find all Views in the current database using system catalog views.

Example 1: Display View Names

SELECT
    name,
    object_id,
    create_date,
    modify_date
FROM sys.views;

Example 2: Display Views from a Specific Schema

SELECT
    SCHEMA_NAME(schema_id) AS SchemaName,
    name AS ViewName
FROM sys.views;

Example 3: View the Definition of a View

EXEC sp_helptext 'vw_EmployeeDetails';

You can also use:

SELECT OBJECT_DEFINITION(
    OBJECT_ID('dbo.vw_EmployeeDetails')
) AS ViewDefinition;

These queries help developers inspect existing database Views.


???? 14. Can We INSERT, UPDATE, and DELETE Data Through a View?

Yes, certain Views support data modifications.

A simple View based on one table can often support INSERT, UPDATE, and DELETE operations, provided the modification follows SQL Server's updatable View rules.

Example: Update Data Through a View

UPDATE vw_EmployeeDetails
SET Department = 'Finance'
WHERE EmployeeID = 1;

The underlying Employees table is updated.

Example: Insert Data Through a Simple View

INSERT INTO vw_EmployeeDetails
(EmployeeName, Department, City)
VALUES
('Karan Mehta', 'IT', 'Raipur');

This works only when the View and the required underlying table columns permit the insert. For example, columns omitted from the View must have suitable defaults, allow NULL, or be generated automatically.

Example: Delete Data Through a View

DELETE FROM vw_EmployeeDetails
WHERE EmployeeID = 5;

This deletes the corresponding row from the underlying Employees table.

⚠️ Important

Not every View is updatable. Views involving GROUP BY, aggregate functions, DISTINCT, and certain joins or computed expressions may prevent direct data modifications.

For complex modification requirements, an INSTEAD OF trigger may be an option.


???? 15. What Is WITH CHECK OPTION in SQL Server Views?

The WITH CHECK OPTION clause ensures that modifications made through a View continue to satisfy the View's WHERE condition.

Example

CREATE VIEW vw_RaipurEmployees
AS
SELECT
    EmployeeID,
    EmployeeName,
    Department,
    City
FROM Employees
WHERE City = 'Raipur'
WITH CHECK OPTION;
GO

Now try:

UPDATE vw_RaipurEmployees
SET City = 'Delhi'
WHERE EmployeeID = 1;

SQL Server rejects this modification because the updated row would no longer satisfy the View's condition.

Purpose: WITH CHECK OPTION helps ensure that changes made through the View remain visible through that View.


???? 16. Important Limitations of SQL Server Views

Before using Views in real-world applications, understand these limitations:

  1. A standard View normally does not store a separate copy of the result data.

  2. A View can have a maximum of 1,024 columns.

  3. A View can be created only in the current database.

  4. ORDER BY cannot normally be used in a View definition unless it is associated with TOP or OFFSET as permitted by the syntax; result ordering must be specified in the outer query.

  5. Temporary tables and table variables cannot be referenced in a View definition.

  6. Not all Views support direct INSERT, UPDATE, or DELETE operations.

  7. If underlying tables are changed, dependent Views may need to be updated or refreshed.


???? 17. View vs Stored Procedure

View Stored Procedure
Stores a SELECT query definition Stores one or more SQL statements
Queried like a table Executed using EXEC
Does not accept parameters Can accept input and output parameters
Primarily used to present data Used for database operations and business logic
Cannot contain general procedural control flow Can use IF, WHILE, and other procedural statements

Example: View

SELECT * FROM vw_EmployeeDetails;

Example: Stored Procedure

CREATE PROCEDURE sp_GetEmployees
AS
BEGIN
    SELECT * FROM Employees;
END;
GO

Execute it using:

EXEC sp_GetEmployees;

???? 18. SQL Server View Best Practices

Follow these practices while creating Views:

✅ 1. Use Meaningful Names

Use names such as vw_EmployeeDetails or vw_CustomerOrders.

✅ 2. Avoid SELECT *

Specify only the columns required by the View.

✅ 3. Keep View Definitions Simple

Avoid unnecessary complexity and nested Views when a simpler query can solve the problem.

✅ 4. Use Views for Access Control

Grant users access to appropriate Views when they do not need direct access to all base-table columns.

✅ 5. Test Views After Table Changes

Verify dependent Views whenever the structure of underlying tables changes.

✅ 6. Use Schema Names

Prefer names such as dbo.vw_EmployeeDetails for clarity.

✅ 7. Do Not Assume Views Automatically Improve Performance

A standard View generally uses its underlying query. Measure actual query performance and optimize the underlying tables and queries when needed.


???? 19. Frequently Asked Questions (FAQs)

Q1. What is a View in SQL Server?

A View is a virtual table defined by a SELECT query. It displays data from one or more underlying tables or Views.

Q2. Does a View store data in SQL Server?

A standard View normally does not store a separate copy of its result data. Indexed Views are a special case that can store indexed results.

Q3. How do I create a View in SQL Server?

Use the CREATE VIEW statement followed by a SELECT query that defines the View.

Q4. What is the difference between CREATE VIEW and ALTER VIEW?

CREATE VIEW creates a new View, while ALTER VIEW changes an existing View definition.

Q5. Can we update data through a View?

Yes, when the View satisfies SQL Server's updatable View conditions. Some complex Views do not support direct data modification.

Q6. How do I delete a View in SQL Server?

Use DROP VIEW followed by the View name.

Q7. Can a View use JOIN?

Yes. A View can combine information from multiple tables using INNER JOIN, LEFT JOIN, and other supported query operations.

Q8. Can we create a View inside another View?

Yes. A View can reference another View, provided the referenced objects and query satisfy SQL Server's requirements.

Q9. What is WITH CHECK OPTION?

It ensures that modifications made through a View satisfy the filtering conditions defined in that View.

Q10. What is the difference between a View and an Indexed View?

A standard View stores its query definition. An Indexed View has a qualifying unique clustered index and stores the indexed result data, subject to SQL Server's requirements.


???? Conclusion

SQL Server Views are an important feature of database development. They help simplify complex queries, present selected information, support reporting, and provide an additional layer of data access control.

In this tutorial, we learned how to create Views using CREATE VIEW, retrieve information from Views, modify existing Views using ALTER VIEW, and remove them using DROP VIEW. We also explored JOIN-based Views, filtered Views, WITH CHECK OPTION, and the conditions for modifying data through Views.

Understanding Views is an important step toward becoming a professional SQL Server developer. At Param Classes, students can improve their database development skills through practical SQL Server tutorials, real-world examples, and hands-on projects.

Keep learning, practice SQL queries, and build your database development skills with Param Classes!

←
Post 25 of 40
→