← Back to SQL, page 12

sql-0237 · Easy fresher

What is a view, and what are good reasons to create one?

A view is a stored, named query that behaves like a table when you select from it. It does not store data; the query runs each time. Views give a stable name to complex joins, hide columns or rows from some users, and let you change the underlying tables while keeping the interface the same.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
sql
CREATE VIEW shipped_revenue AS
SELECT c.name, SUM(i.qty * i.unit_price) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id AND o.status = 'shipped'
JOIN order_items i ON i.order_id = o.id
GROUP BY c.id, c.name;
SELECT * FROM shipped_revenue ORDER BY revenue DESC;
namerevenue
Ana43
Chloe42

The view hides the join and the cancelled-order filter. Every report that reads shipped_revenue uses the same definition, so the rule "cancelled orders do not count" lives in one place.

Good uses:

  • Naming business definitions (active customer, net revenue).
  • Restricting access: grant SELECT on a view that omits salary or personal columns, and not on the base table.
  • Keeping old column names working during a migration.

Views do not make queries faster by themselves; the engine expands the view into the outer query. Stacking views on views can produce plans that are hard to read and tune.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

CREATE VIEW shipped_revenue AS
SELECT c.name, SUM(i.qty * i.unit_price) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id AND o.status = 'shipped'
JOIN order_items i ON i.order_id = o.id
GROUP BY c.id, c.name;
SELECT * FROM shipped_revenue ORDER BY revenue DESC;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • A view is a named, stored query
  • It stores no data; it runs on each use
  • Useful for shared definitions, security and migrations
  • Views do not speed up queries on their own

Common mistakes

  • Expecting a plain view to cache results
  • Building deep stacks of views that nobody can tune

Follow-ups

  • What is a materialized view?
  • Can you INSERT into a view?

Tips

  • Mention the single-definition benefit