Saving a query as a view
A view is a saved query with a name. You query it like a table, but it holds no data of its own: each time you use it, it runs its query on the latest data.
CREATE VIEW completed_orders AS
SELECT * FROM orders
WHERE status = 'Completed'Now anyone can write SELECT COUNT(*) FROM completed_orders without remembering the filter. That's
the point of views: put a business rule (what counts as revenue, which customers are active) in
one place, and every report that uses the view agrees.
CREATE VIEW order_lines AS
SELECT oi.order_id, o.order_date, oi.product_id,
oi.quantity * oi.unit_price AS revenue
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
WHERE o.status = 'Completed'Views can join, group and filter, and you can join a view to tables or to other views.