> ## Documentation Index
> Fetch the complete documentation index at: https://forest-chore-open-api.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage SQL views

In SQL, a view is a virtual table based on the result-set of an SQL statement. Views can provide advantages over tables, such as:

* represent a subset of the data contained in a table (see also[ segments](https://docs.forestadmin.com/user-guide/collections/segments)).
* join and simplify many tables into a single virtual table.
* act as aggregated tables, where the database engine aggregates data (sum, average etc.) and presents the calculated results as part of the data.

<Info>
  Forest natively supports SQL views. If you have already implemented views, simply add [the associated models](https://docs.forestadmin.com/documentation/reference-guide/models/enrich-your-models#declaring-a-new-model) to display them on your interface.
</Info>

## Creating the SQL View

To create a view, we use `CREATE VIEW` statement.&#x20;

In the following example, we look for the **user's email**, **the number of orders** and **the total amount spent**.

```sql theme={null}
CREATE VIEW customer_stats AS
  SELECT customers.id,
    customers.email,
    count(orders.*) AS nb_orders,
    sum(products.price) AS amount_spent,
    customers.created_at,
    customers.updated_at
  FROM customers
    JOIN orders ON customers.id = orders.customer_id
    JOIN products ON orders.product_id = products.id
  GROUP BY customers.id;
```

## Adding the model

To display the SQL view on your Forest interface, you must add the associated Sequelize model in your application.

<Info>
  You must restart your server to see the changes on your interface.
</Info>

## Managing the view

Once your SQL view is implemented, you'll be able to filter, search, export and change the order of your fields.

<Warning>
  Most of the time SQL views are used as **read-only**. If this is the case, we recommend changing the CRUD permission in your [roles's settings](https://docs.forestadmin.com/user-guide/project-settings/teams-and-users/manage-roles).
</Warning>

<img src="https://mintcdn.com/forest-chore-open-api/DwOJ-XBdKEod-4Pc/images/legacy/javascript-agents/customer-stats-sql-view.png?fit=max&auto=format&n=DwOJ-XBdKEod-4Pc&q=85&s=bf8fa745e9d3bbdaa63cf022984ca666" alt="" width="2880" height="1596" data-path="images/legacy/javascript-agents/customer-stats-sql-view.png" />
