sql views

Updated : Oct 12, 2019 in SQL server

Create Views in SQL server with examples

Introduction views in SQL server:

In this article, we will discuss the Create views in SQL server with examples and how views are to improve performance

Views In SQL server are it actually saves you a lot of time by actually not building similar tables or same tables for different needs.

Views can be actually created joining multiple tables

create a view in SQL databases means simply we can say we are going to create as a virtual base table

what are the tables we created in database that every table is called a “Base table”

when I’m going to accessing multiple times my base table data it was giving some low performance and reducing your performance.

Views in SQL Server syntax:
CREATE VIEW [ <schema_name>. ] <view_name> [ ( column [, …n ] ) ]
AS <select_statement>

Download the script and Run Script

But every request I want to send it to my base table and retrieve the data so instead of that now I’m created one “view”

Views in SQL Server with joins
CREATE  VIEW [dbo].[V_Orders] AS
select  [FirstName],[Customer].[City],[ProductName],[Product].[UnitPrice],[OrderNumber],[CompanyName],[ContactName],[Supplier].[Country]
from [dbo].[Customer]  inner join  [dbo].[Order]
on [dbo].[Customer].Id=[dbo].[Order].Id inner join 
[dbo].[OrderItem] on [dbo].[OrderItem].OrderId=[dbo].[Order].Id inner join
[dbo].[Product] on [dbo].[Product].Id=[dbo].[OrderItem].ProductId  inner join
[dbo].[Supplier] on [dbo].[Supplier].Id=[dbo].[Product].SupplierId
select *from V_Orders
views in SQL server examples
views in SQL server examples

the above example what is happening here I am not interacting with the main table or base tables okay it’s not necessary to interact with the base table instead of that I’m going to accessing each and everything through the view

views are a symbol I can say it is a virtual or else even though we can say logical is a virtual or logical table

Views in SQL Examples

Find the total sum of unit-price belongs to the city name “Kirkland”  or the base table.

select SUM(V_Orders.UnitPrice)as totalcount from V_Orders where V_Orders.City='kirkland' group by City
Views In SQL Server Examples
Views In SQL Server Examples

Find the details of the order only belongs to the country is “spain”

  select *from V_Orders where V_Orders.Country='Spain'
Views In SQL
Views In SQL

To find the details of the orders which do not belong to the country name “Spain”

select *from V_Orders where V_Orders.Country <>'Spain'

Calculate the total count of order details from the cities

select COUNT(V_Orders.City) as totalcount,City from V_Orders group by V_Orders.City
views in SQL server examples
views in SQL server examples

Leave a Reply

Your email address will not be published. Required fields are marked *