Mid-level 10 min · March 05, 2026

SQL Views — The 12M Row JOIN That Crashed Your Dashboard

A 12M row intermediate join from a simple view caused 504s in under an hour.

N
Naren Founder & Principal Engineer

20+ years shipping high-throughput database systems. Drawn from code that ran under real load.

Follow
Production
production tested
May 23, 2026
last updated
1,554
articles · all by Naren
 ● Production Incident 🔎 Debug Guide
Quick Answer
  • A view is a named, saved SELECT query — it stores the query definition, not the data itself
  • Views re-execute the underlying query every time they are accessed — they are not a cache
  • Materialized views store the query results physically and can be indexed — they are a cache
  • Updatable views allow INSERT/UPDATE/DELETE on the underlying table through the view — subject to restrictions
  • Views enforce security: GRANT SELECT ON view while hiding sensitive columns from the underlying table
  • Biggest mistake: treating a view as a performance optimization — a slow query behind a view is still a slow query
✦ Definition~90s read
What is SQL Views?

SQL views are virtual tables that encapsulate a SELECT query into a reusable, schema-level object. Unlike materialized views, which persist data to disk, standard views store no data themselves—they execute their underlying query every time you reference them in a FROM clause.

Imagine your company has a massive filing cabinet with thousands of folders.

This means a view joining 12 million rows across five tables will re-execute that join on every access, which is exactly why your dashboard crashed: the view looked like a table but behaved like a heavyweight query, and your BI tool paginated through it without understanding the cost. Views exist to provide logical abstraction, not performance optimization; use them to simplify complex queries, enforce row-level security, or mask columns, but never assume they cache results.

In the ecosystem, views sit between raw tables and application code as a governance layer. They compete with CTEs (which are ephemeral and scoped to a single query) and stored procedures (which can contain logic but return result sets). When you need to expose a subset of a table to a reporting tool without granting direct table access, a view is the right tool.

When you need to hide salary columns from junior analysts, a view with a column whitelist beats column-level permissions in many databases. But when you need sub-second response on a 100M-row aggregation, you want a materialized view, a summary table, or a columnstore index—not a standard view.

Critically, views are not just saved queries—they are first-class schema objects with their own permissions, dependencies, and behaviors. PostgreSQL, MySQL, SQL Server, and Oracle all support updatable views under specific conditions (single table, no aggregates, no DISTINCT), but most views are read-only by design.

The WITH CHECK OPTION clause prevents inserts or updates that would make rows invisible through the view, which is essential for data integrity when views enforce business rules. Dropping a view breaks nothing but the queries that reference it, making views safer to refactor than tables—but renaming a view can cascade failures through your ORM or BI layer just as easily.

Plain-English First

Imagine your company has a massive filing cabinet with thousands of folders. Every morning your manager needs the same five folders from the same three drawers. Instead of digging through the cabinet each time, you create a shortcut folder on the desk that automatically shows exactly what she needs. A SQL View is that shortcut folder — it's a saved query that looks and feels like a table, but the data always comes fresh from the real tables underneath.

Every production database grows complicated fast. A single order in an e-commerce system might touch five or six tables — customers, orders, order_items, products, addresses, and discounts. If every developer on your team writes their own join query to pull a customer order summary, you get six slightly different versions of the truth, and the day a column gets renamed you're hunting down broken queries across a dozen files. SQL Views were built precisely to kill that problem before it kills your sanity.

A view wraps a complex query into a single named object that lives inside the database. Anyone who needs that data just selects from the view — they don't need to know or care about the joins, filters, or subqueries underneath. When the underlying structure changes, you update the view in one place and every consumer is instantly fixed. Beyond simplicity, views also act as a security layer: you can grant a user access to a view that shows only certain columns or rows, without ever giving them access to the raw tables.

By the end of this article you'll know exactly when to reach for a view instead of repeating a query, how to build both read-only and updatable views, how to use views as a security boundary, and — critically — the limitations that trip up even experienced developers. You'll walk away with patterns you can drop into a real project today.

Why SQL Views Are Not Just Saved Queries

A SQL view is a virtual table defined by a SELECT statement that the database stores as a schema object. Unlike a materialized view, a standard view does not hold data — it's a saved query that runs every time you reference it. The core mechanic: the database engine merges the view's definition into the outer query during planning, then executes the combined statement against the underlying tables.

When you query a view, the optimizer expands it inline. This means every column reference, filter, and join in the view becomes part of the final execution plan. A view that joins 12 million rows across four tables will re-execute that join on every access unless the database caches the result. Indexes on the base tables still apply, but the view itself cannot be indexed. Performance depends entirely on how the optimizer rewrites the query — and it often fails to push predicates down through complex views, causing full table scans.

Use views to enforce column-level security, abstract table schemas, or provide a stable interface for reporting. But never assume a view is free. In production, a view that wraps a heavy join will crash dashboards when users filter by a column the optimizer cannot push down. The rule: treat a view as a macro that must be analyzed with EXPLAIN, not a precomputed result.

