How to CREATE VIEW in PostgreSQL

Galaxy Glossary

How do I create or replace a view in Oracle?

CREATE VIEW builds a stored SELECT query that can be referenced like a table.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

What does CREATE VIEW do in Oracle?

CREATE VIEW stores a SQL query as a named, virtual table. Querying the view runs the underlying SELECT, letting you hide complexity, improve security, and reuse logic.

Why use a view instead of a table?

Views avoid data duplication, centralize business rules, and expose only the columns users need. They update automatically when base tables change, so no refresh jobs are required.

How to define a simple view?

Use CREATE VIEW view_name AS SELECT ... FROM ....Oracle saves the SELECT text; no data is copied.

How to create a view with joins?

Join Orders and Customers to give analysts a ready-made dataset. Filters, calculations, and column aliases can be embedded.

How to replace an existing view?

Add OR REPLACE after CREATE VIEW. The view is rebuilt in place without having to DROP and re-grant privileges.

How to lock data entry on a view?

Add WITH READ ONLY to prevent DML through the view.This protects analytics views from accidental updates.

Best practices for CREATE VIEW

Name views by business purpose, not table names. Keep SELECTs simple; nest complicated logic in separate views. Add column comments for self-documentation.

Common pitfalls and how to avoid them

FORCE creates invalid views if base tables are missing—avoid in production. WITH CHECK OPTION rejects inserts that don’t satisfy the view’s WHERE clause; use only when needed.

.

Why How to CREATE VIEW in PostgreSQL is important

How to CREATE VIEW in PostgreSQL Example Usage


-- Total spent per customer in the last year
SELECT customer_name,
       SUM(total_amount) AS yearly_spend
FROM   customer_orders_v
GROUP  BY customer_name
ORDER  BY yearly_spend DESC;

How to CREATE VIEW in PostgreSQL Syntax


CREATE [OR REPLACE] [FORCE | NOFORCE] VIEW view_name [(column_alias [, ...])] AS
    SELECT columns
    FROM   base_tables [WHERE ...]
    [WITH READ ONLY | WITH CHECK OPTION [CONSTRAINT constraint_name]];

-- Ecommerce example
CREATE OR REPLACE VIEW customer_orders_v AS
SELECT  c.id          AS customer_id,
        c.name        AS customer_name,
        c.email,
        o.id          AS order_id,
        o.order_date,
        o.total_amount
FROM    Customers c
JOIN    Orders    o ON o.customer_id = c.id
WHERE   o.order_date >= ADD_MONTHS(SYSDATE, -12)
WITH READ ONLY;

Common Mistakes

Frequently Asked Questions (FAQs)

Can a view improve query performance?

Yes, Oracle’s optimizer can rewrite queries using the view’s SQL. Materialized views give even bigger gains because they store data.

Are indexes required on a view?

Indexes go on the underlying tables. A view itself holds no data, so you can’t index it directly.

How do I drop a view?

Run DROP VIEW view_name;. Add CASCADE CONSTRAINTS if foreign-key constraints reference the view.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo