Skip to content

Views in SQL Server

A view is a virtual table in SQL Server. It is created from a SELECT query on one or more existing tables, and you can query it just like a table.

Think of a view as a saved SQL query with a name. Whenever you query the view, SQL Server runs that query against the underlying tables and returns the current data.

Important: a normal view does not store a separate copy of the table data. It stores only the query definition. (The exception, the indexed view, is covered at the end.)

Every output on this page comes from a real run on SQL Server 2025.

Sample tables

CREATE TABLE dbo.Departments
(
    DepartmentId INT PRIMARY KEY,
    DepartmentName VARCHAR(50) NOT NULL,
    Location VARCHAR(50) NOT NULL
);

CREATE TABLE dbo.Employees
(
    EmployeeId INT IDENTITY(1,1) PRIMARY KEY,
    EmployeeName VARCHAR(100) NOT NULL,
    Department VARCHAR(50) NOT NULL,
    DepartmentId INT NOT NULL REFERENCES dbo.Departments(DepartmentId),
    Salary DECIMAL(18,2) NOT NULL,
    City VARCHAR(50) NOT NULL
);

INSERT INTO dbo.Departments VALUES
(1, 'IT', 'Indore'), (2, 'HR', 'Bhopal'), (3, 'Finance', 'Mumbai');

INSERT INTO dbo.Employees (EmployeeName, Department, DepartmentId, Salary, City) VALUES
('Rahul Sharma', 'IT', 1, 80000, 'Indore'),
('Amit Verma', 'HR', 2, 50000, 'Bhopal'),
('Priya Singh', 'IT', 1, 90000, 'Pune'),
('Neha Jain', 'Finance', 3, 65000, 'Indore'),
('Vikas Patel', 'IT', 1, 75000, 'Mumbai'),
('Ankit Gupta', 'HR', 2, 55000, 'Indore');
GO

Create a view

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

Read data from the view

SELECT *
FROM vw_EmployeeDetails;
Output
EmployeeIdEmployeeNameDepartmentCity
1Rahul SharmaITIndore
2Amit VermaHRBhopal
3Priya SinghITPune
4Neha JainFinanceIndore
5Vikas PatelITMumbai
6Ankit GuptaHRIndore

The view returns the four columns it defines; Salary and DepartmentId stay hidden.

A view with a filter

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

SELECT * FROM vw_ITEmployees;
Output
EmployeeIdEmployeeNameSalaryCity
1Rahul Sharma80000.00Indore
3Priya Singh90000.00Pune
5Vikas Patel75000.00Mumbai

You can filter, sort and join a view like a table:

SELECT EmployeeName, Salary
FROM vw_ITEmployees
WHERE Salary > 78000
ORDER BY Salary DESC;
Output
EmployeeNameSalary
Priya Singh90000.00
Rahul Sharma80000.00

Proof: a view stores the query, not the data

SELECT OBJECT_DEFINITION(OBJECT_ID('vw_ITEmployees')) AS ViewDefinition;
Output
CREATE VIEW vw_ITEmployees
AS
SELECT
    EmployeeId,
    EmployeeName,
    Salary,
    City
FROM Employees
WHERE Department = 'IT';

What SQL Server saved is the SELECT statement. That's why a view always shows current data. Change a salary in the table, and the view reflects it immediately:

UPDATE Employees SET Salary = 95000 WHERE EmployeeName = 'Rahul Sharma';

SELECT EmployeeName, Salary
FROM vw_ITEmployees
WHERE EmployeeName = 'Rahul Sharma';
Output
EmployeeNameSalary
Rahul Sharma95000.00

You can also see a view's definition with EXEC sp_helptext 'vw_ITEmployees', or in SSMS under Database > Views (right-click > Script View as).

ALTER VIEW: change the definition

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

SELECT TOP (3) * FROM vw_EmployeeDetails ORDER BY EmployeeId;
Output
EmployeeIdEmployeeNameDepartmentSalaryCity
1Rahul SharmaIT95000.00Indore
2Amit VermaHR50000.00Bhopal
3Priya SinghIT90000.00Pune

