đī¸ A View in SQL is a virtual table created from the result of a SELECT query. A view does not usually store the actual data itself; instead, it presents data from one or more underlying tables as if it were a regular table. Views simplify complex queries, improve security, and provide a consistent way to access data.
đ What is a View?
A view is a saved SQL query that can be queried just like a table. Whenever you retrieve data from a view, the database executes the underlying query and returns the result.
Information
đ¯ Why Use Views?
Views make database applications easier to build, maintain, and secure.
- đ Simplify complex SQL queries.
- đ Hide unnecessary columns.
- đ Improve data security.
- đ Provide a consistent interface for applications.
- đ Reuse frequently executed queries.
- đ Abstract changes in the underlying database schema.
đ Sample Tables
Students
| StudentID | Name | Department |
|---|---|---|
| 101 | Alice | Computer Science |
| 102 | Bob | Mathematics |
Enrollments
| StudentID | Course |
|---|---|
| 101 | Database Systems |
| 102 | Data Structures |
đ Basic Syntax
CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;đĄ Create a Simple View
Create a view that displays only student names and departments.
Simple View
CREATE VIEW StudentInfo AS
SELECT Name, Department
FROM Students;đ Query the View
Once created, a view can be queried just like a table.
Select from a View
SELECT *
FROM StudentInfo;The database executes the underlying query and returns the requested data.
đ Create a View Using JOIN
Views can combine data from multiple tables, making complex joins easier to reuse.
View with INNER JOIN
CREATE VIEW StudentCourses AS
SELECT
s.StudentID,
s.Name,
e.Course
FROM Students s
INNER JOIN Enrollments e
ON s.StudentID = e.StudentID;Applications can now retrieve student and course information without writing the join repeatedly.
đ Update a View
Many database systems support modifying an existing view definition.
Replace or Alter a View
CREATE OR REPLACE VIEW StudentInfo AS
SELECT
Name,
Department
FROM Students
WHERE Department = 'Computer Science';Important
đī¸ Drop a View
Remove a view when it is no longer needed.
Drop a View
DROP VIEW StudentInfo;Warning
đ Virtual View vs Materialized View
| Feature | Virtual View | Materialized View |
|---|---|---|
| Stores Data | â Usually No | â Yes |
| Uses Latest Table Data | â Always | After Refresh |
| Query Speed | Depends on the underlying query. | Usually Faster for complex queries. |
| Storage Requirement | Minimal | Additional Storage Required |
âī¸ View vs Table
| Feature | View | Table |
|---|---|---|
| Stores Data | Usually No | â Yes |
| Created From | SQL Query | Database Structure |
| Can Join Multiple Tables | â Yes | â No |
| Purpose | Simplify and secure data access. | Store actual records. |
đŧ Real-World Example
A company's HR department needs employee names and departments but should not see salary information. A view can expose only the required columns while hiding sensitive data stored in the underlying table.
Employee View
CREATE VIEW EmployeeDirectory AS
SELECT
EmployeeID,
FullName,
Department
FROM Employees;â ī¸ Updatable Views
Some views allow INSERT, UPDATE, and DELETE operations, while others do not. Whether a view is updatable depends on its definition and the database system.
- â Simple views based on a single table are often updatable.
- â ī¸ Views containing joins, aggregate functions, GROUP BY, DISTINCT, or set operations are often read-only.
đī¸ Database Compatibility
| Database System | View Support |
|---|---|
| MySQL | Supports views and many updatable views. |
| PostgreSQL | Supports views and materialized views. |
| SQL Server | Supports views and indexed views under specific conditions. |
| Oracle | Supports views and materialized views. |
| SQLite | Supports views with limited update capabilities. |
â ī¸ Common Mistakes
- â Assuming every view is automatically updatable.
- â Creating unnecessarily complex nested views.
- â Expecting a standard view to store data permanently.
- â Forgetting that changes to underlying tables affect view results.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ A view is a virtual table created from a SELECT query.
- đ Views simplify complex queries and improve code reuse.
- đ They help protect sensitive data by exposing only selected columns.
- đ Standard views usually do not store data physically.
- đ Updatability depends on the view definition and the database system.
- đ Views are widely used to improve maintainability, readability, and security.