The Hidden Cost of Views
A view does not cache data — every SELECT against it re-executes the underlying query. A 12M-row join in a view runs 12M-row joins every time.
Production Insight
A BI dashboard querying a view with a WHERE clause on a computed column caused a 12M-row full scan every refresh — the optimizer couldn't push the filter into the view's inner join.
Symptom: dashboard load time jumped from 2 seconds to 47 seconds, and the database CPU pinned at 100% during business hours.
Rule of thumb: always run EXPLAIN on view-backed queries; if you see a full scan on a large table, rewrite the view or materialize it.
Key Takeaway
A view is a macro, not a cache — every query against it triggers the full underlying execution.
Predicate pushdown is not guaranteed; complex views often force full scans on large tables.
Always profile view queries with EXPLAIN before deploying to production — especially in reporting or dashboard contexts.
SQL Views: From Virtual Tables to Materialized THECODEFORGE.IO SQL Views: From Virtual Tables to Materialized Flow from view creation to performance and security traps Create View Stored SELECT query, not data Security Layer Hide columns, filter rows via view Updatable vs Read-Only WITH CHECK OPTION prevents corruption Materialized View Cached result set, faster reads Nested Views View calling another view adds complexity ⚠ Nested views + large joins = 12M row crash Materialize or limit rows; avoid deep nesting THECODEFORGE.IO
thecodeforge.io
SQL Views: From Virtual Tables to Materialized
Sql Views

Creating Your First View — and Understanding What Actually Happens

When you run CREATE VIEW, the database doesn't execute the query and store a snapshot of the results. It stores the query definition itself. Every time someone SELECTs from the view, the database runs that stored query fresh against the live tables. This is the single most important thing to understand about views: they are not cached copies of data. They are reusable, named queries.

This design has a beautiful consequence. If a new order is inserted into the orders table at 2pm, anyone querying the view at 2:01pm sees it immediately — no refresh, no sync, no ETL job needed.

Start with a realistic scenario. You have an e-commerce database. Business stakeholders constantly ask: 'Show me each customer's name, their total number of orders, and their lifetime spend.' Writing that join every time is tedious and error-prone. A view makes it a one-liner for every future query.

The syntax is simple, but the thinking behind it matters more than the keywords. You're essentially giving a SELECT statement a permanent name and home inside your database schema.

create_customer_order_summary_view.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- ─────────────────────────────────────────────────────────────
-- SETUP: Create the base tables we'll build our view on top of
-- ─────────────────────────────────────────────────────────────
CREATE TABLE customers (
    customer_id   INT          PRIMARY KEY,
    full_name     VARCHAR(100) NOT NULL,
    email         VARCHAR(150) NOT NULL,
    country       VARCHAR(50)  NOT NULL
);

CREATE TABLE orders (
    order_id     INT           PRIMARY KEY,
    customer_id  INT           NOT NULL REFERENCES customers(customer_id),
    order_date   DATE          NOT NULL,
    total_amount NUMERIC(10,2) NOT NULL
);

-- ─────────────────────────────────────────────────────────────
-- SEED DATA: Realistic rows so we can see real output
-- ─────────────────────────────────────────────────────────────
INSERT INTO customers VALUES
    (1, 'Amara Osei',    'amara@example.com',  'Ghana'),
    (2, 'Lena Fischer',  'lena@example.com',   'Germany'),
    (3, 'Carlos Ruiz',   'carlos@example.com', 'Mexico');

INSERT INTO orders VALUES
    (101, 1, '2024-01-15',  89.99),
    (102, 1, '2024-03-22', 214.50),
    (103, 2, '2024-02-10',  49.00),
    (104, 3, '2024-04-01', 320.00),
    (105, 3, '2024-04-18',  75.25);

-- ─────────────────────────────────────────────────────────────
-- THE VIEW: Stores the query definition, NOT the result data.
-- Every SELECT against this view re-runs the JOIN live.
-- ─────────────────────────────────────────────────────────────
CREATE VIEW customer_order_summary AS
SELECT
    c.customer_id,
    c.full_name,
    c.country,
    COUNT(o.order_id)       AS total_orders,      -- aggregates across all rows for this customer
    SUM(o.total_amount)     AS lifetime_spend,    -- total money spent, ever
    MAX(o.order_date)       AS last_order_date    -- most recent purchase date
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id  -- LEFT JOIN keeps customers with zero orders
GROUP BY
    c.customer_id,
    c.full_name,
    c.country;

-- ─────────────────────────────────────────────────────────────
-- USING THE VIEW: Looks exactly like querying a table
-- ─────────────────────────────────────────────────────────────
SELECT
    full_name,
    total_orders,
    lifetime_spend
FROM customer_order_summary
WHERE lifetime_spend > 100.00
ORDER BY lifetime_spend DESC;
Output
full_name | total_orders | lifetime_spend
---------------+--------------+---------------
Carlos Ruiz | 2 | 395.25
Amara Osei | 2 | 304.49
Key Mental Model:
Think of a view as a 'query alias', not a 'data copy'. The database runs the underlying SELECT fresh on every access. This means your data is always current, but it also means a poorly-written view query will be slow every time it's called — there's nowhere to hide a bad JOIN inside a view.
Production Insight
Views are query aliases, not data caches — a view on a slow query is a slow view.
EXPLAIN on a view-based query shows the full expanded plan — the view is transparent to the optimizer.
Materialized views (CREATE MATERIALIZED VIEW) store data physically and can be indexed — use them for expensive aggregations accessed frequently.
Key Takeaway
A regular view re-executes its query every time — no caching, no performance benefit from the view itself.
Materialized views store results and can be indexed — the right tool for expensive report queries.
Push selective WHERE conditions inside view definitions when the view is always queried with a specific filter.

Views as a Security Layer — Hide Columns, Filter Rows, Control Access

This is the use case that makes views indispensable in production systems — and the one most tutorials skip entirely. Views let you expose only the data a given role should see, without duplicating tables or writing application-level filters that can be bypassed.

Consider a HR system. The employees table holds salary, bank account numbers, and performance review scores alongside public info like name and department. Your operations team needs to look up who's in which department. Your payroll team needs salary data. Under no circumstances should either team see the other's sensitive columns.

Instead of granting direct table access, you create purpose-built views for each audience, grant SELECT on the view, and explicitly deny SELECT on the raw table. The database enforces this at the engine level — no application bug can accidentally leak a salary figure through a view that was never designed to show one.

Row-level filtering works the same way. A regional manager should only see employees in their region. You bake that WHERE clause into the view definition. They literally cannot query rows outside their region, because the view definition never fetches them.

security_views_hr_example.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- ─────────────────────────────────────────────────────────────
-- BASE TABLE: Contains sensitive columns alongside public ones
-- ─────────────────────────────────────────────────────────────
CREATE TABLE employees (
    employee_id    INT           PRIMARY KEY,
    full_name      VARCHAR(100)  NOT NULL,
    department     VARCHAR(80)   NOT NULL,
    region         VARCHAR(50)   NOT NULL,
    annual_salary  NUMERIC(12,2) NOT NULL,   -- SENSITIVE
    bank_account   VARCHAR(30)   NOT NULL,   -- SENSITIVE
    hire_date      DATE          NOT NULL
);

INSERT INTO employees VALUES
    (1, 'Yuki Tanaka',    'Engineering', 'APAC',   95000.00, 'GB29NWBK60161331926819', '2021-06-01'),
    (2, 'Sofia Delgado',  'HR',          'EMEA',   72000.00, 'DE89370400440532013000', '2020-03-15'),
    (3, 'Marcus Webb',    'Engineering', 'EMEA',   88000.00, 'FR7614508059405402982935', '2019-11-22'),
    (4, 'Priya Sharma',   'Sales',       'APAC',   67000.00, 'IN30267931234567890123', '2022-09-10');

-- ─────────────────────────────────────────────────────────────
-- VIEW 1: Operations team — sees name, department, region only.
-- Salary and bank account columns are simply not included.
-- ─────────────────────────────────────────────────────────────
CREATE VIEW employee_directory AS
SELECT
    employee_id,
    full_name,
    department,
    region,
    hire_date
FROM employees;  -- no salary, no bank_account — they don't exist in this view

