# SQL Models


RudderStack's **SQL Models** feature lets you create models by defining custom SQL queries. You can then run these queries on your warehouse and send the resulting data to specific destinations. You can create as many models and reuse them with their corresponding [Reverse ETL sources]({{< ref "sources/reverse-etl/_index.md" >}}).

With this feature, you can:

- Build models with complex SQL queries using an intuitive UI.
- Manage views of all models synced to different destinations in one place.
- Reuse existing models in multiple connections.

## Create new model

1. Log in to the [RudderStack dashboard](https://app.rudderstack.com). 
2. From the left navigation bar, go to **Activate** > **Models** and click **New model**.
3. Configure the settings explained in the following sections:

### Select source

Select the warehouse source for which you want to create a model. RudderStack supports the Models feature for the following sources:

- [Amazon Redshift]({{< ref "sources/reverse-etl/amazon-redshift.md" >}})
- [Google BigQuery]({{< ref "sources/reverse-etl/google-bigquery.md" >}})
- [PostgreSQL]({{< ref "sources/reverse-etl/postgresql.md" >}})
- [Snowflake]({{< ref "sources/reverse-etl/snowflake.md" >}})
- [Databricks]({{< ref "sources/reverse-etl/databricks.md" >}})
- [Trino]({{< ref "sources/reverse-etl/trino.md" >}})

### Specify warehouse credentials

Specify the warehouse credentials to authenticate RudderStack. See the **Configure warehouse credentials** section of the [source-specific documentation]({{< ref "sources/reverse-etl/_index.md#supported-reverse-etl-sources" >}}) (for example, [Redshift]({{< ref "sources/reverse-etl/amazon-redshift.md#configuring-the-connection-credentials" >}})) for more details.

{{< info >}}
Once you select or add your warehouse credentials and click **Next**, RudderStack will validate them before you can proceed with the setup. See [FAQ](#faq) for more information on these validations.
{{< /info >}}

### Specify model name

- Assign a name to uniquely identify the source in the RudderStack dashboard.
- Select the source type to **Model** and click **Continue**.

### Configure model

1. Enter an optional model description.
2. Specify the custom SQL query in **Query** section.
3. Click **Run Query** to fetch the data preview.
4. Select the **Primary key** to use a column that uniquely identifies your warehouse records.

{{< warning >}}
You can set a primary key only after you run the SQL query successfully using the **Run Query** option.
{{< /warning >}}

{{< image src="images/data-pipelines/model-source-configuration.webp" alt="Model configuration" >}}

Note the following:

- The **Preview** section displays the preview of the 50 resultant rows in a paginated format.
- You can add single-line/multiline comments above the query. However, RudderStack supports only multiline comments at the end of the query. Support for single-line comments is coming soon.
- You don't need to add a semi-colon at the end of a query. RudderStack handles this automatically before running the query on the warehouse data.

### Review and complete setup

To make any changes to the warehouse credentials or audience configuration, click the edit icon present next to those sections.

{{< image src="images/data-pipelines/edit-model-configuration.webp" alt="Edit audience configuration" >}}

Once you have reviewed your configuration, click **Create source** to complete the setup.

### Connect model to destination

{{< customreadfile "/includes/retl/connect-retl-destination.md" >}}

## Update model

1. Go to the **Configuration** tab of the model source.
2. To change the model description, click the edit icon in the **Configuration settings**.
3. Click **Edit query** to change or update the model settings and primary key.

{{< warning >}}
Note that:

- You can set a primary key only after you run the updated SQL query successfully using the **Run Query** option.
- If you update the model's query, you also need to check and update the relevant destination mappings in the **Schema** tab of the connection page.
- After updating the configuration, the next sync will be a full sync. To validate if the query is running fine, you can manually trigger a full sync.
{{< /warning >}}

Go to the **Settings** tab to:

- Get your source ID.
- Change your warehouse credentials.
- [Set up custom alerts]({{< ref "monitor/alerts.md" >}}) for your Reverse ETL source.
- Delete the source permanently.

{{< warning >}}
You cannot delete a source that is connected to any destination.
{{< /warning >}}

{{< image src="images/retl-sources/source-settings.webp" alt="Edit source settings" >}}

## FAQ

#### **What do the three validations under Verifying Credentials imply?**

Once you proceed after entering the connection credentials, you will see the following validations under the **Verifying Credentials** option:

{{< image src="images/retl-sources/retl-credentials-validate.webp" alt="Validating credentials" >}}

These options are explained below:

- **Verifying Connection**: Indicates that RudderStack is trying to connect to the warehouse with the information specified in the connection credentials. 

{{< warning >}}
If this option gives an error, it means that one or more fields specified in the connection credentials are incorrect. Verify your credentials in this case.
{{< /warning >}}

- **Able to List Schema**: Checks if RudderStack is able to fetch all schema details using the provided credentials. 
- **Able to Access RudderStack Schema**: Checks if RudderStack has the required access to the `_RUDDERSTACK` / `_rudderstack` schema (depending on your warehouse). To create the schema, run all commands listed in the **Permissions** section of the respective Reverse ETL source documentation:

   - [BigQuery]({{< ref "sources/reverse-etl/google-bigquery.md#granting-permissions" >}})
   - [Databricks]({{< ref "sources/reverse-etl/databricks.md#granting-permissions" >}})
   - [PostgreSQL]({{< ref "sources/reverse-etl/postgresql.md#granting-permissions" >}})
   - [Redshift]({{< ref "sources/reverse-etl/amazon-redshift.md#granting-permissions" >}})
   - [Snowflake]({{< ref "sources/reverse-etl/snowflake.md#granting-permissions" >}})
   - [Trino]({{< ref "sources/reverse-etl/trino.md#granting-permissions" >}})

{{< warning >}}
If this option gives an error, verify if you have successfully created the `_RUDDERSTACK` / `_rudderstack` schema and given RudderStack the required permissions to access it.
{{< /warning >}}

