Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

Altering a View

Learn how to modify an existing SQL Server View using the ALTER VIEW statement with practical examples.

What is ALTER VIEW?

The ALTER VIEW statement is used to modify the definition of an existing SQL Server View.

When the structure or SELECT query of a View needs to be changed, we can use ALTER VIEW instead of dropping the View and creating it again.

The ALTER VIEW statement changes the View definition while keeping the View object in the database.

ALTER VIEW Syntax

ALTER VIEW ViewName
AS
SELECT column1, column2
FROM TableName
WHERE condition;
Syntax Explanation
  • ALTER VIEW specifies that an existing View will be modified.
  • ViewName is the name of the existing View.
  • AS separates the View name from the SELECT query.
  • The SELECT statement defines the new View definition.

Step 1: Create an Existing View

First, suppose we already have a View named EmployeeDetailsView.

CREATE VIEW EmployeeDetailsView
AS
SELECT
    EmployeeId,
    EmployeeName,
    Department
FROM Employees;

This View currently displays EmployeeId, EmployeeName, and Department.

Checking the Existing View

We can retrieve the data from the View using SELECT:

SELECT *
FROM EmployeeDetailsView;

Example 1: Adding a Column

Suppose we now want to include the employee's salary in the View.

We can modify the existing View using ALTER VIEW:

ALTER VIEW EmployeeDetailsView
AS
SELECT
    EmployeeId,
    EmployeeName,
    Department,
    Salary
FROM Employees;

The View now returns the Salary column along with the previously selected columns.

Example 2: Adding a WHERE Condition

We can also change the View so that it returns only employees from the IT department.

ALTER VIEW EmployeeDetailsView
AS
SELECT
    EmployeeId,
    EmployeeName,
    Department,
    Salary
FROM Employees
WHERE Department = 'IT';

After altering the View, only IT department employees will be returned.

Example 3: Changing the View Query

The SELECT query inside an existing View can be changed completely using ALTER VIEW.

ALTER VIEW EmployeeDetailsView
AS
SELECT
    EmployeeName,
    Salary
FROM Employees
WHERE Salary > 50000;

The View now displays only EmployeeName and Salary for employees whose salary is greater than 50000.

Verifying the Altered View

After using ALTER VIEW, execute the View to verify the new definition.

SELECT *
FROM EmployeeDetailsView;

The result will now be based on the new SELECT query defined by ALTER VIEW.

CREATE VIEW vs ALTER VIEW

CREATE VIEW ALTER VIEW
Creates a new View. Modifies an existing View.
Used when the View does not exist. Used when the View already exists.
Creates the View object. Changes the definition of the existing View object.

Important Points

  • ALTER VIEW is used for an existing View.
  • It changes the SELECT definition of the View.
  • The View name remains the same.
  • The underlying tables are not automatically changed just because the View definition is altered.
  • Always test the altered View after making changes.

Best Practices

  • Check the existing View definition before altering it.
  • Test the new SELECT query separately before using ALTER VIEW.
  • Make sure applications depending on the View are not affected by unexpected column changes.
  • Use meaningful and consistent View names.
  • Verify the View after altering it.

Summary

The ALTER VIEW statement is used to modify the definition of an existing SQL Server View. We can change the SELECT query, add or remove selected columns, and modify filtering conditions without dropping the View object first.