# Views

> Give a query a name with CREATE VIEW, see why it stores no rows, and learn when you can write through a view and how CHECK OPTION guards it.

Subject: SQL | Lesson 38 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/views
Previous: [Normalization](https://codewithanimation.in/sql/normalization.md) | Next: [Indexes & B-Trees](https://codewithanimation.in/sql/indexes-and-b-trees.md)

## A named query

A **view** gives a name to a query. After `CREATE VIEW high_scorers AS SELECT ... WHERE points >= 30`, you can write `SELECT * FROM high_scorers` and use the view almost anywhere you could use a table.

A view stores no rows. The manual says it is not physically materialized: the query runs again every time the view is used. So a view always shows the current data.

## One definition, many users

A long join or a filter that many reports need can be written once and given a name. The manual calls liberal use of views a key aspect of good SQL design, because they hide the details of how the tables are built behind a stable interface. If the tables change, you update the view, and the code that reads it keeps working.

## A view follows its tables

```sql
CREATE VIEW high_scorers AS
  SELECT id, name, team, points
  FROM players
  WHERE points >= 30;
```

The view shows Asha (31) and Gus (40). Update Gus to 10 points in the `players` table, and the view now shows only Asha. You changed the table, not the view, and it kept up because it is just a query.

A simple view, with one table in `FROM` and no `DISTINCT`, `GROUP BY`, aggregates or set operations, is **automatically updatable**: you can `INSERT`, `UPDATE` and `DELETE` through it, and the change goes to the base table.

## Common mistake: Writing through a view

Insert Zed with 10 points through `high_scorers`, and the row goes into `players`, but the view does not show it, because 10 is below 30. The row vanished from the point of view of the person who inserted it. `WITH CHECK OPTION` prevents that: the insert is refused with `new row violates check option for view "high_scorers"`.

A view with `GROUP BY` cannot be written to: one row of `team_sizes` does not match one row of `players`. PostgreSQL says `cannot insert into view "team_sizes"` and explains that views containing GROUP BY are not automatically updatable. You can still make it writable with an `INSTEAD OF` trigger.

`CREATE OR REPLACE VIEW` can change the query, but it must keep the same columns, in the same order and types. It may only add new columns at the end.

An ordinary view is not the same as a **materialized view**, which does store its result and must be refreshed. Reading from a view is only as fast as its query.

## Tip: Views for reading, CHECK OPTION when you write

Use views to simplify queries and to expose only the columns and rows a user should see. If people insert or update through a filtering view, add `WITH CHECK OPTION` so the view cannot accept rows it cannot show.

## Try it: Change the table, change the view

Read the view, then drop Gus to 10 points. Insert Zed with 10 points, with and without WITH CHECK OPTION. Then try the team_sizes view.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/views

## Check yourself

### Question 1

How many rows does the view `high_scorers` store?

- A. None. The query runs each time the view is used
- B. The rows that matched when it was created
- C. A copy that is refreshed every night

Correct answer: A

A view is a saved query. It always reflects the current contents of the base tables.

- Why not B: That would be a snapshot. An ordinary view is not materialized.
- Why not C: Only a materialized view stores a result, and you refresh it yourself.

### Question 2

`high_scorers` shows points >= 30 and has no CHECK OPTION. You insert a row with 10 points through it. What happens?

- A. The row is added to the base table but is not visible in the view
- B. The insert is refused because 10 is too low
- C. The row is added and shows in the view

Correct answer: A

Without CHECK OPTION, commands may create rows that are not visible through the view.

- Why not B: Only WITH CHECK OPTION refuses such a row.
- Why not C: The view's WHERE filters it out, so it cannot be seen.

### Question 3

Why can you not insert into a view that uses GROUP BY?

- A. A row of the view stands for many base rows, so there is no single row to insert
- B. Views can never be written to
- C. GROUP BY views are stored read-only on disk

Correct answer: A

The manual lists GROUP BY among the things that make a view not automatically updatable.

- Why not B: Simple views over one table can be written to.
- Why not C: Views store nothing. The reason is how the rows map back to the table.

## Remember this

- A view is a named query. It stores no rows and always shows the current data.
- A simple view over one table is automatically updatable, and writes go to the base table.
- Without WITH CHECK OPTION a view can accept rows it cannot show. With it, the insert is refused.
- A view with GROUP BY, DISTINCT, aggregates or set operations is read-only. A materialized view is a different thing that stores its result.
