30. Views: a query with a name

Save a query you use often under a name, and use it as if it were a table.

What a view is

By now some of our queries are long: an order's total needs orders, customer and order_item joined and summed. Writing that out every time is tiring and easy to get wrong.

A view is a SELECT saved in the database under a name. Once it is made, you can read from it just as from a table. CREATE VIEW name AS SELECT ... makes one:

CREATE VIEW order_total AS
SELECT o.id, c.name AS customer, o.ordered, o.status,
       sum(oi.qty * oi.price) AS total
FROM orders AS o
JOIN customer AS c ON c.id = o.customer_id
JOIN order_item AS oi ON oi.order_id = o.id
GROUP BY o.id;
✓ Done

Nothing comes out: the statement only stores the query. Now read the view:

SELECT * FROM order_total ORDER BY id;
idcustomerorderedstatustotal
1Somchai Jaidee2026-09-02paid180.0
2Malee Srisuk2026-09-05shipped165.0
3Somchai Jaidee2026-09-12shipped175.0
4Anan Wongsa2026-09-20paid300.0
5Pim Chaiya2026-10-01cancelled150.0
6Narin Thongdee2026-10-03shipped290.0
7Malee Srisuk2026-10-10paid235.0
8Kanya Boonmee2026-10-15shipped390.0
9Wichai Saelim2026-10-22paid490.0
10Somchai Jaidee2026-10-28paid45.0
11Siriporn Kaewdee2026-11-02shipped300.0
12Pim Chaiya2026-11-05paid175.0
13Kanya Boonmee2026-11-11paid90.0
14Anan Wongsa2026-11-18cancelled80.0
15Wichai Saelim2026-11-25paid375.0
15 rows

All 15 orders, each with its customer's name and its total, from a statement as short as SELECT * FROM category.

Using a view as a table

Wherever a SELECT can name a table, it can name a view: with WHERE, GROUP BY, JOIN, a subquery, anything. Here is how much each customer has spent, cancelled orders left out:

SELECT customer, count(*) AS orders, sum(total) AS spent
FROM order_total
WHERE status <> 'cancelled'
GROUP BY customer
ORDER BY spent DESC, customer;
customerordersspent
Wichai Saelim2865.0
Kanya Boonmee2480.0
Malee Srisuk2400.0
Somchai Jaidee3400.0
Anan Wongsa1300.0
Siriporn Kaewdee1300.0
Narin Thongdee1290.0
Pim Chaiya1175.0
8 rows

Wichai Saelim leads with 865 baht from 2 orders. Malee Srisuk and Somchai Jaidee both spent 400, so customer after spent DESC decides their order.

The view's columns are named by the SELECT inside it: customer and total come from the AS there. Give every worked-out column a name with AS in a view, or you will have to use names like sum(oi.qty * oi.price) later.

A view keeps the query, not the data

This is the most important thing about views: a view holds no data. It keeps only the query, and runs it again each time you read the view. So whatever changes in the tables shows in the view at once.

Order 13 is worth 90 baht now:

SELECT * FROM order_total WHERE id = 13;
idcustomerorderedstatustotal
13Kanya Boonmee2026-11-11paid90.0
1 row

Add 3 green teas to it, in the order_item table, and read the view again. We didn't touch the view at all:

INSERT INTO order_item (order_id, product_id, qty, price) VALUES (13, 2, 3, 45);
SELECT * FROM order_total WHERE id = 13;
idcustomerorderedstatustotal
13Kanya Boonmee2026-11-11paid225.0
1 row

The total is now 225: 90 + 3 × 45. A view is never "out of date", because it has nothing of its own to go out of date.

A view doesn't make a query faster. Reading order_total runs the whole join and sum every time, as if you had typed it out. A view saves your effort, not the database's. If a view is slow, the fix is in its query and in the indexes of the tables under it (chapter 25).

Views to hide complexity and to show only some columns

There are two common reasons for a view.

To show only some columns. Customers' phone numbers are private. Give the people who only need names and cities a view without the phone column, and they can do their work without seeing the numbers:

CREATE VIEW customer_public AS
SELECT id, name, city FROM customer;

SELECT * FROM customer_public WHERE city = 'Bangkok' ORDER BY id;
idnamecity
1Somchai JaideeBangkok
3Anan WongsaBangkok
7Wichai SaelimBangkok
10Lalita MeesukBangkok
4 rows

