SQL CREATE VIEW: save a query and use it like a table
A view is a query with a name. You write it once, say revenue per customer across three tables, and from then on you just select from the view as if it were a table. The result always reflects the data as it is right now.
In short: CREATE VIEW name AS SELECT … saves a query under a name. SELECT * FROM name runs it. DROP VIEW name removes only the saved query, never the data behind it.
What you should know first: joining tables with JOIN and grouping with GROUP BY. For the first exercise, SELECT with WHERE is enough.
Try it now
In this lesson you create views. After your statement runs, the check deliberately changes some data and reads your view again. What you see is what the view returns at that point.
Task Sales asks every day which products can ship right away. Create the view in_stock for them: name, category and price of every product with stock above 0. It has to be a view, not a table: after your statement, the check changes one product's stock and reads your view again.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
How CREATE VIEW works
Take a finished SELECT query and put CREATE VIEW name AS in front of it. The database doesn't run the query at that moment and doesn't store a result. It stores the query itself. Only when someone selects from the view does the query run, and it runs fresh every time.
An example from the practice shop: an order's value lives in order_items and has to be summed every time you need it. The view order_totals does that for you:
CREATE VIEW order_totals AS
SELECT o.order_id, o.order_date, o.status,
SUM(oi.quantity * oi.unit_price) AS total
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
GROUP BY o.order_id, o.order_date, o.status;
Now you query order_totals like any table, with WHERE, ORDER BY and everything else you know. Which valid orders were over 1,000?
SELECT order_id, order_date, total
FROM order_totals
WHERE status <> 'cancelled' AND total > 1000
ORDER BY total DESC;
| order_id | order_date | total |
|---|---|---|
| 1004 | 2026-02-03 | 1344 |
| 1009 | 2026-03-30 | 1284.4 |
| 1014 | 2026-06-02 | 1236 |
| 1008 | 2026-03-17 | 1067.45 |
The view's column names come from its SELECT, so every calculated column needs an alias. Without AS it would be called SUM(oi.quantity * oi.unit_price).
A view always shows current data
Change a line item and the view reflects it immediately. Raise the monitors in order 1009 from 2 to 5, and order_totals reports 2121.4 instead of 1284.4 for that order without you doing anything else. That's the difference from CREATE TABLE … AS SELECT, which makes a copy as of the moment you run it and goes stale with the first change.
Why views earn their keep
- Write long queries once: two joins and a GROUP BY live in the view, and everyone else writes
SELECT * FROM customer_revenue. - One definition for everybody: when "revenue excluding cancelled orders" sits in a view, sales and finance report the same number.
- Show only what's needed: a view can leave out columns such as salary. Microsoft lists exactly this as a purpose of views in SQL Server: people read data through the view without being granted access to the tables behind it.
Another exercise: a view with a join and a sum
This time a real report goes into the view. Build the query first, check it with Run, and only then put CREATE VIEW in front.
Task Management wants revenue per customer on demand, without joining three tables every time. Create the view customer_revenue with the columns customer (the customer's name) and revenue (sum of quantity times unit_price). Cancelled orders don't count, and customers without revenue don't need to appear. Afterwards the check cancels one order and queries the view just like a table: SELECT customer, revenue FROM customer_revenue.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Changing a view: DROP VIEW
SQLite has no CREATE OR REPLACE VIEW. To change what a view returns, drop it with DROP VIEW name and create it again. That only touches the saved query; no rows are lost. DROP VIEW IF EXISTS name doesn't raise an error when the view isn't there.
Task The view open_orders already exists (shown above the editor), but customer service needs more from it now. Replace it with a new view of the same name: order_id, order_date, status and customer (the customer's name) of every order with status open or shipped. Afterwards the check changes one status and reads your view.
open_orders 3 rows, 2 columns
| order_id | order_date |
|---|---|
| 1016 | 2026-06-12 |
| 1017 | 2026-06-13 |
| 1018 | 2026-06-14 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Common mistakes
CREATE TABLE instead of CREATE VIEW
CREATE TABLE in_stock AS SELECT … makes a copy. As soon as the check changes a stock level, a row is missing from it and the exercise says "After your changes: The check shows 10 rows, expected: 11 rows". Use CREATE VIEW.
Forgetting AS before SELECT
CREATE VIEW in_stock SELECT … gives a syntax error near "SELECT". AS goes between the name and the query.
The view already exists
A second CREATE VIEW with the same name fails with a message that the name already exists. The suggested IF NOT EXISTS only helps if the old view should stay as it is. To replace it, put DROP VIEW first.
CREATE OR REPLACE or DROP TABLE
CREATE OR REPLACE VIEW gives a syntax error near "OR" in these exercises, because SQLite doesn't have that form. DROP TABLE on a view is refused by SQLite, and the exercise tells you to use DROP VIEW.
Views in MySQL, PostgreSQL, SQL Server and SQLite
| Database | Replace a view | Change data through a view |
|---|---|---|
| MySQL | CREATE OR REPLACE VIEW | when each view row matches exactly one table row (no aggregates, DISTINCT or GROUP BY) |
| PostgreSQL | CREATE OR REPLACE VIEW, only with the same columns in the same order; new columns go at the end | automatic for simple views (one table, no GROUP BY, DISTINCT or LIMIT) |
| SQL Server | CREATE OR ALTER VIEW (SQL Server 2016 SP1 and later) | when the change touches columns of one table only |
| SQLite (these exercises) | DROP VIEW, then create it again | no, views are read-only |
Materialized views: with CREATE MATERIALIZED VIEW, PostgreSQL stores the result much like a table. That makes it quick to query, but it's no longer live: it only catches up when you run REFRESH MATERIALIZED VIEW name. SQL Server's counterpart is the indexed view. You create the view WITH SCHEMABINDING and add a unique clustered index, after which its result is stored in the database. Unlike PostgreSQL, SQL Server keeps that stored result up to date on every INSERT, UPDATE and DELETE against the tables, which can slow those changes down.
ORDER BY inside a view: SQL Server only allows it together with TOP (or OFFSET), and even then the order isn't guaranteed when you query the view. MySQL ignores a view's ORDER BY when the query on the view has its own. Sort in the query that reads the view.
Sources: MySQL Reference Manual (CREATE VIEW Statement, Updatable and Insertable Views), PostgreSQL documentation (CREATE VIEW, CREATE MATERIALIZED VIEW), Microsoft Learn (CREATE VIEW, Create Indexed Views), sqlite.org (CREATE VIEW).
Frequently asked questions
Is a view slower than a table?
A regular view is just a name for a query, so it runs exactly as fast as that query. If the query behind it is heavy, so is every SELECT on the view.
Can a view use another view?
Yes. A view can read from tables and from other views, for example a monthly summary built on order_totals. Keep the chains short, or it gets hard to trace where a number comes from.
What happens to the view when I change the table?
New rows and changed values show up right away, because the view reads the table on every query. Changing the table's structure depends on the database. SQL Server, for instance, recommends running sp_refreshview on affected views afterwards, and won't let you change a table referenced by a view WITH SCHEMABINDING in a way that breaks the view.
How do I list the views in a database?
In SQLite they're in the sqlite_master table with type = 'view'. MySQL, PostgreSQL and SQL Server list them in information_schema.views. In the exercises they appear above the editor.