ALTER VIEW replaces the definition but keeps the view's permissions. (CREATE OR ALTER VIEW works too: it creates the view if it doesn't exist.)

A view on multiple tables (JOIN)

CREATE VIEW vw_EmployeeWithDepartment
AS
SELECT
    e.EmployeeId,
    e.EmployeeName,
    d.DepartmentName,
    d.Location AS DepartmentLocation,
    e.Salary
FROM dbo.Employees AS e
INNER JOIN dbo.Departments AS d ON d.DepartmentId = e.DepartmentId;
GO

SELECT * FROM vw_EmployeeWithDepartment ORDER BY EmployeeId;
Output
EmployeeIdEmployeeNameDepartmentNameDepartmentLocationSalary
1Rahul SharmaITIndore95000.00
2Amit VermaHRBhopal50000.00
3Priya SinghITIndore90000.00
4Neha JainFinanceMumbai65000.00
5Vikas PatelITIndore75000.00
6Ankit GuptaHRBhopal55000.00

Applications and report writers can now query vw_EmployeeWithDepartment without knowing the join. This is one of the most common uses of views.

Insert, update and delete through a view

A simple view on one table is usually updatable: the change is applied to the underlying table.

UPDATE vw_ITEmployees
SET City = 'Indore'
WHERE EmployeeName = 'Vikas Patel';

SELECT EmployeeName, City FROM Employees WHERE EmployeeName = 'Vikas Patel';
Output
EmployeeNameCity
Vikas PatelIndore

But there are limits. An INSERT through a view fails if the view hides a NOT NULL column that has no default:

INSERT INTO vw_ITEmployees (EmployeeName, Salary, City)
VALUES ('New Joiner', 60000, 'Pune');
Messages
Msg 515, Level 16, State 2
Cannot insert the value NULL into column 'Department', table 'MMC_BlogDemo.dbo.Employees'; column does not allow nulls. INSERT fails.
The statement has been terminated.

And a view with GROUP BY or aggregates can't be updated at all:

CREATE VIEW vw_DepartmentSalary
AS
SELECT Department, COUNT(*) AS Employees, SUM(Salary) AS TotalSalary
FROM dbo.Employees
GROUP BY Department;
GO

SELECT * FROM vw_DepartmentSalary ORDER BY Department;
Output
DepartmentEmployeesTotalSalary
Finance165000.00
HR2105000.00
IT3260000.00
UPDATE vw_DepartmentSalary SET TotalSalary = 0 WHERE Department = 'HR';
Messages
Msg 4406, Level 16, State 1
Update or insert of view or function 'vw_DepartmentSalary' failed because it contains a derived or constant field.

The rules: a modification through a view may affect only one base table, may not change computed or aggregated columns, and must supply all required columns. For complex views you can add an INSTEAD OF trigger that decides what to do.

WITH CHECK OPTION: keep rows inside the view

Without it, you could update a row through vw_ITEmployees so that it's no longer IT, and it would silently disappear from the view. WITH CHECK OPTION prevents that:

CREATE VIEW vw_ITEmployeesChecked
AS
SELECT EmployeeId, EmployeeName, Department, DepartmentId, Salary, City
FROM dbo.Employees
WHERE Department = 'IT'
WITH CHECK OPTION;
GO

UPDATE vw_ITEmployeesChecked
SET Department = 'HR'
WHERE EmployeeName = 'Priya Singh';
Messages
Msg 550, Level 16, State 1
The attempted insert or update failed because the target view either specifies WITH CHECK OPTION or spans a view that specifies WITH CHECK OPTION and one or more rows resulting from the operation did not qualify under the CHECK OPTION constraint.
The statement has been terminated.

Rows that satisfy the view's filter are still allowed:

