bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/SQL/SQL Database
SQL•SQL Database

SQL Views

SQL CREATE VIEW Statement

An SQL view is a virtual table based on the result-set of an SQL statement. An SQL view contains rows and columns, just like a real table. The fields in the 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 it were coming from one single table.

A view is created with the CREATE VIEW statement.

CREATE VIEW Syntax

CREATE VIEW
view_name
AS SELECT
column1
,
column2
, ...
FROM
table_name
WHERE
condition
;

Note

A view always shows real-time data! The database engine only stores the view's definition (the SELECT statement), not a copy of the data.

Example

CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';

To query the view above, use the following SQL syntax:

Example

SELECT * FROM [Brazil Customers];

The following SQL creates a view named "Products Above Average Price", that selects all products in the "Products" table with a Price higher than the average price:

Example

CREATE VIEW [Products Above Average Price] AS
SELECT ProductName, Price
FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);

To query the view above, use the following SQL syntax:

Example

SELECT * FROM [Products Above Average Price];

ALTER VIEW Statement (SQL Server)

In SQL Server, a view can be updated with the ALTER VIEW statement.

ALTER VIEW Syntax

ALTER VIEW
view_name
AS SELECT
column1
,
column2
, ...
FROM
table_name
WHERE
condition
;

The following SQL adds the "City" column to the "Brazil Customers" view:

Example

ALTER VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName, City
FROM Customers
WHERE Country = 'Brazil';

CREATE OR REPLACE VIEW Statement (MySQL and Oracle)

In MySQL and Oracle, a view can be updated with the CREATE OR REPLACE VIEW statement.

CREATE OR REPLACE VIEW Syntax

CREATE OR REPLACE VIEW
view_name
AS SELECT
column1
,
column2
, ...
FROM
table_name
WHERE
condition
;

The following SQL adds the "City" column to the "Brazil Customers" view:

Example

CREATE OR REPLACE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName, City
FROM Customers
WHERE Country = 'Brazil';

DROP VIEW Statement

A view is deleted with the DROP VIEW statement.

DROP VIEW Syntax

DROP VIEW
view_name
;

The following SQL deletes the "Brazil Customers" view:

Example

DROP VIEW [Brazil Customers];

Previous

SQL Working With Dates

Next

SQL Injection

This chapter

Overview
21

Lessons

50m

Read time

1. SQL CREATE DATABASE Statement2. SQL DROP DATABASE Statement3. SQL BACKUP DATABASE Statement4. SQL CREATE TABLE Statement5. SQL DROP TABLE Statement6. SQL ALTER TABLE Statement7. SQL Constraints8. SQL NOT NULL Constraint9. SQL UNIQUE Constraint10. SQL PRIMARY KEY Constraint11. SQL FOREIGN KEY Constraint12. SQL CHECK Constraint13. SQL DEFAULT Constraint14. SQL CREATE INDEX Statement15. SQL AUTO INCREMENT Field16. SQL Working With Dates17. SQL Views18. SQL Injection19. SQL Parameters20. SQL Prepared Statements21. SQL Hosting

On this page

SQL CREATE VIEW StatementCREATE VIEW SyntaxALTER VIEW Statement (SQL Server)ALTER VIEW SyntaxCREATE OR REPLACE VIEW Statement (MySQL and Oracle)CREATE OR REPLACE VIEW SyntaxDROP VIEW StatementDROP VIEW Syntax