Write SQL with PostHog AI

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_subscriptions table?"
  • "Show me 5 sample rows from hubspot_contacts"

Try it

Select a prompt to try it out in the PostHog app:

Tips for better results

  • Reference table names – If you know the table (e.g. stripe_charges or events), 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.

Community questions

Was this page useful?

Questions about this page? or post a community question.