Advanced Queries
Advanced Queries
Once you are comfortable with natural language queries, these advanced techniques will help you build more powerful analyses.
Parameterized Queries
You can add parameters to saved queries using the {{parameter_name}} syntax. When the query runs, a form appears asking for the parameter value.
For example, a saved query "Show me all orders for customer {{customer_id}} in the last {{days}} days" will prompt for a customer ID and number of days each time it runs.
Parameters work in both the natural language question and in raw SQL mode. They are especially useful for dashboard widgets that need to be filtered dynamically.
Query Variables
Set session variables in Settings > Query Preferences that are available to all your queries:
{{current_user_email}}-- your logged-in email{{today}}-- today's date{{start_of_month}}-- first day of the current month{{start_of_quarter}}-- first day of the current quarter
Use these in questions like "Show me my assigned tickets since {{start_of_month}}" for personalized queries.
Cross-Database Queries
On Pro and Enterprise plans, you can query across multiple connected databases in a single question. Prefix table references with the connection name:
"Compare total users in production.users with staging.users"
AI for Database will query both databases and combine the results. Note that cross-database queries may be slower due to network round trips.
Raw SQL Mode
Toggle SQL Mode in the query bar to write raw SQL directly. This gives you full control over the query while still benefiting from AI for Database's result formatting, charting, and dashboard features.
In SQL mode you can use CTEs, window functions, subqueries, and any SQL feature your database supports.
Scheduled Queries
Any saved query can be scheduled to run automatically. Navigate to the saved query and click Schedule. Choose a frequency (hourly, daily, weekly, monthly) and a delivery method:
- Email -- receive the results as a CSV or HTML table.
- Slack -- post the results to a Slack channel.
- Webhook -- send the results as a JSON payload to any URL.
Scheduled queries are the foundation of the alerting system. See Setup Alerts for more.
Performance Tips
- Use specific column names instead of
SELECT *when possible. - Add time range filters to avoid scanning entire tables.
- For large datasets, ask for aggregations rather than raw rows.
- Check the Query Plan tab on slow queries to identify missing indexes.