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;
| EmployeeId | EmployeeName | Department | City |
|---|---|---|---|
| 1 | Rahul Sharma | IT | Indore |
| 2 | Amit Verma | HR | Bhopal |
| 3 | Priya Singh | IT | Pune |
| 4 | Neha Jain | Finance | Indore |
| 5 | Vikas Patel | IT | Mumbai |
| 6 | Ankit Gupta | HR | Indore |
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;
| EmployeeId | EmployeeName | Salary | City |
|---|---|---|---|
| 1 | Rahul Sharma | 80000.00 | Indore |
| 3 | Priya Singh | 90000.00 | Pune |
| 5 | Vikas Patel | 75000.00 | Mumbai |
You can filter, sort and join a view like a table:
SELECT EmployeeName, Salary
FROM vw_ITEmployees
WHERE Salary > 78000
ORDER BY Salary DESC;
| EmployeeName | Salary |
|---|---|
| Priya Singh | 90000.00 |
| Rahul Sharma | 80000.00 |
Proof: a view stores the query, not the data
SELECT OBJECT_DEFINITION(OBJECT_ID('vw_ITEmployees')) AS ViewDefinition;
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';
| EmployeeName | Salary |
|---|---|
| Rahul Sharma | 95000.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;
| EmployeeId | EmployeeName | Department | Salary | City |
|---|---|---|---|---|
| 1 | Rahul Sharma | IT | 95000.00 | Indore |
| 2 | Amit Verma | HR | 50000.00 | Bhopal |
| 3 | Priya Singh | IT | 90000.00 | Pune |
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;
| EmployeeId | EmployeeName | DepartmentName | DepartmentLocation | Salary |
|---|---|---|---|---|
| 1 | Rahul Sharma | IT | Indore | 95000.00 |
| 2 | Amit Verma | HR | Bhopal | 50000.00 |
| 3 | Priya Singh | IT | Indore | 90000.00 |
| 4 | Neha Jain | Finance | Mumbai | 65000.00 |
| 5 | Vikas Patel | IT | Indore | 75000.00 |
| 6 | Ankit Gupta | HR | Bhopal | 55000.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';
| EmployeeName | City |
|---|---|
| Vikas Patel | Indore |
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');
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;
| Department | Employees | TotalSalary |
|---|---|---|
| Finance | 1 | 65000.00 |
| HR | 2 | 105000.00 |
| IT | 3 | 260000.00 |
UPDATE vw_DepartmentSalary SET TotalSalary = 0 WHERE Department = 'HR';
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';
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;
| EmployeeName | Department |
|---|---|
| Rahul Sharma | IT |
| Priya Singh | IT |
| Vikas Patel | IT |
| Sara Khan | IT |
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;
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;
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;
| Department | Employees | TotalSalary |
|---|---|---|
| Finance | 1 | 65000.00 |
| HR | 2 | 105000.00 |
| IT | 4 | 332000.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');
| IndexName | RowsStored |
|---|---|
| IX_vw_DepartmentTotals | 3 |
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(*), notCOUNT(*). - The session must have the right SET options, especially
QUOTED_IDENTIFIER ON, which SSMS uses by default. Running the same script withsqlcmdwithout the-Ioption 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
Employeesnow 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
| Use | What it gives you |
|---|---|
| Simplify queries | Avoid repeating complex SELECT statements |
| Security | Expose selected columns instead of the entire table |
| Reusability | Multiple applications can use the same view |
| Reporting | Create views for dashboards and reports |
| Abstraction | Hide the complexity of joins and calculations |
Advantages of views
| Advantage | Explanation | Example |
|---|---|---|
| 1. Query reusability | Write a complex query once and reuse it. | Reuse the employee details query. |
| 2. Data security | Show only selected columns to users. Grant SELECT on the view, not on the table. | Hide employee salary information. |
| 3. Simplifies complex queries | Hide complicated joins and conditions. | Combine the Employee and Department tables. |
| 4. Easy maintenance | Change the view definition in one place. | Change the employee report query. |
| 5. Data abstraction | Users don't need to know the underlying table structure. | The application queries one view instead of several tables. |
| 6. Reporting | Useful for reports and dashboards. | Department-wise employee reports. |
| 7. Consistent results | Different applications use the same query definition. | HR and management reports agree. |
Disadvantages of views
| Disadvantage | Explanation |
|---|---|
| 1. Performance overhead | A complex view can produce an expensive execution plan, especially when you select one column from a view that joins five tables. |
| 2. Dependency | If an underlying table or column changes, the view can break (Msg 207 above) or become outdated. |
| 3. Limited modification | Views with GROUP BY, aggregates or complex joins can't be updated directly (Msg 4406 above). |
| 4. Debugging complexity | Troubleshooting nested views can be difficult. |
| 5. No automatic performance gain | A normal view doesn't make a query faster. |
| 6. Nested views | Views that reference other views make queries hard to understand and maintain. |
| 7. Indexed view maintenance | Indexed 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.