How to write a SQL query in PostHog

Learn how to write a SQL query in PostHog: enter a query in the SQL editor, run it, and read a table of your most frequent events over 14 days.

Quick answer

To write a SQL query in PostHog, open the SQL editor, click into the editor, and enter a query against the events table. Select event and count the rows, filter to the last 14 days, group by event, and order by total. Click Run to see the results table.

Steps at a glance

  1. Open the SQL editor in PostHog
  2. Click into the editor
  3. Enter a query that counts events by name
  4. Click Run
  5. Read the results table
  6. Sort the output by total, highest first

Interactive demo

Loading demo…

Summary

The SQL editor lets you query your PostHog event data directly instead of building an insight by hand. A short query can answer questions that would take several clicks in a chart builder.

Step-by-step guide

  1. Step 1

    Click the SQL editor

    Open a new SQL query. The editor sits at the top of the page, with a Run button, Variables and Filters above it. Click inside the editor so the cursor is ready for your query.

  2. Step 2

    Enter your SQL query in the editor

    Enter a query that selects each event name and counts it as total from the events table. Add a 14-day timestamp filter, group by event, and order by total descending. The query reads: SELECT event, count() AS total FROM events WHERE timestamp > now() - INTERVAL 14 DAY GROUP BY event ORDER BY total DESC.

  3. Step 3

    Click Run

    Click Run at the top left of the editor. While the query executes, the results area shows a progress indicator. Wait for it to finish.

  4. Step 4

    See your most frequent events

    The Results tab shows a table with an event column and a total column. The highest count comes first, so in this project task_created leads with 104. Use the arrows beside each column header to sort the table differently.

Why this matters

You want to know which events your product fires most before you build a funnel or dashboard. Writing one query gives you a ranked list of event names and counts for the last 14 days, so you can decide what to analyze next.

Frequently asked questions

  • Can I use the SQL editor without connecting a data source?

    Yes. The query reads from the events table that PostHog already holds, so you do not need to add a data source. The editor panel offers one if you want to bring in external data.

  • How do I see the same output as a chart?

    Next to the Results tab there is a Visualization tab. Open it after running the query to view the output differently.

Create interactive demos like this one — free, no coding required.

Start for free →