How to Create a View 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
select
  top 10 p.name,
  p.category,
  p.price,
  ps.quantity_sold,
  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;
database icon
Real-time SQL collaboration is here
Get started with PopSQL and SQL Server in minutes