Bird
Raised Fist0
PostgreSQLquery~20 mins

Views with CHECK OPTION in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
View Mastery with CHECK OPTION
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of INSERT on a view with CHECK OPTION

Consider the following table and view in PostgreSQL:

CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, department TEXT);
INSERT INTO employees (name, department) VALUES ('Alice', 'Sales'), ('Bob', 'HR');

CREATE VIEW sales_employees AS
  SELECT * FROM employees WHERE department = 'Sales'
  WITH CHECK OPTION;

What happens when you run this query?

INSERT INTO sales_employees (name, department) VALUES ('Charlie', 'HR');
PostgreSQL
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, department TEXT);
INSERT INTO employees (name, department) VALUES ('Alice', 'Sales'), ('Bob', 'HR');

CREATE VIEW sales_employees AS
  SELECT * FROM employees WHERE department = 'Sales'
  WITH CHECK OPTION;

INSERT INTO sales_employees (name, department) VALUES ('Charlie', 'HR');
AThe insert succeeds and adds Charlie to employees with department 'HR'.
BThe insert succeeds but Charlie's department is automatically changed to 'Sales'.
CThe insert succeeds but Charlie is not visible in the sales_employees view.
DThe insert fails with a CHECK OPTION violation error.
Attempts:
2 left
💡 Hint

Think about what the CHECK OPTION enforces on inserts through the view.

❓ query_result
intermediate
2:00remaining
Update behavior on a view with CHECK OPTION

Given the same employees table and sales_employees view as before, what is the result of this update?

UPDATE sales_employees SET department = 'HR' WHERE name = 'Alice';
PostgreSQL
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, department TEXT);
INSERT INTO employees (name, department) VALUES ('Alice', 'Sales'), ('Bob', 'HR');

CREATE VIEW sales_employees AS
  SELECT * FROM employees WHERE department = 'Sales'
  WITH CHECK OPTION;

UPDATE sales_employees SET department = 'HR' WHERE name = 'Alice';
AThe update fails with a CHECK OPTION violation error.
BThe update succeeds and Alice's department changes to 'HR'.
CThe update succeeds but Alice remains visible in the view with department 'Sales'.
DThe update succeeds but Alice is deleted from the employees table.
Attempts:
2 left
💡 Hint

Consider what the CHECK OPTION enforces on updates through the view.

🧠 Conceptual
advanced
1:30remaining
Purpose of WITH CHECK OPTION in views

What is the main purpose of using WITH CHECK OPTION when creating a view in PostgreSQL?

ATo allow the view to be updated even if it contains aggregate functions.
BTo automatically create indexes on the view for faster queries.
CTo ensure that any data inserted or updated through the view satisfies the view's defining condition.
DTo restrict SELECT queries on the view to only certain users.
Attempts:
2 left
💡 Hint

Think about data integrity when modifying data through views.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in view creation

Which of the following CREATE VIEW statements will cause a syntax error in PostgreSQL?

ACREATE VIEW v AS SELECT * FROM employees WHERE department = 'Sales' CHECK OPTION;
BCREATE VIEW v AS SELECT * FROM employees WHERE department = 'Sales' WITH LOCAL CHECK OPTION;
CCREATE VIEW v AS SELECT * FROM employees WHERE department = 'Sales' WITH CASCADED CHECK OPTION;
DCREATE VIEW v AS SELECT * FROM employees WHERE department = 'Sales' WITH CHECK OPTION;
Attempts:
2 left
💡 Hint

Check the correct syntax for adding CHECK OPTION to a view.

🔧 Debug
expert
2:30remaining
Diagnose why an update through a view with CHECK OPTION fails

You have this setup:

CREATE TABLE products (id SERIAL PRIMARY KEY, name TEXT, price NUMERIC);
INSERT INTO products (name, price) VALUES ('Pen', 1.5), ('Notebook', 3.0);

CREATE VIEW expensive_products AS
  SELECT * FROM products WHERE price > 2
  WITH CHECK OPTION;

When you run this update, it fails:

UPDATE expensive_products SET price = 1.0 WHERE name = 'Notebook';

Why does this update fail?

PostgreSQL
CREATE TABLE products (id SERIAL PRIMARY KEY, name TEXT, price NUMERIC);
INSERT INTO products (name, price) VALUES ('Pen', 1.5), ('Notebook', 3.0);

CREATE VIEW expensive_products AS
  SELECT * FROM products WHERE price > 2
  WITH CHECK OPTION;

UPDATE expensive_products SET price = 1.0 WHERE name = 'Notebook';
ABecause the view does not allow updates on the price column.
BBecause the updated price 1.0 does not satisfy the view's WHERE condition price > 2, violating the CHECK OPTION.
CBecause the products table has a trigger preventing price changes below 2.
DBecause the UPDATE statement syntax is incorrect for views with CHECK OPTION.
Attempts:
2 left
💡 Hint

Consider the view's filter condition and how CHECK OPTION enforces it on updates.

Practice

(1/5)
1. What is the main purpose of using WITH CHECK OPTION in a PostgreSQL view?
easy
A. To prevent inserts or updates that violate the view's filter conditions
B. To speed up query execution on the view
C. To allow views to be updated without restrictions
D. To create a temporary table instead of a view

Solution

  1. Step 1: Understand the role of WITH CHECK OPTION

    This option ensures that any data inserted or updated through the view must satisfy the view's filter conditions.
  2. Step 2: Identify what it prevents

    It prevents changes that would cause rows to fall outside the view's defined criteria, keeping data consistent.
  3. Final Answer:

    To prevent inserts or updates that violate the view's filter conditions -> Option A
  4. Quick Check:

    WITH CHECK OPTION blocks invalid changes = A [OK]
