Bird
Raised Fist0
MySQLquery~20 mins

Updatable views in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 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
Given a table Employees with columns id, name, and salary, and a view HighEarners defined as SELECT id, name, salary FROM Employees WHERE salary > 50000, what will be the result of this update?

UPDATE HighEarners SET salary = salary + 5000 WHERE id = 3;

Assume employee with id = 3 has a salary of 60000 before the update.
MySQL
CREATE TABLE Employees (id INT PRIMARY KEY, name VARCHAR(50), salary INT);
INSERT INTO Employees VALUES (1, 'Alice', 45000), (2, 'Bob', 52000), (3, 'Charlie', 60000);
CREATE VIEW HighEarners AS SELECT id, name, salary FROM Employees WHERE salary > 50000;
AThe salary of employee with id 3 remains 60000; no change occurs.
BThe update fails because views cannot be updated.
CThe update changes salary in HighEarners view only, Employees table remains unchanged.
DThe salary of employee with id 3 becomes 65000 in Employees and HighEarners view.
Attempts:
2 left
💡 Hint
Think about whether the view is updatable and if changes reflect in the base table.
🧠 Conceptual
intermediate
1:30remaining
Which views are not updatable?
Which of the following types of views in MySQL are generally not updatable?
AViews that include only columns from one table with no WHERE clause.
BViews that select directly from a single table without joins.
CViews that use aggregate functions like SUM or COUNT.
DViews that select all columns from a single table.
Attempts:
2 left
💡 Hint
Think about what happens when a view summarizes data.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in creating an updatable view
Which option contains a syntax error when creating an updatable view in MySQL?
MySQL
CREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active';
ACREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active' WITH CHECK OPTION;
BCREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active' WITH UPDATE OPTION;
CCREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active' WITH READ ONLY;
DCREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active';
Attempts:
2 left
💡 Hint
Check which clauses are valid in MySQL view definitions.
❓ optimization
advanced
2:00remaining
Optimizing updates through views with joins
Consider a view defined as:
CREATE VIEW EmpDept AS SELECT e.id, e.name, d.dept_name FROM Employees e JOIN Departments d ON e.dept_id = d.id;

Which statement about updating the EmpDept view is true?
AYou can update the employee's name through the view, but not the department name.
BYou cannot update any columns through this view because it uses a join.
CYou can update both employee name and department name through the view.
DYou can update the department name but not the employee name through the view.
Attempts:
2 left
💡 Hint
Think about which table columns the view maps to and MySQL's rules for updatable views with joins.
🔧 Debug
expert
2:30remaining
Why does this update on a view fail?
Given the view:
CREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date >= '2024-01-01' WITH CHECK OPTION;

Attempting to run:
UPDATE RecentOrders SET order_date = '2023-12-31' WHERE order_id = 101;

causes an error. What is the most likely reason?
AThe update violates the view's WHERE condition, so MySQL rejects it.
BViews with WHERE clauses are never updatable in MySQL.
CThe order_date column is read-only in all views.
DThe order_id column is not a primary key, so update fails.
Attempts:
2 left
💡 Hint
Consider what happens if the update changes a row so it no longer meets the view's filter.

Practice

(1/5)
1. What is an updatable view in MySQL?
easy
A. A view that only shows data but does not allow any changes.
B. A view that allows you to change the data in the underlying table through it.
C. A view that automatically creates new tables.
D. A view that stores data separately from the original table.

Solution

  1. Step 1: Understand what a view is

    A view is a saved query that shows data from one or more tables.
  2. Step 2: Define updatable view

    An updatable view lets you change data in the underlying table through the view itself.
  3. Final Answer:

    A view that allows you to change the data in the underlying table through it. -> Option B
  4. Quick Check:

    Updatable view = allows data change [OK]
Hint: Updatable views let you edit data through the view [OK]
Common Mistakes:
  • Thinking views store data separately
  • Confusing read-only views with updatable views
  • Assuming all views are updatable
2. Which of the following is the correct syntax to create an updatable view in MySQL?
easy
A. CREATE VIEW view_name UPDATEABLE AS SELECT * FROM table_name;
B. CREATE UPDATABLE VIEW view_name AS SELECT * FROM table_name;
C. CREATE VIEW view_name WITH UPDATE AS SELECT * FROM table_name;
D. CREATE VIEW view_name AS SELECT * FROM table_name;

