Skip to main content

Database View

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/database-view

In short

A database view is a saved SQL query that behaves like a virtual table, so you can select from it by name instead of repeating the underlying query each time.

What is a database view?

A database view is a named query stored in the database. You create it once with CREATE VIEW, and after that you can query it just like a table. A regular view does not store any data of its own; each time you query it, the database runs the underlying SELECT against the real tables.

When you query a view, the database merges the view's definition into your query and optimizes the combined statement, so the result always reflects the current data. Views are used to hide complicated joins behind a simple name, to expose only some columns or rows to certain users by granting access to the view but not to the base tables, and to keep a stable interface while the underlying tables change. Simple views over a single table can often be updated directly, while complex ones are read-only.

A view is like a saved playlist: it doesn't copy the songs, it only remembers which ones to show and in what order. A materialized view is different: it runs the query once and stores the result as a real table, so reads are fast but the data can be stale until the view is refreshed. Materialized views are common for dashboards and reports that aggregate large tables.

The common confusion is between a view and a table, or between a view and a stored procedure. A table holds data; a regular view holds only a query. A stored procedure is a program you call, which can take parameters and change data, while a view is a result set you read from inside a SELECT. On its own, a regular view does not make queries faster, because the database still does the same work underneath.

Key takeaways

  • A view is a named, saved query that you can select from like a table.
  • A regular view stores no data and always shows current results.
  • Views simplify complex joins and can limit which columns users see.
  • A materialized view stores its result and must be refreshed to stay current.

Example

Creating a view and a materialized view (PostgreSQL syntax)sql
-- A view hides a join behind a simple name
CREATE VIEW active_customers AS
SELECT c.id, c.name, c.country, MAX(o.created_at) AS last_order
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.country;

SELECT * FROM active_customers WHERE country = 'DE';

-- A materialized view stores the result; refresh it to update
CREATE MATERIALIZED VIEW sales_by_month AS
SELECT date_trunc('month', created_at) AS month, SUM(total) AS revenue
FROM orders GROUP BY 1;
REFRESH MATERIALIZED VIEW sales_by_month;

Readers ask

Does a database view store data?

A regular view does not; it stores only the query and runs it each time you read from the view. A materialized view does store its result, which is why it needs to be refreshed.

What is the difference between a view and a materialized view?

A view is recomputed on every query, so it is always current but can be slow for heavy queries. A materialized view is precomputed and fast to read, but it shows the data as of its last refresh.

Can you insert or update data through a view?

Often yes for simple views that select from a single table without aggregates or DISTINCT. Views with joins or grouping are usually read-only unless the database offers special rules or INSTEAD OF triggers.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings