Write SQL with PostHog AI
Contents
PostHog AI generates and fixes HogQL queries from natural language. Describe the data you want, and PostHog AI writes the query – including joins, CTEs, and subqueries – using your project's actual tables and columns.
How it works
PostHog AI explores your Data Warehouse schema to find the right tables and columns, writes the HogQL query with the correct syntax, joins, and filters, then runs it and returns results. It knows your schema – including external sources like Stripe, HubSpot, and any linked warehouse tables – so you can query across all your data without memorizing table names.
Fix broken queries
You can also paste a broken query and ask PostHog AI to fix it. It identifies syntax errors, missing columns, and incorrect joins, then returns the corrected query with an explanation of the error.
Explore your schema
Not sure what data is available? Ask PostHog AI to list your tables, describe their columns, or show sample data. This is useful when you're working with external sources that you haven't queried before.
Example prompts for schema exploration:
- "What tables do I have?"
- "What columns are in the
stripe_subscriptionstable?" - "Show me 5 sample rows from
hubspot_contacts"
Try it
Select a prompt to try it out in the PostHog app:
Write a SQL query to find the top 10 events by count this weekFix the syntax error in the query I have openList all available tables and their columnsWrite a query joining events with Stripe subscription dataFind users who triggered event X but not event Y in the last 30 daysCreate a CTE that calculates weekly active users by cohort
Tips for better results
- Reference table names – If you know the table (e.g.
stripe_chargesorevents), include it in your prompt - Describe the output shape – "Group by day and show a count" or "Return the top 10 by revenue" helps PostHog AI structure the query
- Ask about your schema first – Exploring your tables before querying helps you write better prompts
- Iterate on results – "Add a filter for paid users" or "Break this down by country" refines the query without starting over
Get started
To start writing SQL with PostHog AI, set up PostHog AI.