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;Nothing comes out: the statement only stores the query. Now read the view:
SELECT * FROM order_total ORDER BY id;| id | customer | ordered | status | total |
|---|---|---|---|---|
| 1 | Somchai Jaidee | 2026-09-02 | paid | 180.0 |
| 2 | Malee Srisuk | 2026-09-05 | shipped | 165.0 |
| 3 | Somchai Jaidee | 2026-09-12 | shipped | 175.0 |
| 4 | Anan Wongsa | 2026-09-20 | paid | 300.0 |
| 5 | Pim Chaiya | 2026-10-01 | cancelled | 150.0 |
| 6 | Narin Thongdee | 2026-10-03 | shipped | 290.0 |
| 7 | Malee Srisuk | 2026-10-10 | paid | 235.0 |
| 8 | Kanya Boonmee | 2026-10-15 | shipped | 390.0 |
| 9 | Wichai Saelim | 2026-10-22 | paid | 490.0 |
| 10 | Somchai Jaidee | 2026-10-28 | paid | 45.0 |
| 11 | Siriporn Kaewdee | 2026-11-02 | shipped | 300.0 |
| 12 | Pim Chaiya | 2026-11-05 | paid | 175.0 |
| 13 | Kanya Boonmee | 2026-11-11 | paid | 90.0 |
| 14 | Anan Wongsa | 2026-11-18 | cancelled | 80.0 |
| 15 | Wichai Saelim | 2026-11-25 | paid | 375.0 |
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;| customer | orders | spent |
|---|---|---|
| Wichai Saelim | 2 | 865.0 |
| Kanya Boonmee | 2 | 480.0 |
| Malee Srisuk | 2 | 400.0 |
| Somchai Jaidee | 3 | 400.0 |
| Anan Wongsa | 1 | 300.0 |
| Siriporn Kaewdee | 1 | 300.0 |
| Narin Thongdee | 1 | 290.0 |
| Pim Chaiya | 1 | 175.0 |
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;| id | customer | ordered | status | total |
|---|---|---|---|---|
| 13 | Kanya Boonmee | 2026-11-11 | paid | 90.0 |
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;| id | customer | ordered | status | total |
|---|---|---|---|---|
| 13 | Kanya Boonmee | 2026-11-11 | paid | 225.0 |
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;| id | name | city |
|---|---|---|
| 1 | Somchai Jaidee | Bangkok |
| 3 | Anan Wongsa | Bangkok |
| 7 | Wichai Saelim | Bangkok |
| 10 | Lalita Meesuk | Bangkok |
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;| product | category | price | stock_note |
|---|---|---|---|
| Banana Bread | Bakery | 60.0 | running low |
| Cheesecake | Bakery | 120.0 | running low |
| Highlighter | Stationery | 25.0 | out of stock |
| Orange Juice | Drinks | 40.0 | out of stock |
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;| type | name |
|---|---|
| view | customer_public |
| view | order_total |
| view | product_list |
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;INSERT INTO customer_public (id, name, city) VALUES (11, 'Dao', 'Trang');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;| id | customer | total |
|---|---|---|
| 9 | Wichai Saelim | 490.0 |
| 8 | Kanya Boonmee | 390.0 |
| 15 | Wichai Saelim | 375.0 |
| 4 | Anan Wongsa | 300.0 |
| 11 | Siriporn Kaewdee | 300.0 |
DROP VIEW removes a view. It removes only the saved query; the tables and their data stay as they were:
DROP VIEW customer_public;SELECT * FROM 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 |
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).
- Make a view
out_of_stockof the products whosestockis 0 (id,name,price) and read it. - Make a view
category_saleswith each category's name and its sales (qty * priceof every order line), and show it from the biggest down. - Orange Juice (id 3) gets 50 new bottles. Set its stock, then read
out_of_stockagain: 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;| id | name | price |
|---|---|---|
| 3 | Orange Juice | 40.0 |
| 12 | Highlighter | 25.0 |
| category | sales |
|---|---|
| Stationery | 1065.0 |
| Snacks | 935.0 |
| Drinks | 825.0 |
| Bakery | 750.0 |
| id | name | price |
|---|---|---|
| 12 | Highlighter | 25.0 |
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