What is a View in SQL?
Learn what a SQL view is — a virtual table from a stored query — plus materialized views, security uses and updatability, with examples and interview questions.
Expected Interview Answer
A view is a virtual table defined by a stored SQL query; it holds no data of its own but presents the result of that query as if it were a table whenever you select from it.
Views simplify complex queries (encapsulate joins and filters behind a name), provide a security layer (expose only certain columns/rows), and give a stable interface even if underlying tables change. A standard view runs its query each time it is accessed. A materialized view stores the result physically and must be refreshed, trading storage and staleness for faster reads. Some simple views are updatable; complex ones are read-only.
- Encapsulates complex joins/filters behind a name
- Restricts access to selected columns or rows
- Stable interface over changing tables
AI Mentor Explanation
A view is like a pre-set "top scorers" summary card generated from the full scorebook: it stores no runs itself, it just re-runs the query on the raw data whenever you look. It hides the messy calculation behind one clean name and can show only what you’re allowed to see. A materialized view is like printing that summary once and refreshing it periodically — faster to read, but possibly slightly out of date.
Step-by-Step Explanation
Step 1
Define the view
CREATE VIEW name AS <select query> — store the query, not data.
Step 2
Query it like a table
SELECT from the view; the underlying query runs on access.
Step 3
Use for abstraction/security
Hide joins and restrict which columns/rows are exposed.
Step 4
Materialize if needed
Store results physically for speed, refreshing to control staleness.
What Interviewer Expects
- View as a virtual table / stored query with no data of its own
- Uses: abstraction, security, stable interface
- Standard vs materialized view distinction
- Awareness that only simple views are updatable
Common Mistakes
- Thinking a standard view stores data
- Assuming all views are updatable
- Confusing a view with a temporary table
- Forgetting materialized views need refreshing
Best Answer (HR Friendly)
“A view is a saved query that behaves like a virtual table — it stores no data itself but shows the query’s result whenever you use it. Views simplify complex queries, hide sensitive columns, and give a stable interface. A materialized view stores the result for speed but must be refreshed.”
Code Example
CREATE VIEW active_customers AS
SELECT customer_id, name, email
FROM customers
WHERE status = 'active';
-- Query it like a table
SELECT * FROM active_customers WHERE name LIKE 'A%';Follow-up Questions
- What is the difference between a view and a materialized view?
- When is a view updatable?
- How do views help with security?
- Do views improve query performance?
MCQ Practice
1. A standard SQL view?
A standard view is a virtual table — it runs its defining query on access and stores no data.
2. A materialized view differs by?
Materialized views store the query result on disk and require refreshing to stay current.
3. A common use of views is?
Views can expose only selected columns/rows, acting as a security layer.
Flash Cards
What is a view? — A virtual table defined by a stored query; holds no data of its own.
Materialized view? — Stores query results physically for speed; must be refreshed.
Why use views? — Abstraction, security (limit columns/rows), and a stable interface.
Are all views updatable? — No — only simple views; complex ones are read-only.