This resource demystifies SQL views. You’ll learn what a view is, why it’s useful, how to create and modify views, and the best practices for using them safely. Interactive examples and Galaxy-specific guidance help you practice in a modern SQL editor.
You should be able to write basic SELECT statements and understand primary key/foreign key relationships. No prior experience with views is required.
A view is a saved query definition that behaves like a virtual table. When you query a view, the database runs the underlying SELECT statement and returns the result set on demand.
Unlike a table, a standard (non-materialized) view stores no data. It stores only the SQL text. Each time you access the view, the database recomputes the results.
-- Logical definition only
CREATE VIEW active_users AS
SELECT id, email, last_login
FROM users
WHERE last_login >= NOW() - INTERVAL '30 days';
Because it’s virtual, the data is always as fresh as the underlying tables. But it also means performance is tied to the complexity of the SELECT.
Some databases support materialized views, which cache results physically and refresh on a schedule. They improve read performance at the cost of storage and freshness. This guide focuses on regular views, but nearly all concepts carry over.
Wrap complex joins and calculations once and expose a clean interface:
CREATE VIEW customer_revenue AS
SELECT c.id,
c.name,
SUM(o.total) AS lifetime_value
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name;
Analysts can now SELECT * FROM customer_revenue
without re-writing the aggregation.
Grant SELECT on a view instead of the base table to hide sensitive columns or rows.
CREATE VIEW public_users AS
SELECT id, username
FROM users
WHERE is_staff = FALSE;
GRANT SELECT ON public_users TO analytics_team;
By centralizing a formula (e.g., “active user”), you reduce metric drift across teams.
With materialized views or indexed views (SQL Server), you can precompute heavy aggregations.
General syntax:
CREATE [OR REPLACE] VIEW view_name AS
SELECT ...;
OR REPLACE allows you to overwrite an existing view atomically (supported in PostgreSQL, Snowflake, BigQuery).
-- 1. Create
CREATE VIEW recent_orders AS
SELECT *
FROM orders
WHERE created_at >= NOW() - INTERVAL '7 days';
-- 2. Query like a table
SELECT customer_id, COUNT(*)
FROM recent_orders
GROUP BY customer_id;
-- 3. Update definition
CREATE OR REPLACE VIEW recent_orders AS
SELECT *
FROM orders
WHERE created_at >= NOW() - INTERVAL '14 days';
Can you INSERT
, UPDATE
, or DELETE
rows via a view? It depends:
WITH CHECK OPTION
for constraint enforcement.DROP VIEW IF EXISTS recent_orders;
Careful! Dependent views or stored procedures may break.
v_
or _view
) or descriptive nouns (daily_active_users
).Follow this mini-project using Galaxy’s free workspace. We’ll use a sample e-commerce schema (customers
, orders
, order_items
).
If you’re on PostgreSQL, run:
\i https://raw.githubusercontent.com/galaxy-examples/ecommerce.sql
Galaxy’s AI copilot can auto-suggest this command if you start typing “load ecommerce”.
CREATE VIEW monthly_revenue AS
SELECT DATE_TRUNC('month', created_at) AS revenue_month,
SUM(total) AS revenue
FROM orders
GROUP BY 1;
SELECT * FROM monthly_revenue ORDER BY revenue_month;
Use Galaxy’s inline chart (shortcut Shift + V
) to visualize the revenue trend without leaving the editor.
CREATE VIEW anon_customers AS
SELECT id,
LEFT(email, 3) || '***@' || SPLIT_PART(email, '@', 2) AS masked_email,
country
FROM customers;
GRANT SELECT ON anon_customers TO support_team;
Galaxy’s permission UI lets you map database roles to workspace roles so that only the support team can run this query.
Add both views to a “Finance Metrics” Collection and click “Endorse.” Teammates can now reference them with confidence.
CREATE OR REPLACE VIEW ... WITH (security_barrier)
.CREATE VIEW
, update with OR REPLACE
, and manage them like code.Happy querying — and may your views always stay in sync!