-- ─────────────────────────────────────────────────────────────
-- VIEW 2: Payroll team — salary visible, but still no bank
-- account number (that's only for the finance system to access)
-- ─────────────────────────────────────────────────────────────
CREATE VIEW employee_payroll_summary AS
SELECT
    employee_id,
    full_name,
    department,
    annual_salary
FROM employees;  -- bank_account intentionally excluded

-- ─────────────────────────────────────────────────────────────
-- VIEW 3: EMEA regional manager — row-level restriction.
-- This view physically cannot return rows from other regions.
-- ─────────────────────────────────────────────────────────────
CREATE VIEW emea_employee_directory AS
SELECT
    employee_id,
    full_name,
    department,
    hire_date
FROM employees
WHERE region = 'EMEA';  -- the filter lives in the database, not the application

-- ─────────────────────────────────────────────────────────────
-- GRANT access to the VIEW only. The ops_user role never gets
-- SELECT on the raw employees table.
-- ─────────────────────────────────────────────────────────────
-- GRANT SELECT ON employee_directory TO ops_user;
-- REVOKE SELECT ON employees FROM ops_user;

-- Test the EMEA view — only EMEA rows should appear
SELECT * FROM emea_employee_directory ORDER BY full_name;
Output
employee_id | full_name | department | hire_date
------------+----------------+-------------+-----------
3 | Marcus Webb | Engineering | 2019-11-22
2 | Sofia Delgado | HR | 2020-03-15
Pro Tip — WITH CHECK OPTION:
If you allow INSERTs or UPDATEs through a view, add WITH CHECK OPTION to the view definition. This forces the database to reject any write that would create a row the view itself couldn't see — for example, inserting an APAC employee through the emea_employee_directory view would be blocked. Without it, someone can silently insert a row that then vanishes from the view, which is a data integrity nightmare.
Production Insight
Views are the standard database security layer — grant SELECT on a view, revoke SELECT on the underlying table.
CREATEOR REPLACE VIEW is safe — it redefines the view without dropping dependent objects.
For row-level security, add WHERE clauses inside the view: CREATE VIEW my_orders AS SELECT * FROM orders WHERE user_id = current_user_id().
Key Takeaway
Views are the correct way to expose a subset of columns or rows without granting access to the underlying table.
GRANT SELECT ON view_name TO role_name — the role never sees the underlying table's sensitive columns.
Row-level security inside views (WHERE user_id = current_user()) is a proven pattern for multi-tenant data isolation.

Updatable Views vs. Read-Only Views — Knowing Which is Which

Not all views are equal when it comes to writing data back through them. Some views allow INSERT, UPDATE, and DELETE — these are called updatable views. Others are permanently read-only. Knowing the rules prevents runtime errors and design mistakes.

A view is updatable when it maps cleanly to a single base table with no transformation: no GROUP BY, no aggregate functions like SUM or COUNT, no DISTINCT, no subqueries in the SELECT list, and no JOINs that would make row identity ambiguous. If the database can figure out exactly which row in exactly which table to touch, it'll allow the write.

The moment you add an aggregate, a GROUP BY, or a JOIN across multiple tables, the view becomes read-only. This makes sense when you think about it: if a view row represents the SUM of five order rows, which of those five rows should an UPDATE actually modify?

Materialized views are a separate concept (available in PostgreSQL, Oracle, and others) where the query result IS physically stored. They're fast to read but require explicit refreshing. The trade-off is freshness vs. performance — crucial for dashboards and reporting.

updatable_vs_readonly_views.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- ─────────────────────────────────────────────────────────────
-- SETUP
-- ─────────────────────────────────────────────────────────────
CREATE TABLE products (
    product_id   INT           PRIMARY KEY,
    product_name VARCHAR(100)  NOT NULL,
    category     VARCHAR(50)   NOT NULL,
    unit_price   NUMERIC(8,2)  NOT NULL,
    is_active    BOOLEAN       NOT NULL DEFAULT TRUE
);

INSERT INTO products VALUES
    (1, 'Wireless Keyboard', 'Electronics', 49.99, TRUE),
    (2, 'Desk Lamp',         'Office',      29.99, TRUE),
    (3, 'Ergonomic Chair',   'Furniture',  349.00, FALSE),
    (4, 'USB-C Hub',         'Electronics', 34.99, TRUE);

-- ─────────────────────────────────────────────────────────────
-- UPDATABLE VIEW: Single table, no aggregates, no DISTINCT.
-- The database can map each view row to exactly one table row.
-- ─────────────────────────────────────────────────────────────
CREATE VIEW active_products AS
SELECT
    product_id,
    product_name,
    category,
    unit_price
FROM products
WHERE is_active = TRUE;  -- simple filter, view is still updatable

-- This UPDATE works — the database knows exactly which row to change
UPDATE active_products
SET unit_price = 54.99
WHERE product_id = 1;  -- maps cleanly to products row with product_id = 1

-- Confirm the change made it through to the base table
SELECT product_id, product_name, unit_price FROM products WHERE product_id = 1;

-- ─────────────────────────────────────────────────────────────
-- READ-ONLY VIEW: Uses GROUP BY + COUNT — not updatable.
-- The database can't reverse-engineer which base rows to touch.
-- ─────────────────────────────────────────────────────────────
CREATE VIEW product_count_by_category AS
SELECT
    category,
    COUNT(*)          AS product_count,
    AVG(unit_price)   AS avg_price
FROM products
GROUP BY category;

-- This would FAIL with: ERROR: cannot update a non-updatable view
-- UPDATE product_count_by_category SET avg_price = 40.00 WHERE category = 'Electronics';
-- (Commented out so the script runs cleanly — uncomment to see the error)

-- Safe to SELECT from it though
SELECT * FROM product_count_by_category ORDER BY category;
Output
-- After the UPDATE through active_products:
product_id | product_name | unit_price
-----------+--------------------+-----------
1 | Wireless Keyboard | 54.99
-- product_count_by_category SELECT result:
category | product_count | avg_price
------------+---------------+-----------
Electronics | 2 | 44.99
Furniture | 1 | 349.00
Office | 1 | 29.99
Watch Out — The Silent DELETE Problem:
With an updatable view that has a WHERE filter, you can UPDATE a row so it no longer satisfies the view's WHERE clause. The row doesn't get deleted — it just disappears from the view while still living in the base table. This looks exactly like a delete to anyone querying the view. Always add WITH CHECK OPTION to prevent writes that would make rows 'vanish' silently.
Production Insight
Updatable views require a simple SELECT on a single table without DISTINCT, GROUP BY, aggregate functions, or subqueries.
Most production views are read-only — treat them as such and update the underlying tables directly.
INSTEAD OF triggers on views (PostgreSQL) allow DML on complex non-updatable views when the update semantics are well-defined.
Key Takeaway
Updatable views are the exception, not the rule — most views with JOINs or aggregates are read-only.
For complex view DML, INSTEAD OF triggers handle the routing to underlying tables.
Always test INSERT/UPDATE through a view with a single row before bulk operations.

Replacing and Dropping Views — Managing Views in a Real Schema

Views aren't fire-and-forget. Business requirements change, columns get renamed, and performance optimizations force you to refactor the underlying query. Knowing how to safely update and remove views without taking down dependent code is a practical skill that separates juniors from seniors.

CREATE OR REPLACE VIEW lets you redefine a view's query in place, without dropping dependent objects or revoking existing permissions. There's one catch: you can't remove columns from the SELECT list using REPLACE — you can only add new ones or change existing expressions. Removing columns requires a DROP followed by a CREATE.

Before dropping a view, check whether other views, stored procedures, or application queries depend on it. Most databases give you a system catalog to query for this. Dropping a view with CASCADE will also drop anything that depends on it — a powerful option that can silently destroy more than you intended.

Replacing the query inside a view is also how you fix a performance problem. If you realize the underlying JOIN was written inefficiently, you REPLACE the view with an optimized query and every caller instantly benefits, with no code changes outside the database.

managing_views_lifecycle.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- ─────────────────────────────────────────────────────────────
-- ORIGINAL VIEW (from our first section)
-- ─────────────────────────────────────────────────────────────
-- CREATE VIEW customer_order_summary AS ... (already created above)

-- ─────────────────────────────────────────────────────────────
-- SCENARIO: Business now wants average order value added.
-- Use CREATE OR REPLACE — keeps existing GRANTs and dependencies.
-- ─────────────────────────────────────────────────────────────
CREATE OR REPLACE VIEW customer_order_summary AS
SELECT
    c.customer_id,
    c.full_name,
    c.country,
    COUNT(o.order_id)                       AS total_orders,
    SUM(o.total_amount)                     AS lifetime_spend,
    ROUND(AVG(o.total_amount), 2)           AS avg_order_value,  -- NEW column added safely
    MAX(o.order_date)                       AS last_order_date
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY
    c.customer_id,
    c.full_name,
    c.country;

-- ─────────────────────────────────────────────────────────────
-- CHECK DEPENDENCIES before dropping (PostgreSQL syntax).
-- Run this BEFORE any DROP VIEW to see what you'd break.
-- ─────────────────────────────────────────────────────────────
SELECT
    dependent_view.relname   AS view_that_depends,
    source_view.relname      AS depends_on_this_view
FROM pg_depend dep
JOIN pg_rewrite rw       ON dep.objid      = rw.oid
JOIN pg_class dependent_view ON rw.ev_class = dependent_view.oid
JOIN pg_class source_view    ON dep.refobjid = source_view.oid
WHERE source_view.relname = 'customer_order_summary'  -- replace with your view name
  AND dependent_view.relname != source_view.relname;  -- exclude self-reference

-- ─────────────────────────────────────────────────────────────
-- SAFE DROP: No cascade — fails loudly if dependencies exist.
-- That loud failure is a feature, not a bug.
-- ─────────────────────────────────────────────────────────────
-- DROP VIEW customer_order_summary;                 -- safe: errors if depended on
-- DROP VIEW customer_order_summary CASCADE;         -- dangerous: silently drops dependents

-- Verify the updated view includes the new column
SELECT
    full_name,
    total_orders,
    lifetime_spend,
    avg_order_value
FROM customer_order_summary
ORDER BY lifetime_spend DESC;
Output
full_name | total_orders | lifetime_spend | avg_order_value
---------------+--------------+----------------+-----------------
Carlos Ruiz | 2 | 395.25 | 197.63
Amara Osei | 2 | 304.49 | 152.25
Lena Fischer | 1 | 49.00 | 49.00
Watch Out — DROP CASCADE is a Wrecking Ball:
DROP VIEW my_view CASCADE will silently remove every view, function, or trigger that depends on my_view — including views that depend on those views. In production, always use DROP VIEW without CASCADE first. If it errors, read the dependency list carefully. Only use CASCADE when you genuinely intend to destroy the entire dependency chain and have confirmed it's safe to do so.
Production Insight
CREATE OR REPLACE VIEW preserves view permissions — prefer it over DROP + CREATE which loses all grants.
Orphan views (referencing renamed or dropped tables) produce errors at query time, not at view creation time.
View dependencies can be checked: SELECT * FROM information_schema.view_table_usage WHERE view_name = 'your_view'.
Key Takeaway
CREATE OR REPLACE VIEW is idempotent and preserves permissions — use it in migration scripts.
Views don't validate their underlying tables at creation time — only at query time.
Document view dependencies explicitly — renaming a table breaks views silently until they're queried.

The WITH CHECK OPTION — Preventing Data Corruption Through Your Views

You built a view that filters rows. A junior runs an UPDATE through it. Suddenly rows disappear from the view. The data didn't vanish — the update pushed those rows outside the view's filter predicate. Now nobody can see them through that view, but they're still in the base table. That's a silent schema violation.

WITH CHECK OPTION forces every write through the view to satisfy the WHERE clause. PostgreSQL, SQL Server, MySQL, Oracle — they all support it, but most devs never use it. The result: views that leak data you thought you'd locked down.

When you define a view as CREATE VIEW active_orders AS SELECT * FROM orders WHERE status = 'active' WITH CHECK OPTION, any UPDATE setting status to 'cancelled' or INSERT with status 'archived' gets rejected. The database enforces the boundary. No surprises. No midnight debugging sessions because "the view just lost 200 rows."

Always add WITH CHECK OPTION on updatable views with filters. Your future self will thank you when a data pipeline doesn't silently eat records.

CheckOptionGuard.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// io.thecodeforge — database tutorial

-- Bad: no check option allows updates that make rows invisible
CREATE VIEW active_orders AS
SELECT id, customer_id, status, total
FROM orders
WHERE status = 'active';

-- This succeeds but row vanishes from view
UPDATE active_orders SET status = 'cancelled' WHERE id = 1042;

-- Good: WITH CHECK OPTION prevents the escape
CREATE VIEW active_orders_safe AS
SELECT id, customer_id, status, total
FROM orders
WHERE status = 'active'
WITH CHECK OPTION;

-- This now fails with:
-- ERROR: new row violates WITH CHECK OPTION for view "active_orders_safe"
UPDATE active_orders_safe SET status = 'cancelled' WHERE id = 1042;
Output
ERROR: new row violates WITH CHECK OPTION for view "active_orders_safe"
Production Trap:
WITH CHECK OPTION applies to INSERT and UPDATE through the view, but DELETE bypasses it entirely — you can still delete visible rows. This is intentional, but catches everyone once.
Key Takeaway
Any updatable view with a WHERE clause needs WITH CHECK OPTION unless you want data to disappear from the view silently.

Materialized Views — When a Virtual Table Isn't Fast Enough

Standard views are just query macros. Every SELECT re-executes the entire underlying query. Fine for light filters. Terrible for aggregates on million-row tables. Your dashboard query that joins five tables and runs GROUP BY on 3M rows? That view takes 12 seconds every page load.

Materialized views cache the result as an actual table. The database updates it on a schedule or on demand. PostgreSQL calls them MATERIALIZED VIEW. SQL Server calls them indexed views. BigQuery has them natively. Oracle has had them since the 90s.

The trade-off: you get read performance of a table with the logical abstraction of a view. The cost is staleness and storage. You refresh the materialized view with REFRESH MATERIALIZED VIEW monthly_sales_summary — but between refreshes, the data lags behind the base tables.

Use materialized views for: reporting aggregates, warehouse-style rollups, cross-database joins where latency kills you. Don't use them for real-time operational queries. Know your refresh tolerance before you build.

MaterializedReporting.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// io.thecodeforge — database tutorial

-- Slow: standard view recalculates every query
CREATE VIEW daily_sales_report AS
SELECT DATE(created_at) AS sale_date,
       product_id,
       COUNT(*) AS units_sold,
       SUM(amount) AS revenue
FROM transactions
GROUP BY DATE(created_at), product_id;

-- Fast: materialized view stores result (PostgreSQL syntax)
CREATE MATERIALIZED VIEW daily_sales_materialized AS
SELECT DATE(created_at) AS sale_date,
       product_id,
       COUNT(*) AS units_sold,
       SUM(amount) AS revenue
FROM transactions
GROUP BY DATE(created_at), product_id
WITH DATA;

-- Refresh on schedule (cron job or pg_cron)
REFRESH MATERIALIZED VIEW daily_sales_materialized;
Output
CREATE MATERIALIZED VIEW
CREATE INDEX idx_daily_sales_date ON daily_sales_materialized (sale_date);
-- Query now returns in <200ms instead of 12s
Senior Shortcut:
In PostgreSQL, create unique indexes on materialized views to enable concurrent refresh — REFRESH MATERIALIZED VIEW CONCURRENTLY avoids locking reads while rebuilding the data.
Key Takeaway
Materialized views trade freshness for speed. Use them when your standard view takes longer to query than your refresh interval allows.

Nested Views — Why Your View Should Call Another View

A view is a table to another view. That isn't a bug — it's a weapon. Real schemas stack views to decompose complex logic into testable layers. Instead of one 200-line monster, you build a base view that cleans the data, a mid-level view that joins domains, and an outer view that applies security policies.

This isn't just neat. It's survivable. When your CEO asks for a report that excludes archived orders, you change one base view. Every dependent view picks it up. No cascading rewrites. No hunting for copy-pasted WHERE clauses across ten files.

Production trap: nested views can hit recursion limits. PostgreSQL defaults to 100. MySQL hates deep stacks. Keep it under 3 levels. If your stack is deeper, you're abusing views — that's a stored procedure or a CTE screaming to be born.

NestedViewsExample.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// io.thecodeforge — database tutorial

-- Base view: clean raw orders
CREATE VIEW v_clean_orders AS
SELECT id, customer_id, amount, order_date
FROM raw_orders
WHERE deleted_at IS NULL;

-- Mid view: join with geography
CREATE VIEW v_regional_orders AS
SELECT o.id, o.amount, c.region
FROM v_clean_orders o
JOIN customers c ON c.id = o.customer_id;

-- Outer view: apply security filter
CREATE VIEW v_eu_orders AS
SELECT * FROM v_regional_orders
WHERE region = 'EU';
Output
SELECT * FROM v_eu_orders;
id | amount | region
----+--------+--------
42 | 150 | EU
88 | 200 | EU
Recursion Ambush:
A nested view referencing itself (directly or indirectly) will crash the query planner. Always graph your view dependencies before deployment. A single circular reference takes down the whole schema.
Key Takeaway
Compose views like functions — one responsibility per layer, never deeper than 3 levels.

Listing & Documenting Views — Your Schema Isn't Self-Explaining

Production schemas accumulate views like dust. Six months in, nobody remembers why v_old_reports exists. Two problems: finding what you have, and knowing what it does. SQL gives you metadata commands for both.

PostgreSQL's \dv or information_schema.views lists every view with its definition. MySQL's SHOW FULL TABLES marks views. Use these to audit before you deploy. Better yet — put a COMMENT on every view. That comment survives migrations, survives team turnover, and shows up in pg_stat_statements when the DBA asks why someone querying v_old_reports is causing a seq scan.

Senior move: query information_schema.views with a WHERE clause to find views using deprecated columns. If you renamed status to order_status last month, find every view that still says status before it breaks production at 3 AM.

ListViewsExample.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
// io.thecodeforge — database tutorial

-- PostgreSQL: list all views with comments
SELECT v.table_name,
       pg_catalog.obj_description(c.oid, 'pg_class') AS comment
FROM information_schema.views v
JOIN pg_catalog.pg_class c ON c.relname = v.table_name
WHERE v.table_schema = 'public';

-- Then add or update a comment
COMMENT ON VIEW v_eu_orders IS
  'EU customer orders, excludes archived records. Updated daily by ETL job. Contact: dba@example.com';
Output
table_name | comment
-------------+--------------------------------------------
v_eu_orders | EU customer orders, excludes archived...
v_clean | Raw orders stripped of soft-deleted rows
Senior Shortcut:
Automate a weekly SELECT * FROM information_schema.views into your team's wiki. A stale view that nobody maintains is a production incident waiting to happen.
Key Takeaway
Document every view with COMMENT ON — because your future self and the on-call engineer will curse you if you don't.

Advanced Techniques with Views — Beyond Basic Abstraction

Views can do more than hide columns or simplify joins. Use views to enforce row-level security through dynamic predicates like WHERE user_id = current_user_id(), ensuring tenants only see their own data without application-level filters. Combine views with window functions to create rolling aggregates — a weekly sales view that always reflects the last 7 days without manual queries. For schema evolution, build versioned views (e.g., customer_v2) that map old column names to new ones, allowing backward compatibility while you migrate clients. Another pattern: union views that merge identical structures from multiple sharded tables — a sales_global view that UNION ALL from sales_us and sales_eu, simplifying cross-region reporting. These techniques shift complexity from application code to the database, where set operations are optimized. Warning: overuse can create debugging nightmares. Always test view query plans with EXPLAIN ANALYZE.

AdvancedViews.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// io.thecodeforge — database tutorial

CREATE VIEW tenant_orders AS
SELECT id, product, amount
FROM orders
WHERE tenant_id = current_setting('app.current_tenant_id')::int;

CREATE VIEW weekly_sales AS
SELECT product,
       SUM(amount) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS rolling_7_day
FROM orders;

CREATE VIEW v2_customer AS
SELECT id, full_name AS name, email
FROM customer_v1;
Output
Views created successfully.
Query: SELECT * FROM tenant_orders — returns rows matching the session's tenant_id.
Performance Trap:
Dynamic predicates in views prevent materialized caching — every call re-runs the filter. For high-traffic tenant views, prefix with a materialized view refreshed per session.
Key Takeaway
Views enforce business rules and schema compatibility at the database layer, not the app layer.

Common View Tasks — What You Actually Do with Them

Three frequent tasks dominate real-world view usage: refreshing stale aggregates, documenting view purpose, and checking updatability. For materialized views, run REFRESH MATERIALIZED VIEW CONCURRENTLY to avoid locking reads — schedule it during low traffic. Document views with COMMENT ON VIEW — this metadata survives schema exports and ORM introspection. To list all views, query information_schema.views or pg_views (Postgres) — filter by schema and check is_updatable column to separate read-only from updatable views. When debugging, use SHOW CREATE VIEW (MySQL) or pg_get_viewdef() (Postgres) to recover the definition verbatim. For renaming, always test dependencies with SELECT * FROM information_schema.view_table_usage WHERE view_name='...'. A common mistake: dropping a view that other views depend on — you'll get a cascade error. Use DROP VIEW IF EXISTS ... RESTRICT to fail safely.

CommonViewTasks.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// io.thecodeforge — database tutorial

REFRESH MATERIALIZED VIEW CONCURRENTLY sales_summary;

COMMENT ON VIEW customer_orders IS 'Shows orders for active customers only';

SELECT table_name, is_updatable
FROM information_schema.views
WHERE table_schema = 'public';

-- Recover definition
SELECT pg_get_viewdef('customer_orders', true);

-- Safe drop
DROP VIEW IF EXISTS monthly_report RESTRICT;
Output
Materialized view refreshed. Comment added. View list returned.
pg_get_viewdef displays the SELECT statement as text.
Production Trap:
Running REFRESH MATERIALIZED VIEW without CONCURRENTLY locks the table — queries to it will fail until the refresh completes. For live systems, always use CONCURRENTLY and create a unique index first.
Key Takeaway
Schedule maintenance tasks like refresh and documentation outside peak hours to avoid production impact.
● Production incidentPOST-MORTEMseverity: high

Dashboard Timed Out After Adding a View for 'Convenience'

Symptom
The customer dashboard timed out for all users after a 'non-breaking' change that added a view. The dashboard team reported 504 errors within an hour of deployment.
Assumption
The developer believed the view would cache the expensive JOIN, making the dashboard query cheaper. Views are 'precomputed', right?
Root cause
Standard views are not precomputed. The dashboard query SELECT * FROM customer_dashboard_view was equivalent to running the full 6-table JOIN inline. Without the WHERE clause on the view's source tables (the dashboard query applied WHERE after the view), the view joined all rows before filtering. EXPLAIN showed a 12M row intermediate result before the final WHERE narrowed it to 50 rows.
Fix
Added the critical WHERE predicate inside the view definition itself. Converted to a materialized view with REFRESH MATERIALIZED VIEW CONCURRENTLY on a 5-minute cron. Dashboard query went from 45s to 80ms.
Key lesson
  • Views are not caches — every SELECT from a view re-runs the underlying query
  • Push WHERE predicates inside the view definition when the view is always queried with a specific filter
  • For expensive aggregate views, use materialized views with scheduled refresh
Production debug guideDiagnosing view performance and updatability issues3 entries
Symptom · 01
Query on a view is much slower than expected
Fix
Run EXPLAIN ANALYZE on the view query. The view is expanded inline — you'll see the full underlying query in the plan. Look for missing WHERE predicates being applied outside the view. Add the most selective filter conditions inside the view definition itself.
Symptom · 02
Error: cannot update a non-updatable view
Fix
Views with JOINs, GROUP BY, DISTINCT, aggregate functions, or subqueries are not updatable. Simplify the view to a single-table SELECT without these features, or use INSTEAD OF triggers (PostgreSQL) to intercept DML on complex views.
Symptom · 03
View returns stale data after underlying table changes
Fix
If this is a regular view, it should return current data — investigate whether you're querying a materialized view instead (\dv in PostgreSQL shows view type). For materialized views, run REFRESH MATERIALIZED VIEW view_name to update the data.
AspectRegular ViewMaterialized View
Data storageNo data stored — query runs on every accessResult set physically stored on disk
Data freshnessAlways current — reflects live table changes instantlyStale until manually or scheduled REFRESH is run
Read performanceAs slow as the underlying query every timeVery fast — reads pre-computed results like a table
Write supportUpdatable views possible (with restrictions)Never directly writable — always read-only
Storage costZero — only the query definition is storedDisk space proportional to the result set size
Best use caseSecurity layers, query simplification, always-live reportsHeavy aggregation, dashboards, slow-changing analytical queries
REFRESH neededNever — no concept of refreshYes — REFRESH MATERIALIZED VIEW must be triggered

Key takeaways

1
A view stores a query definition, not data
every SELECT re-runs the underlying query against the live tables, so your results are always fresh but a bad query is always slow.
2
Views are one of the most underused security tools in SQL
by granting access to a view instead of a base table, you enforce column-level and row-level access control at the database engine, not the application layer.
3
A view is updatable only when the database can unambiguously map one view row to exactly one base table row
aggregates, GROUP BY, DISTINCT, and multi-table JOINs break updatability.
4
Always use CREATE OR REPLACE VIEW to update a view in place (preserving permissions), and always run a dependency check before DROP VIEW
DROP CASCADE can silently destroy dependent objects across your entire schema.

Common mistakes to avoid

3 patterns
×

Treating a view as a performance optimization or cache

Symptom
Dashboard or report query is slow despite being 'simplified' by a view — EXPLAIN shows the full expensive underlying join is still executing
Fix
Views are transparent to the query optimizer — a slow query behind a view is still slow. For caching, use a materialized view: CREATE MATERIALIZED VIEW ... AS SELECT ... with REFRESH MATERIALIZED VIEW on a schedule. Index the materialized view for fast access.
×

Selecting from a view without the WHERE clause that belongs inside the view

Symptom
The view generates a massive intermediate result set before the WHERE is applied, causing full table scans on large tables
Fix
Move the most selective WHERE conditions inside the view definition when the view is always accessed with those conditions. A view that always filters by active status should have WHERE status = 'active' in its definition, not in every query that uses it.
×

Dropping and recreating a view instead of using CREATE OR REPLACE VIEW

Symptom
After a view is recreated, all previously granted permissions are lost — users or roles that had SELECT on the view can no longer access it
Fix
Use CREATE OR REPLACE VIEW view_name AS SELECT ... — it redefines the view while preserving all existing permissions. DROP VIEW is necessary only when changing the number of columns or column types.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01JUNIOR
What is a view in SQL and what are its main use cases?
Q02SENIOR
What is the difference between a view and a materialized view?
Q03SENIOR
Can you UPDATE rows through a view? What are the restrictions?
Q01 of 03JUNIOR

What is a view in SQL and what are its main use cases?

ANSWER
A view is a named saved SELECT query stored in the database. When queried, the view's underlying SELECT is executed as if it were written inline. The three main use cases are: (1) Simplification — wrapping a complex multi-table JOIN into a readable name that application code can SELECT from without duplicating the JOIN logic. (2) Security — granting SELECT on a view that exposes only non-sensitive columns while the application role has no access to the underlying table. (3) Logical data model — presenting data in a shape the application expects without denormalizing the physical schema.
FAQ · 3 QUESTIONS

Frequently Asked Questions

01
Does a SQL view store data or just the query?
02
Can you INSERT or UPDATE data through a SQL view?
03
Why would you use a view instead of just writing the query every time?
N
Naren Founder & Principal Engineer

20+ years shipping high-throughput database systems. Drawn from code that ran under real load.

Follow
Verified
production tested
May 23, 2026
last updated
1,554
articles · all by Naren
🔥

That's SQL Advanced. Mark it forged?

10 min read · try the examples if you haven't

Previous
SQL Indexes
2 / 16 · SQL Advanced
Next
SQL Stored Procedures