CREATE VIEW Statement CREATE VIEW

CREATE VIEW statement The CREATE VIEW statement lets you create a shorthand abbreviation for a more complicated query. The base query can involve joins, expressions, reordered columns, column aliases, and other SQL features that can make a query hard to understand or maintain.

Because a view is purely a logical construct (an alias for a query) with no physical data behind it, ALTER VIEW only involves changes to metadata in the metastore database, not any data files in HDFS.

CREATE VIEW [IF NOT EXISTS] view_name [(column_list)] AS select_statement

The CREATE VIEW statement can be useful in scenarios such as the following:

For queries that require repeating complicated clauses over and over again, for example in the select list, ORDER BY, and GROUP BY clauses, you can use the WITH clause as an alternative to creating a view.

-- Create a view that is exactly the same as the underlying table. create view v1 as select * from t1; -- Create a view that includes only certain columns from the underlying table. create view v2 as select c1, c3, c7 from t1; -- Create a view that filters the values from the underlying table. create view v3 as select distinct c1, c3, c7 from t1 where c1 is not null and c5 > 0; -- Create a view that that reorders and renames columns from the underlying table. create view v4 as select c4 as last_name, c6 as address, c2 as birth_date from t1; -- Create a view that runs functions to convert or transform certain columns. create view v5 as select c1, cast(c3 as string) c3, concat(c4,c5) c5, trim(c6) c6, "Constant" c8 from t1; -- Create a view that hides the complexity of a view query. create view v6 as select t1.c1, t2.c2 from t1 join t2 on t1.id = t2.id;

, ,