# CREATE VIEW

[Read this page in HTML](https://www.scopedb.io/docs/reference/commands/statements/create-view)

Creates a named query that you can read like a table. Use a view to reuse a filter or calculation. The view reads the underlying data when queried.

## Syntax

```scopeql
CREATE [ OR REPLACE ] VIEW <name>
AS <query>
[ COMMENT = '<string_literal>' ]
```

## Parameters

* `<name>`: the view name.
* `<query>`: the ScopeQL query that supplies the view’s rows and columns.
* `OR REPLACE`: replaces an existing view with the new definition.
* `COMMENT`: an optional description of the view.

## Examples

Use the request table from the [query syntax sample data](https://www.scopedb.io/docs/reference/commands/stmt-query.md#sample-data) to create a view of failed requests:

```scopeql
CREATE VIEW failed_requests AS
    FROM reference_requests
    WHERE status >= 500
COMMENT = 'Requests that returned a server error'
```

Query the view:

```scopeql
FROM failed_requests
SELECT event_id, service, status
ORDER BY event_id
```

```text
+----------+----------+--------+
| event_id | service  | status |
+----------+----------+--------+
| req-2    | checkout | 504    |
+----------+----------+--------+
```

To include client errors as well, replace its definition:

```scopeql
CREATE OR REPLACE VIEW failed_requests AS
    FROM reference_requests
    WHERE status >= 400
COMMENT = 'Requests that returned a client or server error'
```

## Usage notes

* Views are read-only. They cannot be targets of `INSERT`, `UPDATE`, or `DELETE`; change the underlying table instead.
* View definitions cannot reference other views; expand the query to read base tables directly.
* These same definition rules apply when creating or updating a view through the [Catalog API](https://www.scopedb.io/docs/api-reference/catalog.md).
