CREATE VIEW ActiveCustomers AS
SELECT CustomerId, Name, Email
FROM Customers
WHERE IsActive = 1;
SELECT * FROM ActiveCustomers;
Topics
43
Aggregate Functions & GROUP BY
Always On Availability Groups
Backup & Recovery
Change Data Capture (CDC)
Columnstore Indexes
Common Table Expressions (CTEs)
Constraints (Primary Key, Foreign Key, Check & Unique)
Cursors
Data Types & Schema Design
Deadlocks
Dynamic Data Masking
Dynamic SQL
Error Handling with TRY CATCH
Full Text Search
Hash Indexes & Hash Join Operations
Indexes
In-Memory OLTP (Memory-Optimized Tables)
Isolation & Locking
Joins
JSON Support in SQL Server
Linked Servers
Merge Statement (Upsert)
Normalization
Partitioning
Pivoting & Unpivoting Data
Query Optimization & Plans
Query Store
Replication
SQL Server Agent & Job Scheduling
SQL Server Architecture & Editions
SQL Server Profiler & Extended Events
SQL Server Security & Permissions
Stored Procedures & Functions
Subqueries
Temporary Tables & Table Variables
Transactions & ACID
Transparent Data Encryption
Triggers
T-SQL Fundamentals & Syntax
User Defined Functions
Views
Window Functions
XML Data Type & Querying
Views
7 questions found
A view is a saved, named query that you can treat like a virtual table, letting you simplify complex queries by hiding their underlying complexity behind a simple name, restrict which columns or rows users can see for security purposes, and provide a consistent, reusable way to access commonly needed data combinations.
Real-world example
A company creates a view that only exposes active customers with their name and email, letting the reporting team easily query this filtered, simplified view without needing to understand or repeat the underlying filtering logic every time.
Data Types & Schema Design;SQL Server Security & Permissions
Can you insert, update, or delete data through a view, and what conditions must be met for this to work?
BeginnerYou can modify data through a view as long as it references only a single underlying table and does not include certain features like GROUP BY, DISTINCT, or aggregate functions, since SQL Server needs to be able to clearly translate your change back to the correct row in the actual underlying table.
CREATE VIEW SimpleCustomerView AS
SELECT CustomerId, Name, Email FROM Customers;
UPDATE SimpleCustomerView SET Email = 'new@example.com' WHERE CustomerId = 123;
Real-world example
A support application updates a customer's email address through a simple view rather than the underlying table directly, which works correctly since the view is based on a single table without any aggregation or complex logic.
Data Types & Schema Design;Constraints (Primary Key
Foreign Key
Check & Unique)
What is a schema bound view, and what specific benefit does the WITH SCHEMABINDING option provide?
IntermediateA schema bound view is created using the WITH SCHEMABINDING option, which prevents the underlying tables referenced by the view from being altered or dropped in a way that would break the view's definition, providing an extra layer of protection and also being a requirement if you want to create an indexed view.
CREATE VIEW dbo.ActiveOrders
WITH SCHEMABINDING
AS
SELECT OrderId, CustomerId, Amount
FROM dbo.Orders
WHERE Status = 'Active';
Real-world example
A team creates a schema bound view over their orders table, preventing a well meaning developer from accidentally dropping a column that the view actually depends on without first being warned by SQL Server.
Constraints (Primary Key
Foreign Key
Check & Unique);Columnstore Indexes
What is an indexed view, and how can it improve the performance of complex, frequently run queries?
IntermediateAn indexed view, also called a materialized view, physically stores the results of a view's query on disk with its own unique clustered index, rather than recalculating the query every time it is accessed, which can dramatically speed up complex aggregations or joins that are queried frequently, at the cost of additional storage and some overhead when the underlying data changes.
CREATE VIEW dbo.SalesSummary
WITH SCHEMABINDING
AS
SELECT ProductId, SUM(Amount) AS TotalSales, COUNT_BIG(*) AS SaleCount
FROM dbo.Sales
GROUP BY ProductId;
CREATE UNIQUE CLUSTERED INDEX IX_SalesSummary ON dbo.SalesSummary(ProductId);
Real-world example
A reporting dashboard uses an indexed view to pre-calculate sales totals by product, delivering near instant results for a calculation that would otherwise require scanning and aggregating millions of rows on every single request.
Columnstore Indexes;Query Optimization & Plans
How would you design a layered set of views to progressively simplify a complex data model for different types of users, such as analysts versus casual business users?
AdvancedYou might create a base set of views that join and clean up the raw underlying tables for analysts who need reasonably detailed access, and then build additional views on top of those base views that further aggregate or simplify the data specifically for casual business users, creating a layered structure where each group interacts with a view appropriately suited to their specific needs and technical comfort level.
-- Base view for analysts
CREATE VIEW dbo.OrderDetails AS SELECT * FROM Orders o JOIN Customers c ON o.CustomerId = c.CustomerId;
-- Simplified view built on top, for business users
CREATE VIEW dbo.MonthlySalesSummary AS
SELECT YEAR(OrderDate) AS SalesYear, MONTH(OrderDate) AS SalesMonth, SUM(Amount) AS TotalSales
FROM dbo.OrderDetails GROUP BY YEAR(OrderDate), MONTH(OrderDate);
Real-world example
A company builds a layered set of views, giving their data analysts access to detailed order level views while giving executives a much simpler monthly summary view built directly on top, each seeing exactly the level of detail appropriate for their needs.
SQL Server Security & Permissions;Aggregate Functions & GROUP BY
What happens to the performance and correctness of a view if the underlying tables change significantly after the view was originally created?
AdvancedA regular, non schema bound view does not store any data itself and simply re-executes its underlying query each time it is accessed, meaning it will always reflect the current structure and data of the underlying tables, though a view without schema binding can break unexpectedly if a referenced column is renamed or removed, since SQL Server does not automatically prevent this kind of change for unprotected views.
-- A regular view is not protected from underlying changes
CREATE VIEW dbo.CustomerView AS SELECT CustomerId, Name FROM Customers;
-- If the Name column is later renamed, this view will break
Real-world example
A team encounters a broken report and discovers a column referenced by an underlying view had been renamed without anyone realizing the view depended on it, prompting them to add schema binding to their important views going forward.
Constraints (Primary Key
Foreign Key
Check & Unique);Query Optimization & Plans
How do you use a view to help enforce a basic level of security by limiting which columns or rows different groups of users can see?
IntermediateYou create a view that only selects the specific columns and rows a particular group of users should be allowed to access, then grant that group permission on the view itself rather than the underlying table directly, meaning they can only ever see and interact with the data exposed through that carefully controlled view.
CREATE VIEW dbo.PublicCustomerInfo AS
SELECT CustomerId, Name, City FROM Customers;
GRANT SELECT ON dbo.PublicCustomerInfo TO CustomerServiceRole;
Real-world example
A company grants its customer service team access only to a view exposing basic, non sensitive customer information, while keeping direct access to the full customers table, including sensitive financial details, restricted to a smaller group of authorized users.
SQL Server Security & Permissions;Dynamic Data Masking