Lesson 12 — Database Design
Views
A view is a saved, named query that behaves like a virtual table, letting you reuse complex logic and simplify what other queries and users see.
Creating and Querying a View
Once created, a view can be queried exactly like a regular table, hiding the underlying joins and calculations from whoever is using it.
T-SQL — Creating a view
CREATE VIEW ActiveCustomerOrders AS
SELECT c.FirstName, c.LastName, o.OrderId, o.Total, o.OrderDate
FROM Customers c
INNER JOIN Orders o ON c.CustomerId = o.CustomerId
WHERE c.IsActive = 1;
-- Later, query it like a table:
SELECT * FROM ActiveCustomerOrders WHERE Total > 500;
Common Mistakes
- Treating a view as if it stores its own data — by default it's a saved query, re-executed every time it's referenced.
- Building deeply nested views (views built on views on views), which can hurt performance and make debugging difficult.
- Forgetting to update a view after the underlying table structure changes, leaving it broken or outdated.
- Using a view purely for convenience where a properly indexed table or indexed view would perform meaningfully better under heavy load.
Professional Tip
Views are an excellent way to expose a simplified, safe subset of a complex schema to reporting tools or less-privileged users, without duplicating the underlying join logic in every report.
Your Turn
Create a view called ProductCatalog that joins Products and Categories to show each product's name, price, and category name, then query it filtering for a specific category.
Mini Quiz
What is a T-SQL view, by default?
By default, a standard view stores only the query definition. It does not store data itself — the underlying query runs fresh each time the view is used, unless it's specifically defined as an indexed view.