0% found this document useful (0 votes)
118 views9 pages

W3schools: SQL Views

Uploaded by

G.D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views9 pages

W3schools: SQL Views

Uploaded by

G.D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

w3schools.com

  HTML CSS MORE  

SQL Views
❮ Previous Next ❯

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 functions, WHERE, and JOIN statements to a view and present the data as
if the data were coming from one single table.

CREATE VIEW Syntax

CREATE VIEW view_name AS


SELECT column1, column2, ...
FROM table_name
WHERE condition;

Note: A view always shows up-to-date data! The database engine recreates the data, using
the view's SQL statement, every time a user queries a view.

SQL CREATE VIEW Examples


https://www.w3schools.com/sql/sql_view.asp 1/9
11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

The following SQL creates a view that shows all customers from Brazil:

Example

CREATE VIEW [Brazil Customers] AS


SELECT CustomerName, ContactName
FROM Customers
WHERE Country = "Brazil";

Try it Yourself »

We can query the view above as follows:

Example

SELECT * FROM [Brazil Customers];

Try it Yourself »

The following SQL creates a view that selects every product 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);

Try it Yourself »

We can query the view above as follows:

Example
https://www.w3schools.com/sql/sql_view.asp 2/9
11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

SELECT * FROM [Products Above Average Price];

Try it Yourself »

SQL Updating a View


A view can be updated with the CREATE OR REPLACE VIEW command.

SQL 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";

Try it Yourself »

https://www.w3schools.com/sql/sql_view.asp 3/9
11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

SQL Dropping a View


A view is deleted with the DROP VIEW command.

SQL DROP VIEW Syntax

DROP VIEW view_name;

The following SQL drops the "Brazil Customers" view:

Example

DROP VIEW [Brazil Customers];

Try it Yourself »

❮ Previous Next ❯

https://www.w3schools.com/sql/sql_view.asp 4/9
11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

COLOR PICKER

HOW TO

Tabs
Dropdowns
Accordions
Side Navigation
Top Navigation

https://www.w3schools.com/sql/sql_view.asp 5/9
11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List

SHARE

  

CERTIFICATES
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.

Read More »

https://www.w3schools.com/sql/sql_view.asp 6/9
11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

https://www.w3schools.com/sql/sql_view.asp 7/9
11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

REPORT ERROR
PRINT PAGE
FORUM
ABOUT

Top 10 Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
SQL Tutorial
PHP Tutorial
jQuery Tutorial
Python Tutorial

Top 10 References
HTML Reference
CSS Reference
JavaScript Reference
W3.CSS Reference
Bootstrap Reference
SQL Reference
PHP Reference
HTML Colors
jQuery Reference
Python Reference

Top 10 Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
jQuery Examples
Angular Examples
XML Examples

Web Certificates
HTML Certificate
CSS Certificate
JavaScript Certificate
jQuery Certificate
PHP Certificate
Bootstrap Certificate
XML Certificate
https://www.w3schools.com/sql/sql_view.asp 8/9
11/19/2018 SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and
basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use,
cookie and privacy policy. Copyright 1999-2018 by Refsnes Data. All Rights Reserved.
Powered by W3.CSS.

https://www.w3schools.com/sql/sql_view.asp 9/9

You might also like