Hint: Check option stops changes outside view filters [OK]
Common Mistakes:
  • Thinking it speeds up queries
  • Believing it allows unrestricted updates
  • Confusing it with temporary tables
2. Which of the following is the correct syntax to create a view with a WITH CHECK OPTION in PostgreSQL?
easy
A. CREATE VIEW v AS SELECT * FROM t WHERE x > 10, WITH CHECK OPTION;
B. CREATE VIEW v AS SELECT * FROM t WHERE x > 10 CHECK OPTION WITH;
C. CREATE VIEW v AS SELECT * FROM t WHERE x > 10 WITH CHECK OPTION;
D. CREATE VIEW v AS SELECT * FROM t WHERE x > 10 OPTION CHECK WITH;

Solution

  1. Step 1: Recall PostgreSQL view creation syntax

    The correct syntax places WITH CHECK OPTION at the end of the CREATE VIEW statement.
  2. Step 2: Match the exact phrase and order

    CREATE VIEW v AS SELECT * FROM t WHERE x > 10 WITH CHECK OPTION; matches the correct syntax: CREATE VIEW v AS SELECT ... WITH CHECK OPTION;
  3. Final Answer:

    CREATE VIEW v AS SELECT * FROM t WHERE x > 10 WITH CHECK OPTION; -> Option C
  4. Quick Check:

    Correct syntax ends with WITH CHECK OPTION [OK]
Hint: WITH CHECK OPTION always goes at the end of CREATE VIEW [OK]
Common Mistakes:
  • Swapping words order
  • Adding semicolons inside SELECT
  • Using incorrect keywords order
3. Given the table employees(id, name, salary) and the view:
CREATE VIEW high_salary AS SELECT * FROM employees WHERE salary > 5000 WITH CHECK OPTION;
What happens if you try to insert (10, 'Anna', 4000) into high_salary?
medium
A. The insert fails due to the CHECK OPTION
B. The insert succeeds but the row is not visible in the view
C. The insert succeeds and adds the row
D. The insert causes a syntax error

Solution

  1. Step 1: Understand the view filter and CHECK OPTION

    The view only shows employees with salary > 5000, and WITH CHECK OPTION prevents inserts that don't meet this.
  2. Step 2: Analyze the insert data

    The salary 4000 is less than 5000, so the insert violates the view's filter and is blocked.
  3. Final Answer:

    The insert fails due to the CHECK OPTION -> Option A
  4. Quick Check:

    Insert violating view filter blocked = C [OK]
Hint: Insert must meet view filter or it fails [OK]
Common Mistakes:
  • Thinking insert always succeeds
  • Assuming row is added but hidden
  • Confusing syntax error with runtime error
4. You have this view:
CREATE VIEW active_users AS SELECT * FROM users WHERE active = true WITH CHECK OPTION;
You try to update a row through the view to set active = false. What is the result?
medium
A. The update succeeds and changes the row
B. The update causes a syntax error
C. The update succeeds but the row disappears from the view
D. The update fails because it violates the CHECK OPTION

Solution

  1. Step 1: Understand the view filter and update

    The view only shows rows where active = true. WITH CHECK OPTION blocks updates that break this condition.
  2. Step 2: Analyze the attempted update

    Setting active = false violates the view filter, so the update is rejected.
  3. Final Answer:

    The update fails because it violates the CHECK OPTION -> Option D
  4. Quick Check:

    Update violating view filter blocked = B [OK]
Hint: Updates must keep rows inside view filter [OK]
Common Mistakes:
  • Assuming update succeeds but hides row
  • Confusing syntax error with constraint error
  • Ignoring CHECK OPTION effect on updates
5. Consider a view CREATE VIEW vip_customers AS SELECT * FROM customers WHERE vip = true WITH CHECK OPTION;
You want to allow inserting new customers through this view only if they are VIPs. Which of the following statements correctly inserts a VIP customer and rejects a non-VIP customer?
hard
A. INSERT INTO vip_customers (id, name, vip) VALUES (1, 'John', false); -- fails
INSERT INTO vip_customers (id, name, vip) VALUES (2, 'Jane', false); -- fails
B. INSERT INTO vip_customers (id, name, vip) VALUES (1, 'John', true); -- succeeds
INSERT INTO vip_customers (id, name, vip) VALUES (2, 'Jane', false); -- fails
C. INSERT INTO vip_customers (id, name, vip) VALUES (1, 'John', true); -- fails
INSERT INTO vip_customers (id, name, vip) VALUES (2, 'Jane', false); -- succeeds
D. INSERT INTO vip_customers (id, name, vip) VALUES (1, 'John', false); -- succeeds
INSERT INTO vip_customers (id, name, vip) VALUES (2, 'Jane', true); -- fails

Solution

  1. Step 1: Understand the view filter and CHECK OPTION

    The view only allows rows where vip = true. WITH CHECK OPTION blocks inserts with vip = false.
  2. Step 2: Analyze each insert statement

    Inserting John with vip = true succeeds. Inserting Jane with vip = false fails due to CHECK OPTION.
  3. Final Answer:

    Only inserts with vip = true succeed; others fail -> Option B
  4. Quick Check:

    Insert vip=true allowed, vip=false blocked = D [OK]
Hint: Only insert rows matching view filter pass CHECK OPTION [OK]
Common Mistakes:
  • Trying to insert non-VIP through VIP view
  • Assuming all inserts succeed
  • Confusing which values pass CHECK OPTION
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard