SQL Tutorial SQL Advanced SQL Database SQL References

SQL CREATE OR ALTER VIEW Keyword



The SQL CREATE OR ALTER VIEW statement is used to update a SQL VIEW. A SQL VIEW is a virtual table created based on the SQL statement. A view contains rows and columns just like a normal table. All SQL functions, WHERE, HAVING and JOINs statements can be used to create a SQL VIEW.

Syntax

The syntax for using CREATE OR ALTER VIEW statement is given below:

CREATE OR ALTER VIEW view_name AS
SELECT column1, column2, column3, ...
FROM table_name
WHERE condition(s);

Example:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

The below mentioned SQL statement is used to create a view on Employee table which contains all records of employee whose salary is greater than 2800.

CREATE VIEW Employee_Salary_GT_2800 AS
SELECT * FROM Employee WHERE Salary > 2800;

After creating the VIEW, it can be used as mentioned below:

SELECT * FROM Employee_Salary_GT_2800;

This will produce the result as shown below:

EmpIDNameCityAgeSalary
1JohnLondon253000
4KimAmsterdam303100
5RameshNew Delhi283000

Here, the CREATE OR ALTER VIEW statement can be used to update the above view. The mentioned below statement is used to update the above view which contains all records of employee whose salary is greater than 2800 and age is greater than 25.

Note: CREATE OR ALTER VIEW clause is not supported in all database. For example MySQL and Oracle supports the CREATE OR REPLACE VIEW clause for the same purpose.

/* SQL Server */
CREATE OR ALTER VIEW Employee_Salary_GT_2800 AS
SELECT * FROM Employee WHERE Salary > 2800 AND AGE > 25;

/* MySQL / Oracle */
CREATE OR ALTER VIEW Employee_Salary_GT_2800 AS
SELECT * FROM Employee WHERE Salary > 2800 AND AGE > 25;

After updating the VIEW, the following query can be used to see its content:

SELECT * FROM Employee_Salary_GT_2800;

This will produce the result as shown below:

EmpIDNameCityAgeSalary
4KimAmsterdam303100
5RameshNew Delhi283000

❮ SQL Keywords