The SQL editor lets you write queries in SQL (or your database’s native query language) when you need more control or complexity than the query builder provides.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/metabase/metabase/llms.txt
Use this file to discover all available pages before exploring further.
When to use the SQL editor
Consider using SQL when you need to:Complex joins
Join multiple tables with specific conditions or use self-joins
Subqueries
Nest queries or use common table expressions (CTEs)
Window functions
Calculate running totals, rankings, or moving averages
Database-specific features
Use features unique to your database like JSON functions or full-text search
Even if you don’t know SQL, someone on your team might share SQL-based questions that could be useful to you. You can still view, run, and modify filter values on SQL questions.
Starting a new SQL query
Create a new SQL query
Click + New in the main navigation and select SQL query (look for the console icon).
Select your database
Choose which database you want to query. Make sure you have the proper permissions to use the SQL editor for that database.
Write your query
Enter your SQL in the editor. The editor provides:
- Syntax highlighting
- Auto-completion for table and column names
- Multiple lines and formatting
SQL editor interface
The native query editor provides a clean interface for writing SQL:
Editor features
Syntax highlighting and auto-complete
Syntax highlighting and auto-complete
The editor recognizes SQL syntax and provides suggestions as you type. Press Tab to accept suggestions for table names, column names, and SQL keywords.
Run partial queries
Run partial queries
Highlight a portion of your SQL and run just that section. Useful for testing parts of complex queries or running multiple statements.
Format SQL
Format SQL
Click the document icon in the sidebar to automatically format your SQL for readability.
SQL formatting works for most databases but is not available for SQLite and SQL Server.
Reference models and questions
Reference models and questions
Use other saved questions and models in your queries by referencing them with special syntax:
{{#1234}} where 1234 is the question ID.Running queries
Full query execution
Click Run query or use the keyboard shortcut:- Windows/Linux: Ctrl + Enter
- Mac: ⌘ + Return
Running query selections
To run only part of your query:- Highlight the SQL you want to execute
- Click Run query or use the keyboard shortcut
SQL query formatting
Metabase can automatically format your SQL: Before formatting:Adding variables and filters
Make your SQL queries interactive by adding variables (also called parameters):- Field filters
- Basic variables
- Time grouping
Create smart filter widgets that connect to database columns:Field filters provide:
- Date pickers for date columns
- Dropdown menus for categorical data
- Auto-complete search boxes
- Proper SQL generation based on filter type
- Variable type: Field filter, text, number, or date
- Label: What users see above the widget
- Widget style: Dropdown, search box, or input box
- Default value: Optional starting value
Variables are required for SQL questions to work with dashboard filters. Add at least one variable to make your SQL question compatible with dashboard filtering.
Database-specific considerations
- PostgreSQL
- MongoDB
- MySQL / MariaDB
JSON operator workaroundUse This is required because JDBC interprets
?? instead of ? for JSON operations:? as a parameter placeholder.How Metabase executes SQL
The native query editor is designed for reading data, not modifying it. Avoid:- Multi-statement queries
- Stored procedures and function calls
- DDL statements (
CREATE,ALTER,DROP) - DML statements (
INSERT,UPDATE,DELETE)
Visualizing SQL query results
Selecting a visualization
Unlike query builder questions, SQL questions don’t automatically select a visualization. After running your query:- Click Visualization at the bottom of the screen
- Choose the chart type that fits your data
- Configure visualization settings
Pivot tables are currently not available for SQL questions. All other visualization types work as long as your result shape matches the chart requirements.
Visualization requirements
Different chart types require specific result structures:- Line charts
- Bar charts
- Pie charts
- One date/time column
- One or more numeric columns
Exploring SQL results
Explore results button
For SQL questions without parameters, you’ll see an Explore results button. This creates a new query builder question using your SQL results as the data source, letting you:- Add additional filters
- Create new summaries
- Join with other data
- Use the full drill-through capabilities
Limited drill-through
SQL questions have limited drill-through compared to query builder questions: Available:- Filter by clicking on data points
- Zoom in on time series and maps
- Some column header actions
- Drill down to unaggregated records
- Change time granularity
- Break out by categories or locations
Advanced SQL features
Using snippets
Using snippets
Save reusable SQL code as snippets:Snippets help you:
- Standardize common logic
- Share SQL code across questions
- Simplify complex queries
- Maintain consistency
Referencing saved questions
Referencing saved questions
Use other questions as data sources in SQL:Replace
1234 with the question ID from the URL.Setting values via URL
Setting values via URL
Pass variable values through the URL:Useful for:
- Direct links with filters applied
- Bookmarkable filtered views
- Integration with other tools
Connecting SQL questions to dashboards
To use a SQL question on a dashboard with filters:Add variables to your SQL
Include at least one variable (field filter or basic variable) in your query.
The type of dashboard filter you can use depends on the variable type in your SQL. Field filters offer more flexibility than basic variables.
Version history and collaboration
SQL questions include version history:- Metabase saves the last 15 versions
- View what changed in each version
- See who made changes and when
- Revert to previous versions if needed
Best practices
Use field filters
Prefer field filters over basic variables for better UI and more flexible dashboard filtering.
Add comments
Document complex SQL with comments to help future maintainers understand your logic.
Test incrementally
Build complex queries piece by piece. Use query selections to test parts independently.
Optimize for performance
Consider indexes, avoid SELECT *, and test queries on representative data volumes.
Format for readability
Use the formatting tool or manually format SQL for better maintainability.
Name columns clearly
Use aliases to give result columns clear, descriptive names.
Permissions
Your admin might grant:- Query builder only: Can’t use SQL editor
- Native query access: Can write SQL
- View SQL only: Can see generated SQL from query builder but can’t write SQL
Next steps
Native queries
Learn about database-specific query languages and capabilities
Query builder
Understand when to use the query builder instead of SQL
Dashboard filters
Connect SQL questions to dashboard filters using variables