INSERT INTO vw_ITEmployeesChecked (EmployeeName, Department, DepartmentId, Salary, City)
VALUES ('Sara Khan', 'IT', 1, 72000, 'Indore');

SELECT EmployeeName, Department FROM vw_ITEmployeesChecked ORDER BY EmployeeId;
Output
EmployeeNameDepartment
Rahul SharmaIT
Priya SinghIT
Vikas PatelIT
Sara KhanIT

The dependency problem, and SCHEMABINDING

A normal view doesn't stop anyone from changing the tables it reads. If a column it uses is dropped, the view breaks, and you only find out when someone queries it:

CREATE TABLE dbo.Projects (ProjectId INT PRIMARY KEY, ProjectName VARCHAR(50), Budget DECIMAL(18,2));
GO
CREATE VIEW vw_Projects AS SELECT ProjectId, ProjectName, Budget FROM dbo.Projects;
GO
ALTER TABLE dbo.Projects DROP COLUMN Budget;   -- succeeds
GO
SELECT * FROM vw_Projects;
Messages
Msg 207, Level 16, State 1, Procedure vw_Projects, Line 1
Invalid column name 'Budget'.
Msg 4413, Level 16, State 1
Could not use view or function 'vw_Projects' because of binding errors.

WITH SCHEMABINDING ties the view to the table's structure, so the dangerous change is blocked instead:

CREATE VIEW dbo.vw_ProjectsBound
WITH SCHEMABINDING
AS
SELECT ProjectId, ProjectName, Budget FROM dbo.Projects;
GO

ALTER TABLE dbo.Projects DROP COLUMN Budget;
Messages
Msg 5074, Level 16, State 1
The object 'vw_ProjectsBound' is dependent on column 'Budget'.
Msg 4922, Level 16, State 9
ALTER TABLE DROP COLUMN Budget failed because one or more objects access this column.

Schema-bound views must use two-part names (dbo.Projects) and list their columns explicitly (no SELECT *).

Indexed views: a view that does store data

Add a unique clustered index to a schema-bound view, and SQL Server physically stores the view's result and keeps it up to date whenever the base table changes. This can make expensive aggregations instant.

CREATE VIEW dbo.vw_DepartmentTotals
WITH SCHEMABINDING
AS
SELECT Department, COUNT_BIG(*) AS Employees, SUM(Salary) AS TotalSalary
FROM dbo.Employees
GROUP BY Department;
GO

CREATE UNIQUE CLUSTERED INDEX IX_vw_DepartmentTotals
ON dbo.vw_DepartmentTotals(Department);
GO

SELECT Department, Employees, TotalSalary
FROM dbo.vw_DepartmentTotals WITH (NOEXPAND)
ORDER BY Department;
Output
DepartmentEmployeesTotalSalary
Finance165000.00
HR2105000.00
IT4332000.00
SELECT i.name AS IndexName, p.rows AS RowsStored
FROM sys.indexes i
JOIN sys.partitions p ON p.object_id = i.object_id AND p.index_id = i.index_id
WHERE i.object_id = OBJECT_ID('dbo.vw_DepartmentTotals');
Output
IndexNameRowsStored
IX_vw_DepartmentTotals3

The index physically stores 3 rows, one per department. (IT now shows 4 employees because Sara Khan was added above.)

  • An aggregated indexed view must use COUNT_BIG(*), not COUNT(*).
  • The session must have the right SET options, especially QUOTED_IDENTIFIER ON, which SSMS uses by default. Running the same script with sqlcmd without the -I option failed with: "Cannot create index. Object 'vw_DepartmentTotals' was created with the following SET options off: 'QUOTED_IDENTIFIER'."
  • WITH (NOEXPAND) tells SQL Server to read the stored index. Enterprise Edition can use indexed views automatically; other editions, including Express, need the hint.
  • Every INSERT, UPDATE and DELETE on Employees now also updates the view's index, so indexed views make writes slower.

DROP VIEW

DROP VIEW vw_EmployeeDetails;
GO

