Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between views and SELECT queries

Tags:

sql

view

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?

like image 929
hrishi Avatar asked Jul 10 '09 09:07

hrishi


People also ask

What is difference between view and select in SQL?

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.

Is query same as view?

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.

Which is faster view or select?

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.

What is the difference between view and table in SQL?

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.


3 Answers

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.

like image 82
Tom Dalling Avatar answered Oct 10 '22 20:10

Tom Dalling


  • Using a view saves you copying and pasting your queries and adds code reusability, so you can change a single view instead of 10 queries in the different places of your code.
  • Different permissions can be granted on views and tables, so that you can show only a portion of data to a user
  • A view can be materialized, which means caching the results of the underlying query
like image 35
Quassnoi Avatar answered Oct 10 '22 21:10

Quassnoi


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.

like image 26
Marcel Avatar answered Oct 10 '22 20:10

Marcel