CREATE VIEW stores a reusable SELECT query as a virtual table, optimizing reusability and security without duplicating data.
CREATE VIEW lets you package complex SELECT logic behind a simple table-like object. Analysts query the view as if it were a table, keeping business logic in one place and preventing code duplication.
Use CREATE VIEW view_name AS SELECT ... . Supply column aliases when expressions are unclear. Add OR REPLACE to update an existing view without dropping it first.
Yes. For example, you can join Orders
, OrderItems
and Products
to expose revenue metrics. Users can then add WHERE
filters when querying the view.
Regular views run the underlying SELECT at query time. If performance is critical and data latency can be tolerated, switch to MATERIALIZED VIEW to store pre-computed results.
sales_
or customer_
.SELECT
only on views to protect raw tables.The example below shows how to expose daily revenue per product using familiar ecommerce tables.
No. Regular views are virtual. The underlying SELECT re-executes on each query, so storage is minimal.
You cannot add indexes directly. Optimize the underlying tables or switch to a MATERIALIZED VIEW for faster reads.
Run DROP VIEW [IF EXISTS] view_name;
. Add IF EXISTS to avoid errors if the view is already gone.