Sub topic 9
Building Views on Top of Views
This chapter assumes you can write SELECT queries with joins and filters, create views, and understand basic database privileges. You will learn how one view can use another view as its data source.
What a nested view is
A nested view is a view whose FROM clause references another view. The inner view defines a reusable data layer. The outer view adds another filter, calculation, or presentation rule.
Suppose the database has employees and departments tables:
CREATE VIEW employee_directory AS
SELECT e.id,
e.name,
e.email,
e.status,
d.name AS department
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id;This view hides the join. A second view can now use the directory without repeating that join:
CREATE VIEW active_employee_directory AS
SELECT id,
name,
email,
department
FROM employee_directory
WHERE status = 'active';
SELECT name, department
FROM active_employee_directory
WHERE department = 'Engineering';The query against active_employee_directory reads as if it were querying a table. Conceptually, the database expands the view definitions until it reaches the base tables:
SELECT name, department
FROM employee_directory
WHERE status = 'active'
AND department = 'Engineering';It then expands employee_directory into its join. The exact optimization process depends on the database system.
Why use layers?
Layers let each view have one clear purpose.
employee_directorydefines the common employee-to-department relationship.active_employee_directorydefines which employees are considered active.- A reporting view could add department totals or other business rules.
This reduces repeated SQL and gives applications stable names to query. If the join changes, you can often update the inner view instead of changing every application query.
A useful design is to expose a narrow outer view:
CREATE VIEW engineering_staff AS
SELECT id,
name
FROM active_employee_directory
WHERE department = 'Engineering';Callers do not need to know about the join, the status column, or the email address.
| Layer | Main responsibility | Example |
|---|---|---|
| Base tables | Store data | `employees`, `departments` |
| Inner view | Reuse a relationship | `employee_directory` |
| Outer view | Apply a business rule | `active_employee_directory` |
Dependencies and maintenance
A view dependency exists when one database object refers to another. In this example, engineering_staff depends on active_employee_directory, which depends on employee_directory.
That dependency affects changes:
- Dropping the inner view can fail because outer views depend on it.
- Changing output column names can break outer views.
- Adding columns usually does not make existing
SELECTstatements return those columns automatically. SELECT *can make view changes harder to predict, so explicit columns are safer.
Deep chains can also make SQL harder to debug. Keep the number of layers small, name each layer clearly, and inspect the final query plan when performance matters. A regular view usually stores only its definition, not its rows. The optimizer may merge its definition into the surrounding query, but complex expressions, aggregation, security rules, or database-specific behavior can prevent that.
When to flatten a view
Use nested views when the layers represent stable concepts and improve reuse. Consider one direct query instead when:
- The chain is difficult to understand.
- Several views repeat the same expensive work.
- A change requires coordinated edits across many dependent views.
- The database produces a poor execution plan.
Nested views improve organization, but they do not automatically improve performance.
Exercise
If active_employee_directory is built on employee_directory, where should you change the employee-to-department JOIN when the relationship changes?
Show answer
Change employee_directory, then test every dependent view. The outer view should continue to use the inner view's documented columns.