đī¸ The CREATE VIEW statement is used to create a view, which is a virtual table based on the result of a SELECT query. Views simplify complex queries, improve security, and provide a reusable way to access data from one or more tables.
đ What is CREATE VIEW?
The CREATE VIEW statement saves a SQL query as a named database object. Instead of repeatedly writing the same query, you can query the view just like a regular table.
Information
đ¯ Why Use CREATE VIEW?
Views make SQL queries easier to write, maintain, and secure.
- đ Simplify complex SQL queries.
- đ Reuse frequently executed queries.
- đ Hide sensitive columns.
- đ Restrict access to selected data.
- đ Improve application maintainability.
đ Sample Table
| StudentID | Name | Department | |
|---|---|---|---|
| 101 | Alice | Computer Science | alice@example.com |
| 102 | Bob | Mathematics | bob@example.com |
| 103 | Charlie | Physics | charlie@example.com |
đ 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, the view can be queried exactly like a table.
Select from View
SELECT *
FROM StudentInfo;The database executes the stored query and returns the latest data from the underlying table.
đ Create a View with a WHERE Clause
Views can display only records that satisfy a specific condition.
Filtered View
CREATE VIEW ComputerScienceStudents AS
SELECT
StudentID,
Name,
Department
FROM Students
WHERE Department = 'Computer Science';đ¤ Create a View Using JOIN
A view can combine data from multiple related tables.
View with INNER JOIN
CREATE VIEW StudentCourses AS
SELECT
s.StudentID,
s.Name,
c.CourseName
FROM Students s
INNER JOIN Enrollments c
ON s.StudentID = c.StudentID;This view provides student and course information without requiring users to write the join each time.
đ Result of StudentCourses View
| StudentID | Name | CourseName |
|---|---|---|
| 101 | Alice | Database Systems |
| 102 | Bob | Data Structures |
đ Replace or Modify a View
Some database systems allow an existing view definition to be replaced.
Replace a View
CREATE OR REPLACE VIEW StudentInfo AS
SELECT
Name,
Department
FROM Students
WHERE Department = 'Physics';Important
đī¸ Drop a View
Remove a view when it is no longer needed.
DROP VIEW
DROP VIEW StudentInfo;Warning
âī¸ CREATE VIEW vs CREATE TABLE
| Feature | CREATE VIEW | CREATE TABLE |
|---|---|---|
| Stores Data | Usually No | â Yes |
| Based On | A SELECT query. | Column definitions. |
| Can Combine Multiple Tables | â Yes | â No |
| Main Purpose | Simplify and secure data access. | Store data. |
đŧ Real-World Example
An HR department should view employee names and departments but not salary information. A view exposes only the required columns while keeping sensitive data protected.
Employee Directory View
CREATE VIEW EmployeeDirectory AS
SELECT
EmployeeID,
FullName,
Department
FROM Employees;đī¸ Database Compatibility
| Database System | CREATE VIEW Support |
|---|---|
| MySQL | â Fully supported. |
| PostgreSQL | â Fully supported. |
| SQL Server | â Fully supported. |
| Oracle | â Fully supported. |
| SQLite | â Supported. |
â ī¸ Common Mistakes
- â Assuming a view stores its own copy of the data.
- â Creating unnecessarily complex nested views.
- â Expecting every view to support INSERT, UPDATE, or DELETE.
- â Forgetting that changes to underlying tables affect view results.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ CREATE VIEW creates a virtual table based on a SELECT query.
- đ Views simplify complex SQL statements.
- đ They improve security by hiding sensitive columns.
- đ Views always reflect the current data in the underlying tables unless using database-specific materialized views.
- đ Views can combine data from multiple tables using joins.
- đ View support is available in all major relational database systems.