SQL Views
SQL CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
You can add SQL statements and functions to a view and present the data as if the data were coming from one single table.
A view is created with the The CREATE VIEW statement.
CREATE VIEW Syntax
Creates an index on a table. Duplicate values are allowed:
Syntax
CREATE VIEW view_name AS
SELECT column1, column2, ....
FROM table_name
WHERE condition;
SQL CREATE VIEW Examples
The following SQL creates a view that shows all employee from India:
Example
CREATE VIEW [India employee] AS
SELECT Employee_name, Address
FROM employee
WHERE Country = 'India';
Example
SELECT *FROM [India employee];
Example
CREATE VIEW [products Above Average Price ] AS
SELECT Product_name, Price
FROM products
WHERE Price > ( SELECT AVG(Price)
FROM products) ;
Example
SELECT *FROM [products Above Average Price];
SQL Updating a View
A view can be updated with theCREATE OR REPLACE VIEW statement.
Syntax
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ....
FROM table_name
WHERE condition;
Example
CREATE OR REPLACE VIEW [India employee] AS
SELECT Employee_name, Address
FROM employee
WHERE Country = 'India';