Views and Derived Tables
Create View
Saving a Query Shape
A view stores a named SELECT statement. Later queries can read the view like a table while the data still comes from the base table.
Program
Play the script to create an order summary view and filter it with a selectable minimum total.
create_view.sql
Replay: real traced execution (multi-file project)
CREATE TABLE orders (region TEXT, amount INTEGER);
INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);
CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;
WITH params(min_total) AS (VALUES (20)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;
CREATE TABLE orders (region TEXT, amount INTEGER);
INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);
CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;
WITH params(min_total) AS (VALUES (15)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;
CREATE TABLE orders (region TEXT, amount INTEGER);
INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);
CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;
WITH params(min_total) AS (VALUES (22)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;
tables ← 1 row
1CREATE TABLE orders (region TEXT, amount INTEGER);2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);values this step1 rowtablesorders ← 3 rows
1CREATE TABLE orders (region TEXT, amount INTEGER);2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;values this step3 rowsordersviews ← 1 row
2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;4WITH params(min_total) AS (VALUES (20)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;values this step1 rowviewsresult ← 1 row
3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;4WITH params(min_total) AS (VALUES (20)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;values this step1 rowresult
tables ← 1 row
1CREATE TABLE orders (region TEXT, amount INTEGER);2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);values this step1 rowtablesorders ← 3 rows
1CREATE TABLE orders (region TEXT, amount INTEGER);2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;values this step3 rowsordersviews ← 1 row
2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;4WITH params(min_total) AS (VALUES (15)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;values this step1 rowviewsresult ← 2 rows
3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;4WITH params(min_total) AS (VALUES (15)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;values this step2 rowsresult
tables ← 1 row
1CREATE TABLE orders (region TEXT, amount INTEGER);2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);values this step1 rowtablesorders ← 3 rows
1CREATE TABLE orders (region TEXT, amount INTEGER);2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;values this step3 rowsordersviews ← 1 row
2INSERT INTO orders VALUES ('east', 12), ('east', 9), ('west', 18);3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;4WITH params(min_total) AS (VALUES (22)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;values this step1 rowviewsresult ← 0 rows
3CREATE VIEW region_totals AS SELECT region, SUM(amount) AS total FROM orders GROUP BY region;4WITH params(min_total) AS (VALUES (22)) SELECT region, total FROM region_totals WHERE total >= (SELECT min_total FROM params) ORDER BY region;values this step0 rowsresult
Follow the View Rows
ordersstarts with east12, east9, and west18.- The
region_totalsview groups orders by region. - The view rows are east
21and west18. - The default
min_totalis20. - Only east
21passes, so the output row iseast | 21. | region | source amounts | view total | kept at 20? | | --- | --- | --- | --- | | east | 12, 9 | 21 | yes | | west | 18 | 18 | no |
CREATE VIEW
`CREATE VIEW name AS SELECT ...` gives a query a reusable name.
view query
Reading from `region_totals` evaluates the saved SELECT over current table data.
parameter CTE
`params` holds the selectable minimum total used by the filter.
Exercise: create_view.sql
Reproduce the default row east | 21, then use the pinned min_total variants 15 and 22 to predict east | 21 plus west | 18, and then no rows.