📘Introducing SQL Notebooks: Tell stories using data.Read more →

How to Create a View in SQL Server in SQL Server

Views allow to encapsulate or “hide” complexities, or allow limited read access to part of the data.

To create a view, use the CREATE VIEW command:

-- syntax
create view view_name
as select_statement;

Some examples:

-- a view to show only products within category 1
create view category_1_products_v as
select *
from products
where category = 1;

-- a view to limit read access to only certain columns
create view category_products_basic_v as
select name, category, price
from products;

-- a view that displays top 10 products that provide highest sold value
create view top_10_products_v as
  top 10 p.name, 
  p.price * ps.quantity_sold as sold_value
from products p
left join products_sold ps on p.id=ps.product_id
order by sold_value desc;

Ready for a modern SQL editor?