Solution

  1. Step 1: Recall MySQL view creation syntax

    MySQL uses CREATE VIEW view_name AS SELECT ... to create views.
  2. Step 2: Understand updatable views syntax

    There is no special keyword for updatable views; they are updatable if the SELECT is simple enough.
  3. Final Answer:

    CREATE VIEW view_name AS SELECT * FROM table_name; -> Option D
  4. Quick Check:

    Standard CREATE VIEW syntax = CREATE VIEW view_name AS SELECT * FROM table_name; [OK]
Hint: No special keyword needed; simple CREATE VIEW works [OK]
Common Mistakes:
  • Adding non-existent keywords like UPDATABLE or WITH UPDATE
  • Using wrong syntax keywords
  • Assuming updatable views need special creation syntax
3. Given the table employees(id, name, salary) and the view:
CREATE VIEW emp_view AS SELECT id, name FROM employees;
Which of the following statements will succeed?
medium
A. UPDATE emp_view SET name = 'Alice' WHERE id = 1;
B. INSERT INTO emp_view (id, name) VALUES (4, 'Bob');
C. DELETE FROM emp_view WHERE salary = 2000;
D. UPDATE emp_view SET salary = 5000 WHERE id = 3;

Solution

  1. Step 1: Check view columns and underlying table

    The view selects only id and name, excluding salary.
  2. Step 2: Determine which operations are allowed

    Updating name through the view is allowed because it maps directly to the table column.
    INSERT fails (missing salary, assuming NOT NULL no default).
    DELETE referencing salary fails because salary not in view.
    Updating salary fails because salary not in view.
  3. Final Answer:

    UPDATE emp_view SET name = 'Alice' WHERE id = 1; -> Option A
  4. Quick Check:

    Update on view columns allowed = UPDATE emp_view SET name = 'Alice' WHERE id = 1; [OK]
Hint: You can update only columns included in the view [OK]
Common Mistakes:
  • Trying to update columns not in the view
  • Assuming inserts/deletes always work on views
  • Ignoring missing columns in the view
4. You created a view with:
CREATE VIEW v AS SELECT id, name FROM employees WHERE salary > 3000;
Why does the statement UPDATE v SET salary = 5000 WHERE id = 5; fail?
medium
A. Because the view does not include the salary column.
B. Because the view has a WHERE clause, making it non-updatable.
C. Because you cannot update views with more than one column.
D. Because the view name is invalid.

Solution

  1. Step 1: Check the columns in the view and the UPDATE target

    The view selects only id and name; salary is excluded.
  2. Step 2: Check other options

    A WHERE clause does not prevent updates to included columns.
    Views can have multiple columns and be updatable.
    The view name is valid.
  3. Final Answer:

    Because the view does not include the salary column. -> Option A
  4. Quick Check:

    Cannot update column not in view [OK]
Hint: Cannot modify columns absent from the view [OK]
Common Mistakes:
  • Thinking WHERE clause blocks updates
  • Assuming multiple columns prevent updates
  • Ignoring missing columns in the view
5. You want to create an updatable view on the orders table that shows only order_id and customer_id. You also want to allow updates to customer_id through the view. Which of the following is the best approach?
hard
A. CREATE VIEW order_view AS SELECT order_id FROM orders; Then update customer_id through order_view.
B. CREATE VIEW order_view AS SELECT order_id, customer_id FROM orders WHERE order_date > '2023-01-01'; Then update customer_id through order_view.
C. CREATE VIEW order_view AS SELECT order_id, customer_id FROM orders; Then update customer_id through order_view.
D. CREATE VIEW order_view AS SELECT order_id, customer_id, total FROM orders; Then update customer_id through order_view.

Solution

  1. Step 1: Identify columns needed in the view

    The view must include customer_id to allow updates on it.
  2. Step 2: Avoid issues that limit updatability or scope

    Adding a WHERE clause (as in B) limits updates to only recent orders.
    Including extra columns (as in C) shows more than requested.
    Excluding customer_id (as in A) prevents updating it.
    D provides exactly the requested columns without restrictions.
  3. Final Answer:

    CREATE VIEW order_view AS SELECT order_id, customer_id FROM orders; Then update customer_id through order_view. -> Option C
  4. Quick Check:

    Simple view with needed columns = updatable [OK]
Hint: Include columns to update, avoid WHERE filters [OK]
Common Mistakes:
  • Adding WHERE filters that limit updates
  • Excluding columns you want to update
  • Adding unnecessary columns that confuse
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