Sub topic 14
SQL Views: Limitations and Common Pitfalls
Assume you know SELECT, joins, grouping, table creation, and basic database privileges. This chapter focuses on the cases where a view behaves differently from what you expect.
A view is not a stored result
A regular view stores a query definition, not the rows returned by that query. When you query the view, the database usually expands its definition into your statement and optimizes the combined query.
CREATE VIEW active_employees AS
SELECT id, name, department_id
FROM employees
WHERE status = 'active';
SELECT name
FROM active_employees
WHERE department_id = 3;The database can often combine both filters. However, the exact execution plan depends on the database, indexes, statistics, and query shape. A view does not automatically improve performance or cache results.
A view with joins, grouping, sorting, or nested views can become difficult for the optimizer and for people reading the execution plan. Use EXPLAIN or your database's equivalent to inspect the actual plan.
Avoid hidden schema changes
Do not usually define a long-lived view with SELECT *.
CREATE VIEW employee_public AS
SELECT *
FROM employees;Adding a column to employees can change the view's output unexpectedly. It may also expose a new sensitive column or break code that expects a fixed column order. Name the columns explicitly and assign stable aliases.
Column names can also become ambiguous in joins. Always qualify columns and alias duplicate names:
CREATE VIEW employee_directory AS
SELECT e.id AS employee_id,
e.name AS employee_name,
d.name AS department_name
FROM employees AS e
JOIN departments AS d ON d.id = e.department_id;Remember that ordering is not preserved
An ORDER BY inside a view does not guarantee the order of a later query. The outer query controls the final order.
SELECT employee_name, department_name
FROM employee_directory
ORDER BY department_name, employee_name;If callers need a specific order, document it or include the ORDER BY in each final query.
Writing through views has restrictions
A simple view over one table may support INSERT, UPDATE, or DELETE. A view containing a join, DISTINCT, grouping, aggregate functions, set operators, or window functions is commonly read-only, although rules differ between database systems.
Do not assume that a successful SELECT means writes will work. Test the intended operation and check your database documentation.
WITH CHECK OPTION prevents a write through a filtered view from creating a row that the view would hide:
CREATE VIEW active_employees AS
SELECT id, name, status
FROM employees
WHERE status = 'active'
WITH CHECK OPTION;Without this option, an update through the view might change status to inactive. The update could succeed, but the row would immediately disappear from the view.
Security is not automatic
A view can expose fewer columns and rows, but granting access to a view does not always guarantee complete isolation. Check the database's rules for view ownership, invoker or definer privileges, row-level security, and indirect access through other objects.
Grant only the required privilege, and test with the actual user or role:
REVOKE ALL ON employees FROM analyst;
GRANT SELECT ON employee_public TO analyst;Also review nested views. A seemingly safe view can depend on another view that exposes more data than intended.
Dependencies and portability
Renaming or dropping a base-table column can invalidate a view. Dropping a view can also break reports, stored procedures, or other views that depend on it. Before changing a view, search the system catalog and test dependent applications.
SQL syntax is not completely portable. CREATE OR REPLACE VIEW, materialized-view refresh commands, security options, and dependency behavior differ across PostgreSQL, MySQL, SQL Server, Oracle, and other systems. Treat database-specific syntax as a deliberate choice.
> callout warning > A regular view solves organization and access-control problems. It does not solve stale data, expensive computation, or every security problem. Use a materialized view for stored results, indexes for access paths, and explicit privileges for authorization.
Exercise
A view uses SELECT *, contains an ORDER BY, and filters active employees. Name two problems that callers might encounter and one change that would make the view safer.
Show answer
New base-table columns can unexpectedly change the view's output, and the inner ORDER BY does not guarantee the caller's result order. Naming the columns explicitly and applying ORDER BY in the final query makes the interface safer.