0
0
PostgreSQLquery~20 mins

Updatable views in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Updatable Views Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of UPDATE on a simple updatable view

Consider a table employees(id, name, salary) and a view high_earners defined as SELECT * FROM employees WHERE salary > 50000. What will be the result of this query?

UPDATE high_earners SET salary = salary + 5000 WHERE id = 3 RETURNING *;
PostgreSQL
CREATE TABLE employees (id INT PRIMARY KEY, name TEXT, salary INT);
INSERT INTO employees VALUES (1, 'Alice', 60000), (2, 'Bob', 45000), (3, 'Carol', 70000);
CREATE VIEW high_earners AS SELECT * FROM employees WHERE salary > 50000;
AReturns the updated row for id=3 with salary increased to 75000
BReturns no rows because id=3 is not in the view
CRaises an error because views cannot be updated
DUpdates all rows in employees, not just id=3
Attempts:
2 left
💡 Hint

Think about whether the view filters rows and if the updated row still meets the filter.

🧠 Conceptual
intermediate
1:30remaining
Which views are updatable in PostgreSQL?

Which of the following views is not updatable by default in PostgreSQL?

AA view selecting all columns from a single table with no WHERE clause
BA view selecting columns with a WHERE filter from one table
CA view selecting all columns from a single table without aggregation
DA view that uses a JOIN between two tables
Attempts:
2 left
💡 Hint

Think about what makes a view simple enough to be updatable.

📝 Syntax
advanced
2:30remaining
Which statement creates an updatable view with an INSTEAD OF trigger?

Given a view v_emp_dept joining employees and departments, which option correctly creates an INSTEAD OF trigger to allow updates on the view?

ACREATE TRIGGER trg_update INSTEAD OF UPDATE ON v_emp_dept FOR EACH ROW EXECUTE FUNCTION update_emp_dept();
BCREATE TRIGGER trg_update AFTER UPDATE ON v_emp_dept FOR EACH ROW EXECUTE PROCEDURE update_emp_dept();
CCREATE TRIGGER trg_update INSTEAD OF UPDATE ON employees FOR EACH ROW EXECUTE FUNCTION update_emp_dept();
DCREATE TRIGGER trg_update BEFORE UPDATE ON v_emp_dept FOR EACH ROW EXECUTE FUNCTION update_emp_dept();
Attempts:
2 left
💡 Hint

INSTEAD OF triggers must be created on views, not tables, and use EXECUTE FUNCTION syntax in PostgreSQL 12+.

optimization
advanced
2:00remaining
Improving performance of updates on an updatable view

You have an updatable view based on a large table with a WHERE clause. Updates on the view are slow. Which approach can improve update performance?

ACreate a materialized view instead of a regular view
BRewrite the view to use a JOIN with another large table
CAdd an index on the columns used in the WHERE clause of the view
DRemove the WHERE clause from the view to include all rows
Attempts:
2 left
💡 Hint

Think about how indexes help queries and updates.

🔧 Debug
expert
3:00remaining
Why does this UPDATE on a view raise an error?

Given the view definition:

CREATE VIEW v_emp AS SELECT id, name, salary, salary * 1.1 AS adjusted_salary FROM employees;

Why does this query raise an error?

UPDATE v_emp SET adjusted_salary = 80000 WHERE id = 2;
ABecause the employees table has no rows with id = 2
BBecause adjusted_salary is a computed column and cannot be updated
CBecause the UPDATE statement syntax is incorrect
DBecause the view does not include the primary key
Attempts:
2 left
💡 Hint

Consider which columns in a view can be updated.