Module 5 · Advanced queries

Save reusable queries with views

By the end you will be able to save a query with a name and read its current data.

About 12 min

0 of 52 lessons

Before you start, you need

Break queries into steps with CTEs · Filter rows with WHERE

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.

Minimal syntax
CREATE VIEW name AS
SELECT columns FROM table_name WHERE condition;

See it in action

We want to reuse the list of available products.

SQL
CREATE VIEW available AS
SELECT id, name, stock
FROM products
WHERE stock > 0;

SELECT id, name, stock
FROM available;
Example result: 7 rows
idnamestock
1Mouse18
2Keyboard7
3Monitor 244
5Headphones11
7Monitor 272
8SSD Drive9
9Cable USB-C30
  1. CREATE VIEW stores the definition of available. The second statement reads the result of that query.
  2. The view exposes only the chosen columns and always applies the availability condition.
  3. It is not a snapshot: if stock changes in products, querying the view applies the rule again. The variant checks that change.
  4. 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

Not started

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.

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

Not started

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.

Key takeaways

  • An ordinary view stores a query.
  • Querying the view reads its sources again.
  • The view can expose only the columns needed.

Next: Number rows with ROW_NUMBER