Creating Models from SQL

Creating a model from SQL allows you to define a custom query as the basis for your model. This is useful for complex transformations, multi-table joins, or when you need precise control over the underlying data.

When to Use This Method

Use "From SQL" when you need:

  • Complex transformations - CTEs, window functions, or custom calculations

  • Multi-table joins - Combine data from multiple tables into one model

  • Filtered base data - Apply WHERE clauses before model creation

  • Derived columns - Create calculated fields in the base query

  • Views or subqueries - Model data that doesn't exist as a single table

How to Create a Model from SQL

  1. Navigate to Models in the left sidebar

  2. Click Create Model

  3. Select From SQL

  4. Enter a model name (this will be the model's identifier)

  5. Choose a data source from the dropdown

  6. Write your SQL query in the editor

  7. Click Execute query to test

  8. Review results and click Create Model

Model Name

Enter a descriptive name for your model:

  • Use lowercase with underscores (e.g., active_customers, monthly_revenue)

  • Choose a name that reflects the business entity

  • Keep it concise but meaningful

Writing Your SQL Query

The SQL editor supports your data warehouse's SQL dialect. Write a query that returns the data you want to model.

Simple example:

Join example:

Complex transformation example:

Testing Your Query

Before creating the model:

  1. Click Execute query to run a test

  2. Review the results table showing sample data

  3. Check column names and data types

  4. Verify the data looks correct

The test shows:

  • Total row count

  • Execution time

  • First 10 rows of results

Error Handling

If your query fails, you'll see:

  • Error message from the database

  • Details about what went wrong

  • Suggestions for fixing common issues

Common errors:

  • Syntax errors - Check SQL syntax for your database

  • Missing tables - Verify table names and schemas

  • Permission errors - Ensure connection has read access

Important Notes

  • The SQL query becomes the model's base table

  • Dimensions and measures reference columns from your query

  • Changes to the query require recreating the model

  • Complex queries may impact query performance

Example Use Cases

Denormalized Customer View

Time-Based Aggregation

Filtered Subset

Last updated