0
0
SQLquery~20 mins

Updatable views and limitations in SQL - 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 updating a simple updatable view

Consider a table Employees with columns id, name, and salary. A view EmpView is created as SELECT id, name, salary FROM Employees. What will be the result of this query after executing UPDATE EmpView SET salary = salary + 1000 WHERE id = 2;?

Assume the original Employees table data is:

id | name  | salary
---+-------+--------
1  | Alice | 5000
2  | Bob   | 6000
3  | Carol | 5500

What will be the salary of Bob after the update?

SQL
CREATE TABLE Employees (id INT PRIMARY KEY, name VARCHAR(50), salary INT);
INSERT INTO Employees VALUES (1, 'Alice', 5000), (2, 'Bob', 6000), (3, 'Carol', 5500);
CREATE VIEW EmpView AS SELECT id, name, salary FROM Employees;
UPDATE EmpView SET salary = salary + 1000 WHERE id = 2;
SELECT salary FROM Employees WHERE id = 2;
A7000
B6000
C6500
DError: View is not updatable
Attempts:
2 left
💡 Hint

Simple views that directly select from one table without aggregation are usually updatable.

🧠 Conceptual
intermediate
2:00remaining
Limitation on updating views with joins

Which of the following statements correctly describes a limitation when trying to update a view that is created by joining two tables?

AYou can only update columns from one of the base tables if the DBMS supports it, but not both simultaneously.
BYou cannot update any columns in a view created by joining tables.
CYou can update columns from both tables freely if the join is an inner join.
DYou can update any column from either table through the view without restrictions.
Attempts:
2 left
💡 Hint

Think about how the database knows which table to update when multiple tables are joined.

📝 Syntax
advanced
2:00remaining
Which view definition is NOT updatable?

Given the following view definitions, which one is NOT updatable according to standard SQL rules?

ACREATE VIEW V1 AS SELECT id, name FROM Employees WHERE salary > 5000;
BCREATE VIEW V2 AS SELECT id, name, salary * 1.1 AS increased_salary FROM Employees;
CCREATE VIEW V3 AS SELECT id, name FROM Employees;
DCREATE VIEW V4 AS SELECT id, name FROM Employees WHERE name LIKE 'A%';
Attempts:
2 left
💡 Hint

Consider if the view contains computed columns or expressions.

🔧 Debug
advanced
2:00remaining
Why does this update on a view fail?

Consider the view and update statement below:

CREATE VIEW DeptEmp AS
SELECT e.id, e.name, d.department_name
FROM Employees e JOIN Departments d ON e.department_id = d.id;

UPDATE DeptEmp SET department_name = 'Sales' WHERE id = 3;

The update fails with an error. Why?

ABecause the update statement syntax is incorrect.
BBecause the view does not include the primary key of Employees.
CBecause the view includes a join, the DBMS cannot determine which base table to update for the department_name column.
DBecause the department_name column is not updatable due to a missing WHERE clause.
Attempts:
2 left
💡 Hint

Think about how updates work on joined views.

🧠 Conceptual
expert
2:00remaining
Which condition prevents a view from being updatable?

Which of the following conditions will prevent a view from being updatable in most SQL database systems?

AThe view is created with a single table and no computed columns.
BThe view selects all columns from a single base table without any filters.
CThe view is created with a simple WHERE clause filtering rows.
DThe view contains aggregate functions like SUM or COUNT.
Attempts:
2 left
💡 Hint

Think about what aggregate functions do to rows.