Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 111/ 160
06. Database Design, Normalization & Semi-Structured SQL

Updatable Views & WITH CHECK OPTION

Update through views safely and enforce WITH CHECK OPTION constraints.

IntermediateUpdatable View · WITH CHECK OPTION3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

You can change data through a simple view, and CHECK OPTION makes sure the changed rows still belong in that view.

Think of it like…

A department's shared folder that only shows files tagged with that department: CHECK OPTION means you can't save a file into the folder with another department's tag, and you can't retag a file so it disappears from the folder.

Where you’ve seen it

A support tool writes through CREATE VIEW eu_customers AS SELECT id, name, email, region FROM customers WHERE region = 'EU' WITH CHECK OPTION; When an agent tries UPDATE eu_customers SET region = 'US' WHERE id = 7, PostgreSQL rejects it with "new row violates check option for view", so EU staff can't accidentally move customers out of their own scope.

Words you’ll hear

Updatable view
A view simple enough that writes map to rows of one base table.
WITH CHECK OPTION
Rejects inserted or updated rows that wouldn't be visible through the view.
LOCAL vs CASCADED
Check only this view's conditions, or also those of the views it's built on.
INSTEAD OF trigger
Custom logic that makes a complex (non-updatable) view writable.

Visual diagram

Loading interactive diagram

Theory & example queries

Which views can be written to

In PostgreSQL 9.3+, a view is automatically updatable when it selects from exactly one table or updatable view, with no WITH, DISTINCT, GROUP BY, HAVING, LIMIT, OFFSET or set operations (UNION, INTERSECT, EXCEPT) at the top level, and no aggregates, window functions or set-returning functions in the select list. Columns that are plain references to base columns can be updated; computed columns such as price * 1.18 AS price_with_tax are read-only.

MySQL and SQL Server follow similar rules: each written row must map to exactly one base-table row, and a statement may change columns of only one base table. Inserting through a view fills columns the view doesn't expose with their defaults, so those columns need defaults or must be nullable. For views with joins or aggregates, write an INSTEAD OF trigger that defines what an insert or update should do.

A view that can't be updatedQuery example
1Input dataStart from the source rows or state that the statement sees.
orders
3 rows
orders sample rows
idcustomer_idstatusamountcoupon_code
1011paid249NULL
1022paid120WELCOME10
1031shipped89.5NULL
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1CREATE VIEW customer_order_totals AS2SELECT3  customer_id,4  SUM(amount) AS total5FROM6  orders7GROUP BY8  customer_id;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/8
query.sql
1UPDATE customer_order_totals2SET3  total = 0
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/3
3OutputInspect the representative rows, affected-row count, plan, or command status.
Expected view update error
CREATE VIEW ERROR: cannot update view "customer_order_totals" DETAIL: Views containing GROUP BY are not automatically updatable.

Note: PostgreSQL-style diagnostic; other databases may use different wording for the same non-updatable aggregate view.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Running

fails, because a total doesn't map to one row of orders.

Key takeaway: One table, no aggregation or set operations, plain columns: updatable.

CHECK OPTION: LOCAL and CASCADED

Without CHECK OPTION, INSERT INTO eu_customers (name, email, region) VALUES ('Ana', 'ana@example.com', 'US') succeeds and the new row is immediately invisible through the view. With CHECK OPTION, the database tests each new or updated row against the view's WHERE clause and rejects violations; DELETE is never checked, since a deleted row isn't written anywhere.

For views built on views, WITH CASCADED CHECK OPTION (the default when you just write WITH CHECK OPTION) checks this view's conditions and those of every view underneath; WITH LOCAL CHECK OPTION checks only conditions defined in this view, plus any underlying views that have their own check option. CHECK OPTION is about data scope, not permissions: combine it with GRANTs on the view and no direct access to the base table.