To hide complexity. A view can hold joins, CASE and calculations, so whoever uses it just sees plain columns. You can also name the view's columns in brackets after its name, instead of AS on each one:

CREATE VIEW product_list (product, category, price, stock_note) AS
SELECT p.name, c.name, p.price,
       CASE WHEN p.stock = 0 THEN 'out of stock'
            WHEN p.stock < 20 THEN 'running low'
            ELSE 'ok' END
FROM product AS p JOIN category AS c ON c.id = p.category_id;

SELECT * FROM product_list WHERE stock_note <> 'ok' ORDER BY product;
productcategorypricestock_note
Banana BreadBakery60.0running low
CheesecakeBakery120.0running low
HighlighterStationery25.0out of stock
Orange JuiceDrinks40.0out of stock
4 rows

4 products need attention: two are out of stock and two are running low. Whoever reads product_list doesn't need to know where stock_note comes from; and if the shop changes "running low" to fewer than 10 one day, only the view changes, not every query that uses it.

Views are kept in the database's list of objects with the tables, sqlite_schema, with type view:

SELECT type, name FROM sqlite_schema WHERE type = 'view' ORDER BY name;
typename
viewcustomer_public
vieworder_total
viewproduct_list
3 rows

A view can't be changed directly

A view is a query, so where would a new row go? The database doesn't guess. INSERT, UPDATE and DELETE on a view are refused:

UPDATE customer_public SET city = 'Phuket' WHERE id = 3;
Error: cannot modify customer_public because it is a view
INSERT INTO customer_public (id, name, city) VALUES (11, 'Dao', 'Trang');
Error: cannot modify customer_public because it is a view

Change the table under it (customer) instead, and the view shows the change. If you really want people to write through a view, you can tell the database what to do with an INSTEAD OF trigger; that is in chapter 31.

Temporary views and DROP VIEW

CREATE TEMP VIEW makes a view that lasts only while this connection is open: handy for a query you lean on during one piece of work, without leaving it in the file for everyone. A view can also be built on another view:

CREATE TEMP VIEW big_order AS
SELECT * FROM order_total WHERE total >= 300;

SELECT id, customer, total FROM big_order ORDER BY total DESC;
idcustomertotal
9Wichai Saelim490.0
8Kanya Boonmee390.0
15Wichai Saelim375.0
4Anan Wongsa300.0
11Siriporn Kaewdee300.0
5 rows

DROP VIEW removes a view. It removes only the saved query; the tables and their data stay as they were:

DROP VIEW customer_public;
✓ Done
SELECT * FROM customer_public;
Error: no such table: customer_public

DROP VIEW IF EXISTS doesn't complain when there is no such view, which suits scripts that may run twice. The customers are all still here:

DROP VIEW IF EXISTS customer_public;
SELECT count(*) FROM customer;
count(*)
10
1 row

There is no ALTER VIEW in SQLite. To change a view, DROP VIEW it and CREATE VIEW it again with the new query (in a transaction, chapter 19, so nobody sees the moment it is missing).

  1. Make a view out_of_stock of the products whose stock is 0 (id, name, price) and read it.
  2. Make a view category_sales with each category's name and its sales (qty * price of every order line), and show it from the biggest down.
  3. Orange Juice (id 3) gets 50 new bottles. Set its stock, then read out_of_stock again: what changed, and why didn't you have to touch the view?
Show the answer
CREATE VIEW out_of_stock AS
SELECT id, name, price FROM product WHERE stock = 0;
SELECT * FROM out_of_stock ORDER BY id;

CREATE VIEW category_sales AS
SELECT c.name AS category, sum(oi.qty * oi.price) AS sales
FROM category AS c
JOIN product AS p ON p.category_id = c.id
JOIN order_item AS oi ON oi.product_id = p.id
GROUP BY c.id;
SELECT * FROM category_sales ORDER BY sales DESC;

UPDATE product SET stock = 50 WHERE id = 3;
SELECT * FROM out_of_stock ORDER BY id;
idnameprice
3Orange Juice40.0
12Highlighter25.0
2 rows
categorysales
Stationery1065.0
Snacks935.0
Drinks825.0
Bakery750.0
4 rows
idnameprice
12Highlighter25.0
1 row

Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made