Concept · Advanced SQL

VIEW

A VIEW stores a named query. A normal view reads the rows from the base tables whenever you query it and does not keep a separate copy.

VIEW example

Creating a view
CREATE VIEW orders_in_progress AS
SELECT id, customer_id, date, total
FROM orders
WHERE status IN ('pending', 'shipped');

The part after AS is an ordinary query. The view keeps its definition, not the particular result that existed when it was created.

Using the view in FROM
SELECT id, customer_id, date, total
FROM orders_in_progress
ORDER BY id
LIMIT 5;

The outer query can add its own filter, sort or grouping. The view simplifies the starting point, but it does not force you to show all its rows every time.

How VIEW is used

The part after AS is an ordinary query. The view keeps its definition, not the particular result that existed when it was created.

Common mix-ups with VIEW

Mistaking a view for a frozen copy

A normal view is not a snapshot of the data. If an order changes from pending to cancelled, it will no longer appear in orders_in_progress the next time you query the view.

Materialised views, available on some engines, do store a result and need an explicit or scheduled refresh. They are a different kind of object.

Key differences between engines

PostgreSQL, MySQL, SQLite, SQL Server and Oracle all let you create views, but they differ in options such as replacing views, security, materialised views and updating data. SQLite treats its views as read-only for direct operations. An INSTEAD OF trigger can intercept an INSERT, UPDATE or DELETE on the view and change the base tables, but that mechanism has to be designed explicitly and does not make the view updatable on its own. Other engines allow changes through certain simple views or dedicated mechanisms. Always check your engine's rules before changing data through a view.

A view stores a query, not its results

A view is a named query stored in the database. Querying it runs its definition against the current data, so it always reflects the state of the source tables. It is useful for reusing a complex query and for exposing only certain columns.

Because it does not store the data, it does not save any computation: a heavy view costs as much as the query inside it. Some engines offer materialised views that do store the result and need refreshing; they are not the same thing, and support varies. Updatable views follow different rules depending on the engine.

Test the view before and after changing the source data and check that the result updates. Document its purpose, and avoid stacking views on top of views, because that makes it hard to see which query actually runs.

Self-contained case to check in SQLite

The view is defined over two orders; then we insert a third.

Data and query · SQLite
CREATE TABLE orders (id INTEGER, total INTEGER);
INSERT INTO orders VALUES (1, 30), (2, 50);
CREATE VIEW large_orders AS SELECT id FROM orders WHERE total >= 40;
INSERT INTO orders VALUES (3, 90);
SELECT id FROM large_orders ORDER BY id;
Verified result of the self-contained case
id
2
3

The view is evaluated when it is queried, which is why it includes order 3, inserted after the view was created.

Check your interpretation: What would happen with a materialised view that had not been refreshed?

Show answer

It would keep showing the result from its last refresh: it would not include order 3 until it is refreshed again.

Technical questions: SQLite: CREATE VIEW (opens in a new tab).