Bird
Raised Fist0
SQLquery~10 mins

Dropping and altering views in SQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Dropping and altering views
Start
Check if view exists
Yes
Drop or Alter view
Confirm changes
End
This flow shows checking for a view, then dropping or altering it, and confirming the change.
Execution Sample
SQL
DROP VIEW IF EXISTS employee_view;

CREATE OR REPLACE VIEW employee_view AS
SELECT id, name, department FROM employees;
This code drops the view if it exists, then creates or replaces it with a new definition.
Execution Table
StepActionCondition/EvaluationResult/Output
1Check if view 'employee_view' existsView existsProceed to drop view
2Execute DROP VIEW IF EXISTS employee_view;Drop command runsView 'employee_view' dropped
3Execute CREATE OR REPLACE VIEW employee_view AS ...Create or replace viewView 'employee_view' created with new definition
4Query SELECT * FROM employee_view;Select from viewReturns id, name, department columns from employees table
5EndNo more commandsProcess complete
💡 All commands executed; view dropped and recreated successfully
Variable Tracker
VariableStartAfter Step 2After Step 3Final
employee_viewExists with old definitionDropped (does not exist)Created with new definitionExists with new definition
Key Moments - 2 Insights
Why do we use DROP VIEW IF EXISTS instead of just DROP VIEW?
Using DROP VIEW IF EXISTS avoids errors if the view does not exist, as shown in step 1 and 2 of the execution_table.
What happens if we use CREATE OR REPLACE VIEW on a view that does not exist?
CREATE OR REPLACE VIEW creates the view if it does not exist, or replaces it if it does, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'employee_view' after step 2?
AView exists with old definition
BView created with new definition
CView dropped (does not exist)
DView altered but still exists
💡 Hint
Check the 'Result/Output' column in step 2 of execution_table
At which step does the view get created or replaced with the new definition?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result/Output' columns in execution_table
If we remove IF EXISTS from the DROP VIEW command and the view does not exist, what would happen?
AAn error occurs stopping execution
BThe view is created instead
CThe command runs successfully anyway
DNothing happens, silently ignored
💡 Hint
Consider the purpose of IF EXISTS in step 1 and 2 of execution_table
Concept Snapshot
DROP VIEW IF EXISTS view_name;
-- Safely drops a view if it exists

CREATE OR REPLACE VIEW view_name AS
SELECT ...;
-- Creates or updates a view definition

Use DROP VIEW IF EXISTS to avoid errors if view missing.
CREATE OR REPLACE VIEW updates or creates the view.
Full Transcript
This lesson shows how to drop and alter views in SQL. First, we check if the view exists. If it does, we drop it using DROP VIEW IF EXISTS to avoid errors if it doesn't exist. Then, we create or replace the view with a new definition using CREATE OR REPLACE VIEW. This ensures the view is updated or created fresh. The execution table traces these steps, showing the view's state changes. Key points include using IF EXISTS to prevent errors and understanding that CREATE OR REPLACE VIEW works whether the view exists or not.

Practice

(1/5)
1. What does the SQL command DROP VIEW view_name; do?
easy
A. It updates the data inside the view view_name.
B. It renames the view view_name to another name.
C. It creates a new view called view_name.
D. It deletes the view named view_name from the database.

Solution

  1. Step 1: Understand the purpose of DROP VIEW

    The command DROP VIEW is used to remove a view from the database completely.
  2. Step 2: Analyze the command effect

    Using DROP VIEW view_name; deletes the view named view_name, so it no longer exists.
  3. Final Answer:

    It deletes the view named view_name from the database. -> Option D
  4. Quick Check:

    DROP VIEW removes a view [OK]
Hint: DROP VIEW deletes a view completely from the database [OK]
Common Mistakes:
  • Thinking DROP VIEW updates data inside the view
  • Confusing DROP VIEW with CREATE VIEW
  • Assuming DROP VIEW renames the view
2. Which of the following is the correct syntax to change the query inside an existing view named employee_view?
easy
A. MODIFY VIEW employee_view SELECT * FROM employees;
B. UPDATE VIEW employee_view SET query = 'SELECT * FROM employees';
C. ALTER VIEW employee_view AS SELECT * FROM employees WHERE active = 1;
D. CHANGE VIEW employee_view TO SELECT * FROM employees;

