How to Answer: What is a view and how is it used?
Advice and answer examples written specifically for a Sql Server Developer job interview.
2. What is a view and how is it used?
This question focuses on SQL terminology and actions related to it.
A view is composed of a query on one or more tables and is saved in the database as a separate object.
Views can be used if you find yourself creating the same query repeatedly. It provides a way of abstracting complex logic so that the user of the view needs only be concerned with the output and not necessarily the complexities of the underlying query.
Views also provide a means of security by adding read-only permissions to views, disallowing access to the underlying tables, and exposing only the required fields.
/**This is an example of creating a view of customer orders
You have the opportunity of exposing only the required fields to an application, by restricting the application to only the view, since either table may contain
sensitive information that the application doesn't necessarily need access to**/
CREATE VIEW [dbo].[CustomerOrders]
AS
SELECT dbo.Customer.LastName, dbo.[Order].OrderAmount, dbo.[Order].OrderDate
FROM dbo.Customer INNER JOIN
dbo.[Order] ON dbo.Customer.CustomerID = dbo.[Order].CustomerID
GO
Written by Edward Danganan on April 13th, 2021