- Home
- Interview Questions
- SQL
- 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
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.
| Aspect | View | Materialized view |
|---|---|---|
| Storage | Only the definition | Definition plus result data |
| Freshness | Always current | Current as of the last refresh |
| Read cost | Same as the underlying query | Like reading a table |
| Indexes | Generally not on the view itself | Can be indexed (PostgreSQL, Oracle) |
| Write overhead | None | Refresh 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
REFRESHin PostgreSQL on a busy system. It takes an exclusive lock that blocks reads, so useCONCURRENTLYwith 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
Related Questions
- How does BCNF differ from 3NF, and why can BCNF decomposition lose a dependency? · SQL · 1 ans
- What are insertion, update and deletion anomalies, and how does normalization prevent them? · SQL · 1 ans
- How do you fetch the entire row with the highest salary in each department? · SQL · 1 ans
- What is the difference between COUNT(*), COUNT(column) and COUNT(DISTINCT column) in SQL? · SQL · 1 ans
- Why is using await inside a loop often slow, and how do you run async tasks in parallel? · JavaScript · 1 ans
- How does a recursive CTE execute step by step, and how do you stop it looping forever? · SQL · 1 ans
- Why can wrapping an indexed column in a function in WHERE make a query slow? · SQL · 1 ans
- Why does WHERE column = NULL return no rows, and how should you filter NULLs correctly? · SQL · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)