SQL VIEW
What is view?
In SQL, a view is a virtual table that does not physically store data but retrieves data from one or more existing tables in the database.
- A view can either have specific rows based on certain condition or all the rows of a table.
- A view can be created using the CREATE VIEW statement. We can create a view from a single table or multiple tables.
1.creating view with single table :
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2…..
FROM table_name
WHERE condition;
Example:
2.creating view with multiple tables:
Syntax:
CREATE VIEW view_name AS
SELECT table1.NAME, table1.ADDRESS, table2.MARKS
FROM table1, table2
WHERE table1.common_col=table2.common_col;
Example :
note that we also can delete the view if not required by using following query:
DROP VIEW view_name;
The benefits of using views include:
- Simplifying complex queries: views can be used to complex queries and make them easier to read and use.
- Hiding sensitive information: views can be used to restrict access to sensitive information by selecting only the necessary columns and filtering out unwanted data.
- Reusing SELECT statements: views can be used to save frequently used SELECT statements as reusable objects.
- Improving performance: views can improve performance by SELECT statements or aggregating data.
Overall, views are a useful tool in SQL for organizing and simplifying complex queries, securing sensitive information, and improving performance.
Thank you!