CREATE VIEW
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_countryASSELECT COUNT(student_name) AS student_count,birth_countryFROM studentsGROUP BY birth_country
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
- Skill path
Analyze Data with SQL
Learn to analyze data with SQL and prepare for technical interviews.Includes 9 CoursesWith CertificateBeginner Friendly17 hours - Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly5 hours