Views: Creating Virtual Tables for Security & Simplicity
Expose curated query interfaces with views for security and reuse.
In plain words
A view is a saved question. When you look at it, the database answers the question again using the latest data.
Think of it like…
A saved filter in your email app: "unread from my manager" doesn't copy any emails, it just shows the matching ones each time you open it.
Where you’ve seen it
CREATE VIEW customer_contacts AS SELECT id, name, city FROM customers WHERE deleted_at IS NULL; then GRANT SELECT ON customer_contacts TO support_team;. Support can look up customers, but never sees email, phone or deleted accounts.
Words you’ll hear
- View
- A named, stored query; no stored data.
- Materialized view
- A view whose results are stored and must be refreshed.
- Updatable view
- A simple single-table view you can INSERT, UPDATE or DELETE through.
- security_barrier / security_invoker
- PostgreSQL options that make views safer as security boundaries.
Visual diagram
Theory & example queries
What a view is good for
Abstraction: a view gives one agreed definition of a business concept (active customers, net revenue) so every report uses the same logic, and you can change the underlying tables while keeping the view's columns stable for its users.
Security: grant SELECT on the view, not the base tables, to expose only certain columns or rows. In PostgreSQL, add WITH (security_barrier) for row-filtering views so cleverly written functions in user queries can't leak hidden rows, and from PostgreSQL 15, security_invoker makes the view check the caller's own permissions on the base tables.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
CREATE VIEW net_revenue_daily ASSELECT created_at::date AS DAY, SUM(amount) - COALESCE(SUM(refund_amount), 0) AS netFROM ordersWHERE status IN ('paid', 'refunded')GROUP BY 1;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
View creation status
Note: Representative PostgreSQL response; the grouped revenue is computed only when the view is queried.
Key takeaway: Views centralise logic and limit what users can see.
Performance and maintenance
A normal view has no performance benefit of its own: its query runs every time, though the optimizer usually merges it into the outer query so indexes still apply. Views built on views built on views become hard to optimise and debug, so keep the layers shallow.
Views depend on their tables. PostgreSQL won't let you drop a column a view uses, and a view defined with SELECT * stores the column list from when it was created, so columns added later don't appear until you recreate it. List columns explicitly. When the query is expensive and slightly stale data is acceptable, use a materialized view and refresh it on a schedule.
CREATE MATERIALIZED VIEW sales_by_monthASSELECT …;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
REFRESH MATERIALIZED VIEW CONCURRENTLY sales_by_month;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
Materialized view workflow status
Note: Assumes the ellipsis is replaced with a complete SELECT and the required unique index exists before the concurrent refresh; command tags vary by client.
then
nightly (CONCURRENTLY needs a unique index on the view).
Key takeaway: Views cost what their query costs; materialize when you need speed over freshness.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Write and test the query
Get the SELECT right first, listing columns explicitly rather than using *.
SELECT id, name, cityFROM customersWHERE deleted_at IS NULLWhen to use it & trade-offs
What it is
A view is a saved SELECT query with a name: CREATE VIEW active_customers AS SELECT …. It stores no data of its own; every time you query it, the database runs the underlying query (usually merging it into yours). You can query it like a table, and grant access to it without granting access to the tables beneath.
Why it matters: Views give a stable, simple interface over a messy schema: one definition of "active customer" that every report reuses, and a way to expose only some columns or rows to an analyst or a service. Interviewers often ask "view vs materialized view vs table".
When to reach for it
- One shared definition of a business metric or entity
- Exposing a subset of columns or rows to a role
- Keeping old column names working during a schema change
When to avoid or be careful
- Expecting a normal view to speed up a slow query
- Deep stacks of views on views
- SELECT * in view definitions
View vs materialized view vs table
Strengths: Always current, no storage, no refresh.
Watch out: No faster than the query it wraps.
Strengths: Fast reads from stored results; can be indexed.
Watch out: Must be refreshed; data is as old as the last refresh.
Strengths: Full control over loading, indexing and history.
Watch out: You write and run the loading process yourself.
Interview signals & numbers to know
When this topic comes up
- “View vs materialized view”
- “Give analysts access to some columns only”
- “Every team calculates active users differently”
- Schema refactoring
- Row-level access
Benchmarks & rules of thumb
- 0 bytes
- of data stored by a normal view
- PostgreSQL 15
- added security_invoker views
- Unique index
- required for REFRESH … CONCURRENTLY
Common mistakes & follow-ups
Common interview mistakes
- Creating a view to make a query faster and finding it just as slow.
- Defining a view with SELECT * and being surprised that new columns don't appear.
- Granting access to the base tables as well, which defeats the view as a security boundary.
Likely interviewer follow-ups
- What's the difference between a view and a materialized view?
- Can you update data through a view? When?
- How would you use views to give a team access to only some customer columns?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Views: Creating Virtual Tables for Security & Simplicity with hands-on SQL practice and interview Q&A.