Summary: in this tutorial, you will learn how to use the SQL Server CREATE VIEW statement to create new views.
To create a new view in SQL Server, you use the CREATE VIEW statement as shown below:
CREATE VIEW [OR ALTER] schema_name.view_name [(column_list)]
AS
select_statement;
Code language: SQL (Structured Query Language) (sql)In this syntax:
- First, specify the name of the view after the
CREATE VIEWkeywords. Theschema_nameis the name of the schema to which the view belongs. - Second, specify a
SELECTstatement (select_statement) that defines the view after theASkeyword. TheSELECTstatement can refer to one or more tables.
If you don’t explicitly specify a list of columns for the view, SQL Server will use the column list derived from the SELECT statement.
In case you want to redefine the view e.g., adding more columns to it or removing some columns from it, you can use the OR ALTER keywords after the CREATE VIEW keywords.
SQL Server CREATE VIEW examples #
We will use the orders, order_items, and products tables from the sample database for the demonstration.