# Views

Source: https://cubibase.com/docs/database/views

Also available: [Markdown version](https://cubibase.com/docs/database/views.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Save a SELECT as a view, or cache it as a materialized view. Views shape what readers see — joins, aggregates, renamed columns — without copying data.

## Creating a view

A view is one SELECT (or WITH … SELECT) query stored under a name. Querying the view runs the query against live data. The editor accepts a single statement up to 16,000 characters.

**Example · what the Console creates**

```sql
CREATE VIEW public.active_customers AS
SELECT id, name, plan
FROM public.customers
WHERE status = 'active';
```

- Standard views can be replaced in place (CREATE OR REPLACE) as long as column names and types stay compatible.
- Views over RLS-protected tables respect the querying role’s policies: the api role sees only the rows its policies allow.
- The Console lists each view with its definition and columns.

## Materialized views

A materialized view runs the query once and stores the result. Reads are fast and constant; the data is a snapshot until you refresh. CubiBase creates them WITH DATA, so they are usable immediately.

|  | View | Materialized view |
| --- | --- | --- |
| Data freshness | Always live | As of the last refresh |
| Read cost | Runs the query each time | Reads stored rows |
| Replace in place | Yes | No — drop and recreate |
| Refresh button | — | Yes: REFRESH MATERIALIZED VIEW |

> **note**: Refresh is manual in this release — press Refresh on the view (or call the control route) when you want new numbers. A refresh rebuilds the whole snapshot.

## The Data API select switch

Like tables, views start private. The Data API select switch grants SELECT on the view to the `api` role, so queries executed as that role — inside functions or other views — can read it. Turning the switch off revokes the grant immediately.

> **note**: The Data API serves tables at /rest/v1 in this release; views are not yet addressable as REST resources. Read a view from the SQL editor, or wrap it in a function.

> **warning**: A view can aggregate or join across rows a caller could not read directly. Grant select only when the combined result is meant to be visible, or add a WHERE on cubibase.current_user_id() inside the view.

## Additional resources

- [SQL editor](https://cubibase.com/docs/database/sql-editor) Read your views with a SELECT.
- [Policies](https://cubibase.com/docs/database/policies) Row rules that views inherit from their tables.
- [Indexes](https://cubibase.com/docs/database/indexes) Speed up the queries inside your views.
