If views are used to show selected columns to the user, and same can be done by using
SELECT col1, col2
FROM xyz
, what is the point of using views?
You can select data from multiple tables, or you can select specific data based on certain criteria in views. It does not hold the actual data; it holds only the definition of the view in the data dictionary. The view is a query stored in the data dictionary, on which the user can query just like they do on tables.
A view is still a query, it just abstracts certain parts of it so that your queries can be simplified (if they do similar things) and to maximize reuse.
there is no difference. A view is just a stored query which can be referred to in sql queries as though they are tables. Note that this does not apply to materialized views. A view is only a query stored in the data dictionary: it is not going to make your query run faster or slower.
View and Table both are integral parts of a relational database, and both terms are used interchangeably. The view is a result of an SQL query and it is a virtual table, whereas a Table is formed up of rows and columns that store the information of any object and be used to retrieve that data whenever required.
As Quassnoi said, it's useful for granting permission to certain rows in a table.
For example, let's say a lecturer at a university needs access to information on their students. The lecturer shouldn't have access to the "students" table because they could look up or modify information for any student in the whole university. The database admin makes a view that only shows students from the lecturers classes, and gives the lecturer the appropriate permissions for the view. Now the lecturer has access to their own students' data but not the whole "students" table.
10
queries in the different places of your code.All answers above provide an excellent explanation for the difference between a view and a query.
The query in the question is simple to an extreme degree, and creating a view for it might be overkill. However, most queries are more complex, for example:
;with Orders2016 as (
select Customers.CustomerID
, Customers.CompanyName
, TotalOrderAmount = sum(OD.Quantity * OD.UnitPrice)
from Customers
join Orders O on Customers.CustomerID = O.CustomerID
join OrderDetails OD on O.OrderID = OD.OrderID
where OrderDate >= '2016-01-01'
and OrderDate < '2017-01-01'
group by Customers.CustomerID, Customers.CompanyName
)
, CustomersGroups as
(
select CustomerID
, CompanyName
, TotalOrderAmount
, CustomerGroup =
(
case
when
TotalOrderAmount >= 0 and TotalOrderAmount < 1000
then 'low'
when TotalOrderAmount >= 1000 and TotalOrderAmount < 5000
then 'Medium'
when TotalOrderAmount >= 5000 and TotalOrderAmount < 10000
then 'High'
when TotalOrderAmount >= 10000 then 'VeryHigh'
end
)
from Orders2016
)
select CustomerGroup
, TotalInGroup = Count(*)
, PercentageInGroup = Count(*) * 1.0 / (select count(*) from CustomersGroups)
from CustomersGroups
group by CustomerGroup
order by TotalInGroup desc;
Imagine rewriting it each time you want to access data. (Or even searching through files, copy & paste). Poor time management.
Views also save us tons of time and are a way to be DRY.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With