SQL Views
A SQL view is a stored query given a name so it can be queried like a virtual table without rewriting its logic. A regular view does not store its own copy of the data and runs its underlying query each time; views are used to reuse complex queries and to expose only specific columns or rows.
A view is a stored SQL query given a name, so it can be queried again just like a real table, without re-writing the underlying logic every time. A view doesn't store its own copy of the data (unless it's a materialized view, a distinct concept), it just runs its underlying query fresh each time it's referenced.
Why views are useful
- Reusability: a complex query with several joins and calculations can be saved once as a view, then queried simply, instead of repeating the full logic everywhere it's needed.
- Security and abstraction: a view can expose only specific columns or rows from an underlying table, hiding sensitive columns (like a salary field) from users who only have access to the view.
- Simplifying complexity: application code or analysts can query a clean, simple view instead of understanding a complicated multi-table join every time.
A worked example
Given employees (with a sensitive salary column) and departments tables, a view can expose just what's needed for a general directory lookup:
CREATE VIEW employee_directory AS
SELECT employees.name, departments.department_name, employees.email
FROM employees
JOIN departments ON employees.department_id = departments.department_id;
Now anyone with access to employee_directory can run:
SELECT * FROM employee_directory WHERE department_name = 'Engineering';
They never see the underlying salary column at all, since the view was defined without it, and they don't need to know the join even exists underneath.
A note on materialized views
A regular view re-runs its query every time it's accessed, always reflecting live data but potentially slower for complex queries. A materialized view (supported in PostgreSQL and other databases) actually stores the query's result physically, like a cached snapshot, which is much faster to read but needs to be manually or periodically refreshed to stay up to date with the underlying tables.
Where this matters in real systems
Views are commonly used to give a reporting or business-intelligence tool a stable, simplified interface into a database whose actual schema is more complex or changes over time, the view's own definition can be updated to match schema changes while every downstream report that queries the view keeps working unchanged. Materialized views are used heavily in analytics systems where a complex aggregation query would be too slow to re-run on every single dashboard load.
What interviewers actually ask about SQL views
- "What is a view?" A saved, named SQL query that can be queried like a table, without storing its own separate copy of the data.
- "Does a view store data?" No, a regular view re-runs its underlying query every time it's accessed; a materialized view is the distinct case that does store a physical snapshot.
- "Why would you use a view instead of just running the full query every time?" Reusability (write the complex logic once), and security (expose only specific columns/rows without giving access to the full underlying table).
- "What's the tradeoff of a materialized view versus a regular view?" A materialized view is much faster to read since it's precomputed, but it can go stale and needs to be refreshed to reflect the latest underlying data.
Key takeaways
- A view is a saved SQL query that behaves like a virtual table when queried.
- Regular views re-run their query live each time; materialized views store a physical snapshot for speed, at the cost of needing refreshes.
- Views are useful for reusing complex query logic and for restricting which columns or rows are exposed.
- They let a stable, simplified interface sit in front of a more complex or evolving underlying schema.
More in SQL
Browse all DBMS topics
Log in to save which topics you've read across every subject.
Frequently asked questions
What is a view?
A saved, named SQL query that can be queried like a table, without storing its own separate copy of the data.
Does a view store data?
No, a regular view re-runs its underlying query every time it's accessed; a materialized view is the distinct case that does store a physical snapshot.
Why would you use a view instead of just running the full query every time?
Reusability (write the complex logic once), and security (expose only specific columns/rows without giving access to the full underlying table).
What's the tradeoff of a materialized view versus a regular view?
A materialized view is much faster to read since it's precomputed, but it can go stale and needs to be refreshed to reflect the latest underlying data.