Dropping a view removes only the definition. The data in Employees is untouched. Use DROP VIEW IF EXISTS in scripts you run repeatedly.

Why use views

UseWhat it gives you
Simplify queriesAvoid repeating complex SELECT statements
SecurityExpose selected columns instead of the entire table
ReusabilityMultiple applications can use the same view
ReportingCreate views for dashboards and reports
AbstractionHide the complexity of joins and calculations

Advantages of views

AdvantageExplanationExample
1. Query reusabilityWrite a complex query once and reuse it.Reuse the employee details query.
2. Data securityShow only selected columns to users. Grant SELECT on the view, not on the table.Hide employee salary information.
3. Simplifies complex queriesHide complicated joins and conditions.Combine the Employee and Department tables.
4. Easy maintenanceChange the view definition in one place.Change the employee report query.
5. Data abstractionUsers don't need to know the underlying table structure.The application queries one view instead of several tables.
6. ReportingUseful for reports and dashboards.Department-wise employee reports.
7. Consistent resultsDifferent applications use the same query definition.HR and management reports agree.

Disadvantages of views

DisadvantageExplanation
1. Performance overheadA complex view can produce an expensive execution plan, especially when you select one column from a view that joins five tables.
2. DependencyIf an underlying table or column changes, the view can break (Msg 207 above) or become outdated.
3. Limited modificationViews with GROUP BY, aggregates or complex joins can't be updated directly (Msg 4406 above).
4. Debugging complexityTroubleshooting nested views can be difficult.
5. No automatic performance gainA normal view doesn't make a query faster.
6. Nested viewsViews that reference other views make queries hard to understand and maintain.
7. Indexed view maintenanceIndexed views use storage and make INSERT, UPDATE and DELETE on the base tables slower.

Interview point: a view is not automatically faster than the equivalent SELECT. A normal view is expanded into its underlying query when executed, so it gets the same execution plan. Only an indexed view stores results, with extra storage and maintenance cost.

Interview questions

1. Does a view store data in SQL Server?

A normal view doesn't store a separate copy of the data; it stores only the query definition (you can see it with OBJECT_DEFINITION or sp_helptext). An indexed view is the exception: its result is stored in its clustered index.

2. Can we insert, update and delete records through a view?

Yes, under certain conditions. Simple views on a single table are usually updatable. The change may affect only one base table, can't touch aggregated or computed columns, and an INSERT must supply all NOT NULL columns. Complex views need an INSTEAD OF trigger.

3. Does a view improve query performance?

Not automatically. A normal view performs like its underlying query. Indexed views can speed up reads of expensive aggregations, at the cost of storage and slower writes.

4. What is an indexed view?

A schema-bound view with a unique clustered index. SQL Server physically stores and maintains its results. On non-Enterprise editions, query it with WITH (NOEXPAND).

5. Can we create a view on multiple tables?

Yes, using JOINs, like vw_EmployeeWithDepartment above. Updates through such a view may affect only one of the tables at a time.

6. What does WITH CHECK OPTION do?

It rejects inserts and updates through the view that would produce rows the view itself can't see, so data stays consistent with the view's WHERE clause.

7. What is SCHEMABINDING?

It binds the view to the structure of its tables, so they can't be changed in a way that breaks the view. It's required for indexed views.

8. What is the difference between a view and a table?

A table stores data physically; a normal view stores only a query and shows the tables' current data when queried. A view can't have its own data, defaults or (except indexed views) indexes.

Summary

  • A view is a saved SELECT query that behaves like a virtual table; it always shows current data.
  • Use CREATE, ALTER (or CREATE OR ALTER) and DROP VIEW to manage views.
  • Simple single-table views are updatable; aggregated views are not; WITH CHECK OPTION keeps changes inside the view.
  • SCHEMABINDING protects a view from breaking table changes.
  • Only indexed views store data; normal views are not faster than their query.

Related: Stored procedures, user-defined functions and cursors.