Before you start, you need
Save a query to reuse it
If you often use the list of products in stock, you can give it a name and reuse it. A view stores the query, so you do not have to write its whole filter every time.
CREATE VIEW available AS goes in front of the SELECT you want to store. Then you can read available with another query, as if it were a table.
The view does not store a snapshot of the rows. If the stock in products changes, querying it applies the condition again and shows the current data. It is different from a CTE: the view stays defined for other statements.
The lab sets up the database again on every attempt. That is why you must create the view and run its checks in the same attempt. The challenge includes an UPDATE line already written so you can watch a change; you will learn to write updates in their own lesson.
CREATE VIEW name AS
SELECT columns FROM table_name WHERE condition;See it in action
We want to reuse the list of available products.
CREATE VIEW available AS
SELECT id, name, stock
FROM products
WHERE stock > 0;
SELECT id, name, stock
FROM available;| id | name | stock |
|---|---|---|
| 1 | Mouse | 18 |
| 2 | Keyboard | 7 |
| 3 | Monitor 24 | 4 |
| 5 | Headphones | 11 |
| 7 | Monitor 27 | 2 |
| 8 | SSD Drive | 9 |
| 9 | Cable USB-C | 30 |
- CREATE VIEW stores the definition of available. The second statement reads the result of that query.
- The view exposes only the chosen columns and always applies the availability condition.
- It is not a snapshot: if stock changes in products, querying the view applies the rule again. The variant checks that change.
- For practice, query the view. Whether you can write through a view depends on the engine and on how the view is defined.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Save the in-stock query
Write the SQL statement that creates the view available_products with id, name and stock for products with stock greater than 0.
The requested structure is checked.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Save the in-stock products query under the requested name.
Add AS before SELECT.
Filter with WHERE stock > 0.
Solution explained
CREATE VIEW available_products AS SELECT id, name, stock FROM products WHERE stock > 0;The view wraps up a frequent query without duplicating data.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Check that the view reads the data again
Create inventory with id and stock for products whose stock is greater than 0. Query id and stock from the view before and after the UPDATE line already provided. Write all four steps in the same attempt.
We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
The view must store the availability rule, not a copy of some rows.
All the steps must go together, because each attempt resets the data.
After the UPDATE, query inventory again.
Solution explained
CREATE VIEW inventory AS SELECT id, stock FROM products WHERE stock > 0; SELECT id, stock FROM inventory; UPDATE products SET stock = 0 WHERE id = 1; SELECT id, stock FROM inventory;The second query on the view no longer includes product 1. The intermediate steps are shown separately.
Key takeaways
- An ordinary view stores a query.
- Querying the view reads its sources again.
- The view can expose only the columns needed.