- Views in SQL are kind of virtual tables.
- A view also has rows and columns as they are in a real table in the database.
- We can create a view by selecting fields from one or more tables present in the database.
- A View can either have all the rows of a table or specific rows based on certain condition.
- Simplify the complex SQL
queries.
- Provide restriction to users
from accessing sensitive data.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2 FROM table_name WHERE condition;
CREATE VIEW emp_view AS
SELECT emp_id, emp_name, salary
FROM employees
WHERE salary > 50000;
This creates a view named
emp_view
that only shows employees with salary > 50,000.==========================================================================
Benefit | Explanation |
---|
✅ Simplify complex queries | Write once, use many times |
✅ Enhance security | Hide sensitive columns (e.g., passwords) |
✅ Logical abstraction | Treat query results as a table |
✅ Data consistency | Centralize business logic |
==========================================================================
Limitations of Views
-
Views do not store data — underlying tables must exist.
-
Some views are not updatable (e.g., views using
GROUP BY
,JOIN
, orDISTINCT
). -
Performance may be impacted if nested queries are complex.
==========================================================================
Types of Views
·
Simple View: A view based on only a single table, which doesn't contain
GROUP BY clause and any functions.
·
Complex View: A view based on multiple tables, which contain GROUP BY clause
and functions.
·
Inline View: A view based on a subquery in FROM Clause, that subquery
creates a temporary table and simplifies the complex query.
·
Materialized View: A view that stores the definition as well as data. It creates
replicas of data by storing it physically.