8.10. CREATE VIEW

Synopsis

  1. CREATE [ OR REPLACE ] VIEW view_name AS query

Description

Create a new view of a SELECT query. The view is a logical tablethat can be referenced by future queries. Views do not contain any data.Instead, the query stored by the view is executed everytime the view isreferenced by another query.

The optional OR REPLACE clause causes the view to be replaced if italready exists rather than raising an error.

Examples

Create a simple view test over the orders table:

  1. CREATE VIEW test AS
  2. SELECT orderkey, orderstatus, totalprice / 2 AS half
  3. FROM orders

Create a view orders_by_date that summarizes orders:

  1. CREATE VIEW orders_by_date AS
  2. SELECT orderdate, sum(totalprice) AS price
  3. FROM orders
  4. GROUP BY orderdate

Create a view that replaces an existing view:

  1. CREATE OR REPLACE VIEW test AS
  2. SELECT orderkey, orderstatus, totalprice / 4 AS quarter
  3. FROM orders

See Also

DROP VIEW, SHOW CREATE VIEW