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
Navigate to Models in the left sidebar
Click Create Model
Select From SQL
Enter a model name (this will be the model's identifier)
Choose a data source from the dropdown
Write your SQL query in the editor
Click Execute query to test
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:
Click Execute query to run a test
Review the results table showing sample data
Check column names and data types
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