CREATE VIEW

StevenSwiniarski's avatar
Published Jul 13, 2022
Contribute to Docs

The CREATE VIEW command creates a virtual table based on a saved query. The resulting view can be accessed via a SELECT statement, just like a normal table in the database.

Syntax

CREATE VIEW viewname AS
SELECT statements

Where viewname is a valid SQL name and SELECT statements can be any valid SELECT query involving any number of tables and may include WHERE, GROUP BY, HAVING, or any type of JOIN. A view can also reference other views as well as tables.

Note: A view will require each column to have a unique name.

Example

The following example creates a view named student_count_by_country based on a SELECT statement grouping the students table by birth_country:

CREATE VIEW student_count_by_country
AS
SELECT COUNT(student_name) AS student_count,
birth_country
FROM students
GROUP BY birth_country

All contributors

Contribute to Docs

Learn SQL on Codecademy