A virtual table called a SQL view is produced as a result of a SELECT command. It doesn’t save any data on its own; instead, it presents data from one or more tables in a certain fashion.
Syntax for creating a view in SQL:
CREATE VIEW view_name AS
SELECT column1, column2, ...
For instance, you could use the following sentence to create a view called “employee_view” that displays all employees from the “employees” table with the last name “Smith”:
CREATE VIEW employee_view AS
WHERE last_name = 'Smith';
The same SELECT statement that you would use to query a table can also be used to query a view. For instance, you could use the following statement to get all rows from the “employee_view” view:
SELECT * FROM employee_view;
Views can be helpful for a variety of things, including:
- Query Simplification: Views can be used to divide down large queries into smaller, more manageable chunks, so simplifying them.
- Security: By limiting user access to the data that is displayed in the view, you can utilize views to limit access to certain data.
- Data integrity: Since views are built using SELECT statements, which are run each time a view is visited, you can make sure that the data displayed is always current by using views.