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
- Open the SQL editor in PostHog
- Click into the editor
- Enter a query that counts events by name
- Click Run
- Read the results table
- Sort the output by total, highest first
Interactive 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
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.
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.
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.
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.
Related guides
Continue with more step-by-step guides for getting work done.
How to create a trends insight in PostHogBuild a PostHog trends insight that charts the task_created event over the last 14 days, then name it and save it so you can reopen it later.
How to create a funnel in PostHogBuild a PostHog funnel from signed_up to task_created to task_completed, set the last 14 days, and save it to see where users drop off between steps.
How to add an insight to a dashboard in PostHogAdd a saved PostHog insight to a dashboard in a few clicks: open it from All insights, choose Add to dashboard, and pin it to your overview.
How to add an annotation in PostHogLearn how to add an annotation in PostHog: open Annotations, write a note such as a launch, and save it so the date is marked on your charts.
How to compare time periods in PostHogCompare two periods in PostHog: set Last 7 days on a saved insight, then use Compare to previous period to see both series on one chart.
How to create a cohort in PostHogCreate a dynamic cohort in PostHog that groups people by a person property such as plan, so you can reuse the group across insights and analysis.
Create interactive demos like this one — free, no coding required.
Start for free →