Skip to content
Closed
62 changes: 61 additions & 1 deletion docs/sql-ref-syntax-ddl-create-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,64 @@ license: |
limitations under the License.
---

**This page is under construction**
### Description
Views are based on the result-set of an `SQL` query. `CREATE VIEW` constructs
a virtual table that has no physical data therefore other operations like
`ALTER VIEW` and `DROP VIEW` only change metadata.

### Syntax
{% highlight sql %}
CREATE [OR REPLACE] [[GLOBAL] TEMPORARY] VIEW [IF NOT EXISTS] [db_name.]view_name
create_view_clauses
AS query;
{% endhighlight %}

### Parameters
<dl>
<dt><code><em>OR REPLACE</em></code></dt>
<dd>If a view of same name already exists, it will be replaced.</dd>
</dl>
<dl>
<dt><code><em>[GLOBAL] TEMPORARY</em></code></dt>
<dd>TEMPORARY views are session-scoped and will be dropped when session ends
because it skips persisting the definition in the underlying metastore, if any.
GLOBAL TEMPORARY views are tied to a system preserved temporary database `global_temp`.</dd>
</dl>
<dl>
<dt><code><em>IF NOT EXISTS</em></code></dt>
<dd>Creates a view if it does not exists.</dd>
</dl>
<dl>
<dt><code><em>create_view_clauses</em></code></dt>
<dd>These clauses are optional and order insensitive. It can be of following formats.
<ul>
<li><code>[(column_name [COMMENT column_comment], ...) ]</code> to specify column-level comments.</li>
<li><code>[COMMENT view_comment]</code> to specify view-level comments.</li>
<li><code>[TBLPROPERTIES (property_name = property_value, ...)]</code> to add metadata key-value pairs.</li>
</ul>
</dd>
</dl>
<dl>
<dt><code><em>query</em></code></dt>
<dd>A <a href="sql-ref-syntax-qry-select.md">SELECT</a> statement that constructs the view from base tables or other views.</dd>
</dl>

### Examples
{% highlight sql %}
-- Create or replace view for `experienced_employee` with comments.
CREATE OR REPLACE VIEW experienced_employee
(ID COMMENT 'Unique identification number', Name)
COMMENT 'View for experienced employees'
AS SELECT id, name FROM all_employee
WHERE working_years > 5;

-- Create a global temporary view `subscribed_movies` if it does not exist.
CREATE GLOBAL TEMPORARY VIEW IF NOT EXISTS subscribed_movies
AS SELECT mo.member_id, mb.full_name, mo.movie_title
FROM movies AS mo INNER JOIN members AS mb
ON mo.member_id = mb.id;
{% endhighlight %}

### Related Statements
- [ALTER VIEW](sql-ref-syntax-ddl-alter-view.md)
- [DROP VIEW](sql-ref-syntax-ddl-drop-view.md)