1. Home
  2. Interview Questions
  3. SQL
  4. What is the difference between a view and a materialized vie…

Try answering first

In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.

1 Answer

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best answer ·

Short answer: A view is a saved query that runs against the base tables every time you select from it, so it always shows current data but stores nothing. A materialized view runs the query once and stores the result physically, so reads are fast but the data is only as fresh as its last refresh.

Detailed explanation

A view is a stored SELECT statement. When you query it, the database usually merges the view's definition into your query and runs it against the underlying tables. The view holds no data of its own. It always reflects the latest committed data, and it costs as much to run as the query inside it.

A materialized view stores the result set on disk, like a table. Querying it reads the stored rows instead of re-running the joins and aggregations. The trade-off is staleness. When the base tables change, the stored result does not change until it is refreshed.

AspectViewMaterialized view
StorageOnly the definitionDefinition plus result data
FreshnessAlways currentCurrent as of the last refresh
Read costSame as the underlying queryLike reading a table
IndexesGenerally not on the view itselfCan be indexed (PostgreSQL, Oracle)
Write overheadNoneRefresh cost, plus disk space

Refresh strategies

  • Complete refresh: re-runs the whole query and replaces the data. This is simple, but it is expensive on large data.
  • Incremental (fast) refresh: applies only the changes since the last refresh. Oracle supports this using materialized view logs on the base tables, but only for queries that meet its restrictions.
  • On demand or scheduled: refresh runs manually or from a job, for example every night.
  • On commit: Oracle can refresh when a base-table transaction commits. This keeps data fresh but slows down writes.

Support varies by database. PostgreSQL has CREATE MATERIALIZED VIEW with manual REFRESH only. SQL Server uses indexed views, which the engine keeps in sync automatically on every write. MySQL has no native materialized views, so teams build summary tables and refresh them with scheduled jobs or triggers.

When to use which

  • Use a view to simplify complex joins and to hide columns for security, for example exposing employees without salary. It also gives applications a stable interface while the tables change underneath. Pick a view when results must be real-time.
  • Use a materialized view for expensive aggregations that are read often and tolerate some delay. Typical cases are dashboards, reports, daily sales summaries and leaderboards.

Example

-- Regular view: always live, re-runs the join each time
CREATE VIEW active_orders AS
SELECT o.order_id, c.name, o.amount
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
WHERE o.status = 'ACTIVE';

-- Materialized view (PostgreSQL): stores aggregated result
CREATE MATERIALIZED VIEW daily_sales AS
SELECT order_date, SUM(amount) AS total, COUNT(*) AS orders
FROM orders
GROUP BY order_date;

-- Index it like a table
CREATE UNIQUE INDEX ON daily_sales (order_date);

-- Refresh: plain refresh locks out readers while it runs
REFRESH MATERIALIZED VIEW daily_sales;

-- CONCURRENTLY lets readers continue; needs a unique index
REFRESH MATERIALIZED VIEW CONCURRENTLY daily_sales;

Suppose a dashboard shows sales per day, and orders has millions of rows. The aggregation could take seconds each time it runs. With daily_sales refreshed every 15 minutes, the dashboard reads a few hundred stored rows almost instantly.

Common mistakes

  • Assuming a regular view makes queries faster. A plain view usually runs the same query, so it adds convenience, not speed.
  • Forgetting that materialized views go stale and showing their data where users expect real-time numbers, such as account balances.
  • Running a plain REFRESH in PostgreSQL on a busy system. It takes an exclusive lock that blocks reads, so use CONCURRENTLY with a unique index.
  • Putting a materialized view on frequently changing data with on-commit refresh. Every write then pays the refresh cost.

Follow-up questions

  • Can you insert or update rows through a view, and what does WITH CHECK OPTION do?
  • How would you build a materialized-view equivalent in MySQL?
  • What is query rewrite in Oracle, and how does it use materialized views automatically?

Interview tip: Frame the answer as a trade-off between freshness and read speed. Then name one concrete refresh strategy and the database that supports it.

Reviewed by the Interview Questions editorial team · Last updated

How well did you know this?

Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.

Your Answer

More medium SQL questions

All medium questions →

Related Questions

‹
Q10 of 10 · SQL
All ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA