Lesson 16: Views

17 Jul 2026 1 min Swarnil Singhai

A view is a saved, named query that behaves like a virtual table — it simplifies repeated complex queries and can restrict what columns are exposed.

Real-time example

An analytics team gets an active_customers view instead of the raw SQL, so the join logic lives in one place.

CREATE VIEW active_customers AS
SELECT c.id, c.name, COUNT(o.id) AS order_count
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= now() - interval '90 days'
GROUP BY c.id, c.name;

SELECT * FROM active_customers WHERE order_count > 5;

What's happening

Every time active_customers is queried, the underlying SELECT actually re-runs — a view doesn't store data, it stores the query.

For a view that's queried very often on large data, look into a materialized view, which does cache the result.

Advertisement