SQL Views Interview Questions
Master SQL Views with interview-focused questions covering Views, Simple Views, Complex Views, Materialized Views, Updatable Views, Security, Performance, View vs Table, View vs Materialized View, and enterprise production best practices.
Introduction
In enterprise applications, developers rarely allow users or applications to access database tables directly.
Instead, they expose
- Views
- Materialized Views
- Reporting Views
- Security Views
Views provide
- Simplicity
- Security
- Reusability
- Abstraction
- Consistent Business Logic
Views are heavily used in
- Banking
- Healthcare
- Insurance
- ERP
- CRM
- Data Warehousing
They are among the most frequently asked SQL interview topics.
View Architecture
flowchart LR
Application --> View
View --> BaseTables["Base Tables"]
BaseTables["Base Tables"] --> Database
Sample Tables
Employee
| ID | Name | Department | Salary |
|---|---|---|---|
| 101 | John | IT | 90000 |
| 102 | Alice | HR | 80000 |
| 103 | David | IT | 95000 |
| 104 | Bob | Finance | 70000 |
Department
| Dept_ID | Department |
|---|---|
| 1 | IT |
| 2 | HR |
| 3 | Finance |
1. What is a View?
Answer
A View is a virtual table created using a SQL query.
Unlike a physical table,
a View normally does not store data itself.
It retrieves data from one or more underlying tables whenever queried.
View Workflow
Application
↓
View
↓
SELECT Query
↓
Base Tables
↓
Result
2. Why are Views used?
Views provide
- Security
- Simplicity
- Code Reusability
- Query Abstraction
- Business Logic Reuse
3. How do you create a View?
CREATE VIEW EmployeeView AS
SELECT
id,
name,
department,
salary
FROM Employee;
4. How do you query a View?
Exactly like a table.
SELECT *
FROM EmployeeView;
5. Does a View store data?
A standard SQL View
does not store data.
Only the SQL definition is stored.
Whenever queried,
the database executes the underlying SQL.
View Execution
flowchart LR
Select["SELECT *"] --> View
View --> UnderlyingQuery["Underlying Query"]
UnderlyingQuery["Underlying Query"] --> Tables
6. What is a Simple View?
A Simple View
is created from
one table
without
- GROUP BY
- Aggregate Functions
- DISTINCT
- Complex JOINs
Example
CREATE VIEW ActiveEmployees AS
SELECT *
FROM Employee
WHERE status='ACTIVE';
7. What is a Complex View?
A Complex View
contains
- JOIN
- GROUP BY
- Aggregate Functions
- DISTINCT
- UNION
Example
CREATE VIEW DepartmentSalary AS
SELECT
department,
AVG(salary)
FROM Employee
GROUP BY department;
8. What is an Updatable View?
An Updatable View allows
INSERT
UPDATE
DELETE
operations.
Generally,
simple views
are updatable.
9. When is a View NOT updatable?
Typically,
Views become non-updatable when they contain
- GROUP BY
- DISTINCT
- Aggregate Functions
- UNION
- Many complex JOINs
Support varies by database vendor.
10. What is a Materialized View?
A Materialized View
physically stores
query results.
Unlike a normal View,
it contains actual data.
Materialized View
flowchart LR
Query --> MaterializedView["Materialized View"]
MaterializedView["Materialized View"] --> StoredData["Stored Data"]
11. Why use Materialized Views?
Benefits
- Faster Reporting
- Faster Analytics
- Reduced Query Time
- Less CPU Usage
12. Difference between View and Materialized View?
| View | Materialized View |
|---|---|
| Virtual | Physical |
| No Data Stored | Stores Data |
| Always Current | Requires Refresh |
| Slower for Large Reports | Very Fast Reads |
13. What is View Refresh?
Materialized Views require
refresh
to synchronize
with
base tables.
Refresh can be
- Complete
- Incremental (database dependent)
- Scheduled
- Manual
14. What is View Security?
Views hide
sensitive columns.
Example
Instead of exposing
Salary
SSN
DOB
only expose
Employee Name
Department
Security View
flowchart LR
Application --> View
View --> SafeColumns["Safe Columns"]
SafeColumns["Safe Columns"] --> EmployeeTable["Employee Table"]
15. Can Views join multiple tables?
Yes.
Example
CREATE VIEW EmployeeDepartment AS
SELECT
e.name,
d.department
FROM Employee e
JOIN Department d
ON e.department_id=d.dept_id;
16. Can Views contain aggregate functions?
Yes.
Example
CREATE VIEW DepartmentSalary AS
SELECT
department,
AVG(salary)
AS avg_salary
FROM Employee
GROUP BY department;
17. Can Views call other Views?
Yes.
One View
may reference
another View.
However,
deeply nested Views
may impact readability and performance.
18. Can indexes be created on Views?
It depends on the database.
Examples
- SQL Server supports Indexed Views (with restrictions)
- Oracle supports Materialized Views
- PostgreSQL does not support indexed standard views (indexes can be created on materialized views)
19. Banking Example
Customer View
Shows
- Name
- Account Number
Hides
- Password
- PIN
20. HR Example
Employee Directory
Shows
- Name
- Department
Hides
- Salary
- SSN
21. Healthcare Example
Doctor Dashboard
Shows
Appointments
without exposing
sensitive billing information.
22. E-Commerce Example
Product View
Combines
Products
Categories
Inventory
into one logical view.
23. Analytics Example
Sales Report View
Aggregates
Monthly Revenue
for dashboards.
24. Production Example
Executive Dashboard
Uses
Materialized Views
to generate reports
within seconds
instead of minutes.
25. Common Advantages
- Simplifies SQL
- Improves Security
- Reuses Business Logic
- Hides Complexity
- Provides Consistent Queries
26. Common Limitations
- Complex Views may be slower
- Nested Views increase complexity
- Materialized Views require refresh
- Updatable Views have restrictions
27. View vs Table
| Table | View |
|---|---|
| Stores Data | Virtual Query |
| Consumes Storage | Minimal Storage |
| Insert Data | Mostly Read Logic |
| Physical Object | Logical Object |
28. Common Mistakes
- Building deeply nested Views
- Assuming Views improve performance
- Forgetting Materialized View refresh
- Exposing sensitive columns
- Writing overly complex Views
29. Best Practices
- Keep Views simple.
- Hide sensitive columns.
- Use meaningful names.
- Avoid excessive nesting.
- Use Materialized Views for heavy reporting.
- Refresh Materialized Views regularly.
- Grant access to Views instead of base tables when appropriate.
- Monitor query performance.
- Document View purpose.
- Test View execution plans.
30. View Workflow
flowchart LR
Application --> View
View --> SqlQuery["SQL Query"]
SqlQuery["SQL Query"] --> Database
Database --> Result
Enterprise Best Practices
- Use Views to enforce data security.
- Expose only required columns.
- Use Views to encapsulate complex business logic.
- Prefer Materialized Views for expensive analytical queries.
- Refresh Materialized Views on an appropriate schedule.
- Avoid excessive View nesting.
- Monitor execution plans.
- Restrict direct access to base tables.
- Version complex reporting Views.
- Periodically review View definitions for performance.
Quick Revision
| Topic | Key Point |
|---|---|
| View | Virtual Table |
| Simple View | Single Table |
| Complex View | JOIN/Aggregation |
| Updatable View | Allows DML (with restrictions) |
| Materialized View | Stores Data |
| Refresh | Synchronize Materialized View |
| View Security | Hide Sensitive Columns |
| View vs Table | Virtual vs Physical |
| View vs Materialized View | Live vs Stored |
| Reporting | Common Use Case |
Interview Tips
Interviewers frequently ask
- What is a View?
- Why are Views used?
- View vs Table.
- View vs Materialized View.
- Simple View vs Complex View.
- What is an Updatable View?
- Why use Materialized Views?
- Can Views improve performance?
- Can Views join multiple tables?
- How do Views improve security?
A strong interview explanation is:
"A View is a virtual table defined by a SQL query that simplifies data access, hides implementation details, and improves security by exposing only required columns. Standard Views execute the underlying query each time they are accessed, whereas Materialized Views physically store query results and require periodic refreshes. Views are widely used to encapsulate business logic, simplify reporting, and restrict direct access to underlying tables."
Summary
SQL Views provide an abstraction layer over database tables, making queries simpler, improving security, and promoting reusable business logic. Standard Views dynamically retrieve data from base tables, while Materialized Views store precomputed results to improve reporting performance. Understanding Simple Views, Complex Views, Updatable Views, Materialized Views, View Security, and performance considerations is essential for enterprise SQL development.
Mastering SQL Views prepares you for advanced topics such as Stored Procedures, Triggers, Query Optimization, Data Warehousing, and Enterprise Reporting Solutions.