Solution

  1. Step 1: Recall ALTER VIEW syntax

    The correct syntax to change a view's query is ALTER VIEW view_name AS SELECT ....
  2. Step 2: Check each option

    ALTER VIEW employee_view AS SELECT * FROM employees WHERE active = 1; uses the correct syntax. Options B, C, and D use invalid or non-existent commands.
  3. Final Answer:

    ALTER VIEW employee_view AS SELECT * FROM employees WHERE active = 1; -> Option C
  4. Quick Check:

    ALTER VIEW uses AS SELECT [OK]
Hint: Use ALTER VIEW view_name AS SELECT ... to update a view [OK]
Common Mistakes:
  • Using UPDATE or MODIFY instead of ALTER
  • Omitting AS keyword
  • Trying to rename view with ALTER VIEW
3. Given a view active_customers defined as SELECT * FROM customers WHERE status = 'active', what will be the result after running ALTER VIEW active_customers AS SELECT * FROM customers WHERE status = 'inactive'; and then querying SELECT * FROM active_customers;?
medium
A. It will return all customers with status 'inactive'.
B. It will cause a syntax error.
C. It will return all customers regardless of status.
D. It will return all customers with status 'active'.

Solution

  1. Step 1: Understand ALTER VIEW effect

    The ALTER VIEW command changes the query inside the view to the new SELECT statement.
  2. Step 2: Analyze the new query

    The view now selects customers where status = 'inactive'. So querying the view returns inactive customers.
  3. Final Answer:

    It will return all customers with status 'inactive'. -> Option A
  4. Quick Check:

    ALTER VIEW changes the view's query [OK]
Hint: ALTER VIEW changes the query; output matches new SELECT [OK]
Common Mistakes:
  • Assuming view still returns old data after ALTER
  • Thinking ALTER VIEW causes errors if view exists
  • Confusing view data with table data
4. You try to run ALTER VIEW sales_view AS SELECT * FROM sales WHERE amount > 1000; but get an error. What is the most likely cause?
medium
A. The view sales_view does not exist.
B. The SELECT query inside ALTER VIEW is missing a semicolon.
C. You cannot use WHERE clauses inside ALTER VIEW.
D. ALTER VIEW requires DROP VIEW before use.

Solution

  1. Step 1: Check ALTER VIEW prerequisites

    ALTER VIEW requires the view to already exist to modify it.
  2. Step 2: Identify common error cause

    If the view sales_view does not exist, ALTER VIEW will fail with an error.
  3. Final Answer:

    The view sales_view does not exist. -> Option A
  4. Quick Check:

    ALTER VIEW fails if view missing [OK]
Hint: Ensure view exists before ALTER VIEW [OK]
Common Mistakes:
  • Thinking WHERE clause is invalid in ALTER VIEW
  • Assuming semicolon inside ALTER VIEW causes error
  • Believing DROP VIEW is needed before ALTER VIEW
5. You have a view product_summary showing product names and total sales. You want to update it to include only products with sales over 500. Which sequence of commands correctly updates the view without losing it?
hard
A. DROP VIEW product_summary; CREATE VIEW product_summary AS SELECT name, SUM(sales) FROM products GROUP BY name HAVING SUM(sales) > 500;
B. ALTER VIEW product_summary AS SELECT name, SUM(sales) FROM products GROUP BY name HAVING SUM(sales) > 500;
C. UPDATE VIEW product_summary SET query = 'SELECT name, SUM(sales) FROM products GROUP BY name HAVING SUM(sales) > 500;';
D. REPLACE VIEW product_summary AS SELECT name, SUM(sales) FROM products GROUP BY name HAVING SUM(sales) > 500;

Solution

  1. Step 1: Understand how to update a view's query

    ALTER VIEW updates the query inside an existing view without dropping it.
  2. Step 2: Check each option's correctness

    ALTER VIEW product_summary AS SELECT name, SUM(sales) FROM products GROUP BY name HAVING SUM(sales) > 500; uses ALTER VIEW with the correct query. DROP VIEW product_summary; CREATE VIEW product_summary AS SELECT name, SUM(sales) FROM products GROUP BY name HAVING SUM(sales) > 500; drops and recreates the view, which is not needed. Options B and D use invalid commands.
  3. Final Answer:

    ALTER VIEW product_summary AS SELECT name, SUM(sales) FROM products GROUP BY name HAVING SUM(sales) > 500; -> Option B
  4. Quick Check:

    Use ALTER VIEW to update view query safely [OK]
Hint: Use ALTER VIEW to update query without dropping [OK]
Common Mistakes:
  • Dropping view unnecessarily before updating
  • Using UPDATE VIEW or REPLACE VIEW which are invalid
  • Not including HAVING clause in the new query