Blog / Measuring in PostHog and Stripe
HogQL: querying your PostHog events in SQL, with examples
HogQL is PostHog's SQL dialect, a layer over ClickHouse SQL. Where to write it, the tables, six growth queries, the API limits and the traps.
HogQL is PostHog's SQL dialect: you write SELECT ... FROM events and query your raw events, persons and sessions directly. PostHog's SQL docs describe it as "effectively a wrapper around ClickHouse SQL," with simpler access to event and person properties. You can run it in the SQL editor, inside regular insights as an expression, or through the API. Below: where each one fits, the tables you will use, six queries for a SaaS funnel, and the traps that return wrong numbers without an error.
HogQL, SQL, Hog: the names
PostHog launched HogQL as a public beta on June 8, 2023. The docs have since dropped the name: posthog.com/docs/hogql now redirects to /docs/sql, and the page on Hog calls HogQL "the former name of our SQL-like query language." The name survives in one place: an API request sends the query with "kind": "HogQLQuery". The SQL docs explain why: "HogQL is the name we use for SQL access internally."
Do not confuse it with Hog, a separate programming language that runs PostHog's realtime destinations. If you want to query data, you want SQL.
Three places to write it
| Where | What you write | Use it for |
|---|---|---|
| SQL editor | A full query: SELECT, JOIN, WITH, GROUP BY | Questions the visual insights cannot answer. Saved as an insight, it goes on a dashboard |
| SQL expression | A single expression inside a Trends, Funnels, Retention or Paths insight | A custom breakdown or filter on an insight you already have |
| Query API | The same query, sent by POST | Scripts, notebooks, internal tools |
The expressions docs list where expressions work: filters, trends series, breakdowns, retention breakdowns, funnel aggregations, user paths, session replay, dashboards and the activity tab. Most growth questions start there. You keep the funnel or the trend you already trust and change one dimension.
The tables you will use
PostHog's own data is available as tables by default, per the PostHog source docs:
events: one row per event, withevent,timestamp,distinct_id,person_idandproperties.persons: one row per person, withid,created_at,propertiesandis_identified. It also has computed fields such as$virt_initial_channel_type, the channel type of the person's first visit.sessions: web sessions captured by the JavaScript SDK or snippet, with columns such as$entry_utm_source,$channel_type,$session_durationand$is_bounce.groups: accounts or companies, if you use group analytics.
Properties are read with dot notation (properties.$current_url, person.properties.email) or brackets for names with special characters (properties['$feature/new-onboarding']). PostHog's own properties start with $; yours do not, unless you added it. From events, person.properties reaches the person without writing a join, a shortcut the launch post names as one of HogQL's additions to ClickHouse SQL.
Six queries for a SaaS funnel
Each query uses two placeholder events, signed_up and project_created. Replace them with your signup event and your activation event. If you do not have clean events yet, start with a tracking plan. Run each query, then compare one number with the matching insight before you trust it.
1. Weekly active users on your core action
SELECT
toMonday(timestamp) AS week,
uniq(person_id) AS weekly_active_users
FROM events
WHERE event = 'project_created'
AND timestamp >= now() - INTERVAL 12 WEEK
GROUP BY week
ORDER BY week
Count people on the action that delivers value, not on $pageview. uniq is approximate and fast. The expressions docs describe uniqExact as exact but slower.
2. Activation rate by signup week
WITH signups AS (
SELECT person_id, min(timestamp) AS signed_up_at
FROM events
WHERE event = 'signed_up'
AND timestamp >= now() - INTERVAL 12 WEEK
AND timestamp < now() - INTERVAL 7 DAY
GROUP BY person_id
),
activations AS (
SELECT person_id, min(timestamp) AS activated_at
FROM events
WHERE event = 'project_created'
AND timestamp >= now() - INTERVAL 12 WEEK
GROUP BY person_id
)
SELECT
toMonday(s.signed_up_at) AS signup_week,
count() AS signups,
countIf(a.activated_at >= s.signed_up_at
AND a.activated_at <= s.signed_up_at + INTERVAL 7 DAY) AS activated_in_7_days,
round(100 * countIf(a.activated_at >= s.signed_up_at
AND a.activated_at <= s.signed_up_at + INTERVAL 7 DAY) / count(), 1) AS activation_rate_pct
FROM signups AS s
LEFT JOIN activations AS a ON a.person_id = s.person_id
GROUP BY signup_week
ORDER BY signup_week
The last seven days are excluded so every cohort has had its full week to activate. A funnel insight gives you the overall rate. This query gives you the trend by cohort, which is what tells you whether last month's onboarding change worked.
3. Median time to activation
WITH signups AS (
SELECT person_id, min(timestamp) AS signed_up_at
FROM events
WHERE event = 'signed_up' AND timestamp >= now() - INTERVAL 90 DAY
GROUP BY person_id
),
activations AS (
SELECT person_id, min(timestamp) AS activated_at
FROM events
WHERE event = 'project_created' AND timestamp >= now() - INTERVAL 90 DAY
GROUP BY person_id
)
SELECT
count() AS activated_users,
median(dateDiff('hour', s.signed_up_at, a.activated_at)) AS median_hours_to_activate
FROM signups AS s
INNER JOIN activations AS a ON a.person_id = s.person_id
WHERE a.activated_at >= s.signed_up_at
If the median is two hours, your onboarding emails on day three arrive after the decision is made. The docs note that median is approximate, which is fine at this scale.
4. Signups by first-touch channel
SELECT
p.$virt_initial_channel_type AS first_channel,
count(DISTINCT e.person_id) AS signups
FROM events AS e
INNER JOIN persons AS p ON e.person_id = p.id
WHERE e.event = 'signed_up'
AND e.timestamp >= now() - INTERVAL 90 DAY
GROUP BY first_channel
ORDER BY signups DESC
$virt_initial_channel_type is computed when the query runs and follows your custom channel types, per the tables docs. Add the activation CTE from query 2 and you get activation by channel, which is the number that decides where acquisition money goes. To tie channels to revenue, join Stripe through the data warehouse.
5. Your most active users, with their current email
SELECT
person_id,
argMax(person.properties.email, timestamp) AS email,
uniqExact(toDate(timestamp)) AS active_days_last_28
FROM events
WHERE event = 'project_created'
AND timestamp >= now() - INTERVAL 28 DAY
GROUP BY person_id
ORDER BY active_days_last_28 DESC
LIMIT 50
This list is for interviews and case studies. argMax takes the email from the most recent event, for a reason explained in the traps below.
6. An expression: work email versus personal email
This one goes in a Trends or Funnels breakdown, not in the editor. Choose the SQL expression option and paste:
if(extract(person.properties.email, '@(.*)$') IN ('gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'icloud.com'), 'personal', 'work')
Applied to your signup funnel, it shows whether business signups activate at a different rate. Extend the domain list with what you see in your data.
Traps that return wrong numbers
Person properties are frozen at capture time. The tables docs explain that person.properties on events holds the values when the event was captured. Events sent before identify have an empty person.properties.email, and a user who upgraded keeps the old plan on old events. For current values, use pdi.person.properties, which is slower because it joins the persons table. This is why query 5 uses argMax.
Join on person_id, never on distinct_id. One person can have several distinct IDs, one per device for example. The same docs warn that joining on distinct_id can miss events or count a person twice. Our identify article covers how distinct IDs merge.
Date literals use your project's timezone. The expressions docs say toDateTime('2026-07-17 06:42:00') is read in project time, not UTC. Prefer relative windows (now() - INTERVAL 7 DAY) or pass 'UTC' explicitly.
Results stop at 100 rows. SQL insights default to LIMIT 100, per the editor docs. A breakdown with 300 values silently shows 100. Write your own LIMIT.
WITH runs again each time you use it. The editor docs state that a WITH subquery is queried each time it is referenced. A CTE used twice costs twice. Always filter on timestamp inside it.
To let the dashboard date picker drive a saved query, put {filters} in the WHERE clause. The date range and property filters then apply without editing the SQL.
Running HogQL through the API
Send a POST to /api/projects/:project_id/query with a personal API key that has query read access and this body:
{ "query": { "kind": "HogQLQuery", "query": "SELECT event, count() FROM events GROUP BY event ORDER BY count() DESC" } }
The API docs set the limits:
| Limit | Value |
|---|---|
| Rows per query | 100 by default, up to 50,000 with an explicit LIMIT |
| Requests | 240 per minute, 2,400 per hour, per project |
| Concurrent queries | 3 |
| Execution time | 10 seconds |
The same page says /query is for ad hoc analysis and is not an export path. For recurring pulls it points to batch exports, and for production features to Endpoints, which save a query behind a named route. The SQL docs add that the SQL API is free during the public beta, with heavy usage to be priced later.
When to bother with SQL
If a Trends, Funnels or Retention insight answers the question, keep it: it is faster to build and anyone on the team can read it. The retention guide and the AARRR framework need very little SQL. Reach for HogQL when the question has a time condition between two events (activated within 7 days of signup), a cohort trend the funnel flattens, or a list of named people.
Then use the output to decide. Query 2 gives activation by week, query 4 by channel. Multiply the gap between your best and your worst channel by monthly signups and average revenue per customer, and you have a monthly dollar figure for fixing it. Compare it with the other gaps in your funnel and work on the largest one this week.
Frequently asked questions
Is HogQL the same as SQL in PostHog?
Yes. HogQL is the former name of PostHog's SQL dialect, and the public docs now call it SQL. The name survives in the API, where a query is sent with the kind HogQLQuery. It is a layer over ClickHouse SQL with simpler access to event and person properties, so many ClickHouse functions work.
Where do I write HogQL queries?
In three places. The SQL editor runs full SELECT queries that you can save as insights. SQL expressions go inside regular insights, as a filter, a breakdown or a funnel aggregation. The query API accepts the same queries over HTTP at /api/projects/:project_id/query with a personal API key.
Why does my HogQL query return only 100 rows?
PostHog adds a default LIMIT of 100 to SQL insights and API queries. Write your own LIMIT to get more. Through the API, an explicit LIMIT returns up to 50,000 rows per query. For bulk exports, PostHog points to batch exports or file downloads instead of the query endpoint.
Is the HogQL API free?
PostHog's SQL docs say the SQL API is free while it is in public beta, and that PostHog plans to charge for heavy usage later. Queries made with a personal API key also draw from an hourly read budget per project, which is larger on paid plans. Queries run inside the PostHog app are not affected.
Sources
- PostHog docs, SQL access in PostHog: https://posthog.com/docs/sql
- PostHog docs, SQL expressions: https://posthog.com/docs/sql/expressions
- PostHog docs, SQL editor: https://posthog.com/docs/data-warehouse/sql
- PostHog docs, Aggregations: https://posthog.com/docs/sql/aggregations
- PostHog docs, Supported ClickHouse functions: https://posthog.com/docs/sql/clickhouse-functions
- PostHog docs, PostHog data in the data warehouse: https://posthog.com/docs/data-pipelines/sources/posthog
- PostHog docs, Querying data with the API: https://posthog.com/docs/api/queries
- PostHog docs, Hog: https://posthog.com/docs/hog
- PostHog blog, Introducing HogQL (June 8, 2023): https://posthog.com/blog/introducing-hogql