Layered viewsQuery example
1Input dataStart from the source rows or state that the statement sees.
customers
3 rows
customers sample rows
idnameemailcity
1Mayamaya@example.comPune
2Raviravi@example.comDelhi
3Noornoor@example.comMumbai
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1CREATE VIEW active_customers AS2SELECT3  *4FROM5  customers6WHERE7  status = 'active';89CREATE VIEW active_eu AS10SELECT11  *12FROM13  active_customers14WHERE15  region = 'EU'16WITH17  CASCADED CHECK OPTION;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/17
3OutputInspect the representative rows, affected-row count, plan, or command status.
Expected CHECK OPTION failure
CREATE VIEW CREATE VIEW ERROR: new row violates check option for view "active_eu"

Note: Assumes the inserted row has region EU but status pending. CASCADED CHECK OPTION checks active_eu and the underlying active_customers predicate.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Inserting a pending EU customer through active_eu fails, because CASCADED also checks status = 'active'.

Key takeaway: CHECK OPTION enforces the WHERE on writes; CASCADED includes underlying views.

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Design

Keep the view simple

Select from one table with a WHERE clause and plain column references so it's automatically updatable.

SQL at this step
1SELECT2  id,3  name,4  email,5  region6FROM7  customers8WHERE9  region = 'EU'
What to watch at this step
One
base table
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

A simple view over one table is updatable: INSERT, UPDATE and DELETE on the view are passed through to the base table. Without protection, you can update a row so it no longer matches the view's WHERE clause, making it vanish from the view, or insert rows the view can't show. WITH CHECK OPTION stops that: any row written through the view must satisfy the view's conditions, or the statement fails.

Why it matters: Views are often used to give a team or service a restricted slice of a table, such as active customers only or one region's orders. If that slice is writable, CHECK OPTION is what turns the view's WHERE clause from a filter into a rule, so a regional team can't move orders to another region by mistake. Knowing which views are updatable also explains confusing "cannot update view" errors.

When to reach for it

  • Giving a team write access to their region or status only
  • Keeping legacy column names via a view during a migration
  • Hiding sensitive columns while allowing edits to others

When to avoid or be careful

  • Writable restricted views without CHECK OPTION
  • Expecting views with GROUP BY or DISTINCT to be updatable
  • Granting access to both the view and its base table

Ways to write through a restricted slice

Simple view + WITH CHECK OPTION
Best for: Row and column subsets of one table

Strengths: No code; the database enforces the scope.

Watch out: Only works for simple, single-table views.

INSTEAD OF triggers
Best for: Writable views over joins

Strengths: Any logic you need.

Watch out: Trigger code to write, test and maintain.

Row-level security policies
Best for: Per-user or per-tenant access on the table itself

Strengths: Applies to every query path.

Watch out: More complex to set up and test.

Interview signals & numbers to know

When this topic comes up

  • “Let the EU team edit only EU customers”
  • "cannot update view" errors
  • “Rows disappear after updating through a view”
  • Views for access control
  • INSTEAD OF triggers

Benchmarks & rules of thumb

1 -
base table for an automatically updatable view
CASCADED -
the default CHECK OPTION mode
PostgreSQL 9.3 -
automatically updatable views

Common mistakes & follow-ups

Common interview mistakes

  • Creating a filtered view for a team without CHECK OPTION, so their inserts for other regions succeed and vanish.
  • Adding GROUP BY to a view the application writes to, breaking every update.
  • Inserting through a view that hides a NOT NULL column with no default.

Likely interviewer follow-ups

  • What makes a view updatable?
  • What does WITH CHECK OPTION prevent?
  • How would you make a view over a join writable?
Interview tip: Demonstrate with one UPDATE that moves a row out of the view: it succeeds without CHECK OPTION and fails with it. That one example explains the whole feature.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Updatable Views & WITH CHECK OPTION with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Views: Creating Virtual Tables for Security & Simplicity
Next topic
Materialized Views & Concurrent Refresh Strategies