Save a query as a view

A query you run again and again can have a name. A view is a saved query with a name, and other queries read it as if it were a table.

How it works

CREATE VIEW saves a query under a name and stores no rows. When a query reads the view, BigQuery puts the saved SQL into that query and runs the whole thing.

Common mistake

A report that must not change after you send it needs a saved table. A view would show every later change to the tables.

Worked example

CREATE OR REPLACE VIEW learn.shop.pricey_products AS
SELECT product_id, product_name, price
FROM `learn.shop.products`
WHERE price >= 200;

SELECT product_name, price
FROM learn.shop.pricey_products
ORDER BY price DESC;

The first statement saves the query. Nothing is computed and no rows are stored, so the statement is over at once.

The last query reads the view in its FROM. BigQuery runs the saved SELECT with its WHERE and returns the three products that cost 200 or more.

All BigQuery lessons