--- title: "CREATE VIEW" description: "`CREATE VIEW` defines view, which provides an alias for the embedded `SELECT` statement." menu: # This should also have a "non-content entry" under Reference, which is # configured in doc/user/config.toml main: parent: 'commands' --- `CREATE VIEW` defines a view, which simply provides an alias for the embedded `SELECT` statement. The results of a view can be incrementally maintained **in memory** within a [cluster](/concepts/clusters/) by creating an [index](../create-index). This allows you to serve queries without the overhead of materializing the view. ### Usage patterns {{% views-indexes/table-usage-pattern-intro %}} {{% views-indexes/table-usage-pattern %}} ## Syntax {{< diagram "create-view.svg" >}} Field | Use ------|----- **TEMP** / **TEMPORARY** | Mark the view as [temporary](#temporary-views). **OR REPLACE** | If a view exists with the same name, replace it with the view defined in this statement. You cannot replace views that other views depend on, nor can you replace a non-view object with a view. **IF NOT EXISTS** | If specified, _do not_ generate an error if a view of the same name already exists.

If _not_ specified, throw an error if a view of the same name already exists. _(Default)_ _view_name_ | A name for the view. **(** _col_ident_... **)** | Rename the `SELECT` statement's columns to the list of identifiers, both of which must be the same length. Note that this is required for statements that return multiple columns with the same identifier. _select_stmt_ | The [`SELECT` statement](../select) to embed in the view. ## Details [//]: # "TODO(morsapaes) Add short usage patterns section + point to relevant architecture patterns once these exist." ### Temporary views The `TEMP`/`TEMPORARY` keyword creates a temporary view. Temporary views are automatically dropped at the end of the SQL session and are not visible to other connections. They are always created in the special `mz_temp` schema. Temporary views may depend upon other temporary database objects, but non-temporary views may not depend on temporary objects. ## Examples ### Creating a view ```mzsql CREATE VIEW purchase_sum_by_region AS SELECT sum(purchase.amount) AS region_sum, region.id AS region_id FROM region INNER JOIN user ON region.id = user.region_id INNER JOIN purchase ON purchase.user_id = user.id GROUP BY region.id; ``` ## Privileges The privileges required to execute this statement are: {{< include-md file="shared-content/sql-command-privileges/create-view.md" >}} ## Additional information - Views can be monotonic; that is, views can be recognized as append-only. ## Related pages - [`SHOW VIEWS`](../show-views) - [`SHOW CREATE VIEW`](../show-create-view) - [`DROP VIEW`](../drop-view) - [`CREATE MATERIALIZED VIEW`](../create-materialized-view) - [`CREATE INDEX`](../create-index)