What is a View in SQL?
A View in SQL is a virtual table created using the result of a SQL query.
In simple words:
A view stores a SQL query and shows data like a table without storing actual data separately.
Why Views are Important
In real-world applications:
- Complex queries are used frequently
- Multiple tables are joined repeatedly
- Security restrictions are needed
Views help:
- Simplify queries
- Improve security
- Provide reusable logic
- Hide complex database structures
Simple Real-Life Example
Think of a view like:
- A saved filter in Excel
Instead of writing filter logic repeatedly:
- You save it once
Same concept applies in SQL views.
What is a Virtual Table?
A virtual table means:
- View behaves like a table
- But actual data remains in original tables
Important Point
View does:
NOT store actual data separately.
How View Works Internally
User Query
|
v
View Executes Stored SQL Query
|
v
Fetch Data from Actual Tables
|
v
Return Result
Real-Time Example
Students Table
| Student ID | Name | Course | Fee |
|---|---|---|---|
| 1 | Naresh | MySQL | 10000 |
| 2 | Rahul | Spring Boot | 15000 |
| 3 | Arjun | Microservices | 20000 |
Suppose Requirement
Management team should see:
- Student Name
- Course
But:
- Fee column should be hidden
Solution
Create a:
View
View Syntax
CREATE VIEW view_name AS SELECT columns FROM table_name;
Example
CREATE VIEW student_view AS
SELECT name,
course
FROM students;
How to Use View
SELECT * FROM student_view;
Result
| Name | Course |
|---|---|
| Naresh | MySQL |
| Rahul | Spring Boot |
| Arjun | Microservices |
Important Observation
Fee column is hidden.
Why Views are Useful
Views help:
- Hide sensitive data
- Simplify complex queries
- Improve code reusability
View Internal Architecture
Actual Tables
|
v
Stored SQL Query (View)
|
v
Virtual Table Result
View Query Flow
SELECT * FROM view_name
|
v
Execute Underlying SQL Query
|
v
Fetch Data from Tables
|
v
Return Result
Types of Views in SQL
- Simple View
- Complex View
- Materialized View
1. Simple View
Created from:
- Single table
Example
CREATE VIEW employee_view AS
SELECT employee_id,
employee_name
FROM employees;
2. Complex View
Created using:
- Multiple tables
- JOINs
- Functions
- GROUP BY
Example
CREATE VIEW order_summary AS
SELECT c.customer_name,
o.order_id,
p.product_name
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id
INNER JOIN products p
ON o.product_id = p.product_id;
3. Materialized View
Materialized view:
- Physically stores data
Unlike normal views:
- Data is stored separately
Why Materialized Views are Used
- Faster reporting
- Heavy analytics
- Large aggregations
Advantages of Views
1. Simplifies Complex Queries
Instead of writing large JOIN queries repeatedly:
- Use view directly
Example
SELECT * FROM sales_report_view;
2. Improves Security
Views can hide:
- Password columns
- Salary details
- Confidential information
3. Code Reusability
Same query logic reused across:
- Applications
- Reports
- Dashboards
4. Better Abstraction
Applications interact with:
- Simplified data structure
Disadvantages of Views
- Complex views may become slow
- Nested views reduce performance
- Some views are not updatable
What is Updatable View?
Some views allow:
- INSERT
- UPDATE
- DELETE
Example
UPDATE student_view SET course = 'Java' WHERE name = 'Naresh';
When Views Become Non-Updatable
Views using:
- GROUP BY
- DISTINCT
- Aggregate Functions
- Complex JOINs
may not support updates.
Real-Time Banking Example
Banking systems use views for:
- Customer dashboards
- Account summaries
- Transaction reports
Example
CREATE VIEW account_summary AS
SELECT customer_name,
account_number,
balance
FROM accounts;
Real-Time E-Commerce Example
E-commerce platforms use views for:
- Order reports
- Product analytics
- Sales dashboards
Example
CREATE VIEW sales_view AS
SELECT product_name,
total_sales
FROM product_sales;
Real-Time Learning Platform Example
Learning platforms use views for:
- Student progress reports
- Course analytics
- Instructor dashboards
Example
CREATE VIEW course_progress AS
SELECT student_name,
course_name,
completion_percentage
FROM progress_tracking;
Views in Microservices
Microservices use views for:
- Reporting APIs
- Analytics dashboards
- Read-only projections
Example
GET /api/reports/course-summary
may internally use:
Database view
View vs Table
| Feature | View | Table |
|---|---|---|
| Stores Data | No | Yes |
| Physical Storage | No | Yes |
| Acts Like Table | Yes | Yes |
| Query Based | Yes | No |
View vs Materialized View
| Feature | View | Materialized View |
|---|---|---|
| Stores Data | No | Yes |
| Performance | Moderate | Faster |
| Refresh Needed | No | Yes |
How to Delete View
DROP VIEW view_name;
Example
DROP VIEW student_view;
Performance Consideration
Views themselves do not improve performance.
Performance depends on:
- Underlying query
- Indexes
- JOIN complexity
Best Practices
- Use views for reusable queries
- Use views for security abstraction
- Avoid deeply nested views
- Optimize underlying queries
Common Interview Mistake
Many developers think:
- Views store data physically
Reality
Normal views:
- Store only SQL query
Related Learning Topics
- What is an Index in SQL?
- Different Types of JOINs in SQL
- What is Normalization in SQL?
- What is a Table in SQL?
Professional Interview Answer
A View in SQL is a virtual table created using the result of a SQL query. Views do not store data physically; instead, they store the query definition and fetch data dynamically from underlying tables whenever accessed. Views are commonly used to simplify complex queries, improve security by hiding sensitive columns, provide reusable query logic, and create abstraction layers in applications. There are different types of views such as simple views, complex views, and materialized views. Views are widely used in enterprise applications, reporting systems, analytics dashboards, banking platforms, e-commerce systems, and microservices architectures.
Why Interviewers Like This Answer
- Clearly explains virtual table concept
- Includes security and abstraction concepts
- Shows enterprise usage understanding
- Provides real-world examples
- Explains view internals and performance
Frequently Asked Questions
What is a view in SQL?
A view is a virtual table created using a SQL query.
Does a view store data physically?
No, normal views do not store data physically.
Why views are used?
Views simplify queries, improve security, and provide reusable logic.
Can views be updated?
Some simple views are updatable, but complex views may not support updates.
What is difference between view and table?
A table stores actual data, while a view stores only query definition.