HogQL is our take on SQL (Structured Query Language), a language used to manage and access data. HogQL is effectively a wrapper around ClickHouse SQL, with a few tweaks, such as simplified event and person property access.
Is HogQL real SQL? Yes, it's a translation layer over ClickHouse SQL. You can use most ClickHouse features in HogQL, including JOINs and subqueries. To learn more about ClickHouse, and how it differs from traditional SQL databases, read PostHog's ClickHouse Manual.
HogQL is currently in public beta. This means it's not yet a perfect experience, and the language itself may still change. Follow along with the development here.
HogQL expressions
HogQL expressions can be used inside insights and dashboards as filters and/or breakdown values. When filtering or breaking down, select the "HogQL" tab, and add your expression.

SQL insights
SQL insights enables you to create and edit insights using full SQL queries instead of the UI. Clicking the {} button in the top right corner of an insight shows you the SQL schema used to generate the insight which you can edit.

Event explorer
The event explorer replaces the "Live Events" tab in PostHog. It provides more functionality including date filtering, event counts, HogQL queries, aggregations and column configuration.

Database
To display all the tables you can query, check out the new "Database" tab under "Data Management".

We're working hard on making all these public. Follow along in the relevant Github issue, or send us a support ticket if you want to get access.
API access
Will there be API pricing? The HogQL API is free to use while it's in the public beta, and we work out the details. After we launch for real, we plan to charge a competitive rate for heavy usage. Stay tuned.
To access HogQL via the PostHog API, make a POST request to /api/project/:id/query with the following JSON payload:
{"query": {"kind": "HogQLQuery", "query": "select * from events limit 100"}}
The response is in the format:
export interface HogQLQueryResponse {/** The input query */query?: string/** An array of result arrays */results?: any[][]/** Returned column types */types?: string[]/** Returned column names/aliases */columns?: string[]/** Generated HogQL query with expressions inlined */hogql?: string/** Generated ClickHouse query for debugging */clickhouse?: string}
While in the public beta, the response format may still change.
HogQL guide
Strings and quotes
Quotation symbols work the same way they would work with ClickHouse, which inherits from ANSI SQL:
- Single quotes (
') for Strings literals. - Double quotes (
") and Backticks (`) for DataBase identifiers.
For example:
SELECT * FROM events WHERE properties.`$browser` = 'Chrome'
Property access
To access a property stored on an event or person, just use dot notation. For example properties.$browser or person.properties.$initial_browser.
Nested property or JSON access, such as properties.$some.nested.property, works as well.
PostHog's properties include always include
$as a prefix, while custom properties do not (unless you add it).
Property identifiers must be known at query time. For dynamic access, use the JSON manipulation functions from below on the properties field directly.
Property types
If you have specified a type for an event or person property under "Data Management", it will be returned in this type. All other properties are returned as strings.
For example:
SELECT round(properties.$screen_width * properties.$screen_height / 1000000, 2) as `Screen MegaPixels` FROM events LIMIT 100
This works because $screen_width and $screen_height are both defined as numeric properties. Thus you can multiply them.
To cast a string property into a different type, use type conversion functions, such as toFloat().
You can find a full list of properties and their types in your data management tab.
Supported ClickHouse functions
The following ClickHouse functions are enabled. This list is ever expanding. Please submit a pull request if we're missing something obvious.
Type conversion
toInttoFloattoDecimaltoDatetoDateTimetoUUIDtoStringtoJSONStringparseDateTimeparseDateTimeBestEffort
Arithmetic
plusminusmultiplydivideintDivintDivOrZeromodulomoduloOrZeropositiveModulonegateabsgcdlcmmax2min2multiplyDecimaldivideDecimal
Arrays and strings in common
emptynotEmptylengthreverseinnotIn
Arrays
arrayrangearrayConcatarrayElementhashasAllhasAnyhasSubstrindexOfarrayCountcountEqualarrayEnumeratearrayEnumerateUniqarrayPopBackarrayPopFrontarrayPushBackarrayPushFrontarrayResizearraySlicearraySortarrayReverseSortarrayUniqarrayJoinarrayDifferencearrayDistinctarrayEnumerateDensearrayIntersectarrayReversearrayFilterarrayFlattenarrayCompactarrayZiparrayAUCarrayMaparrayFillarraySplitarrayReverseFillarrayReverseSplitarrayExistsarrayAllarrayFirstarrayLastarrayFirstIndexarrayLastIndexarrayMinarrayMaxarraySumarrayAvgarrayCumSumarrayCumSumNonNegativearrayProduct
Comparison
equalsnotEqualslessgreaterlessOrEqualsgreaterOrEquals
Logical
andorxornot
Type conversions
toInttoFloattoDecimaltoDatetoDateTimetoUUIDtoStringtoJSONStringparseDateTimeparseDateTimeBestEffort
Dates and times
toTimeZonetimeZoneOftimeZoneOffsettoYeartoQuartertoMonthtoDayOfYeartoDayOfMonthtoDayOfWeektoHourtoMinutetoSecondtoUnixTimestamptoStartOfYeartoStartOfISOYeartoStartOfQuartertoStartOfMonthtoLastDayOfMonthtoMondaytoStartOfWeektoStartOfDaytoStartOfHourtoStartOfMinutetoStartOfSecondtoStartOfFiveMinutestoStartOfTenMinutestoStartOfFifteenMinutestoTimetoISOYeartoISOWeektoWeektoYearWeekagedateDiffdateTruncdateAdddateSubtimeStampAddtimeStampSubnowNOWnowInBlocktodayyesterdaytimeSlottoYYYYMMtoYYYYMMDDtoYYYYMMDDhhmmssaddYearsaddMonthsaddWeeksaddDaysaddHoursaddMinutesaddSecondsaddQuarterssubtractYearssubtractMonthssubtractWeekssubtractDayssubtractHourssubtractMinutessubtractSecondssubtractQuarterstimeSlotsformatDateTimedateNamemonthNamefromUnixTimestamptoModifiedJulianDayfromModifiedJulianDaytoIntervalSecondtoIntervalMinutetoIntervalHourtoIntervalDaytoIntervalWeektoIntervalMonthtoIntervalQuartertoIntervalYear
Strings
lengthUTF8leftPadrightPadleftPadUTF8rightPadUTF8lowerupperlowerUTF8upperUTF8isValidUTF8toValidUTF8repeatformatreverseUTF8concatsubstringsubstringUTF8appendTrailingCharIfAbsentconvertCharsetbase58Encodebase58DecodetryBase58Decodebase64Encodebase64DecodetryBase64DecodeendsWithstartsWithtrimtrimLefttrimRightencodeXMLComponentdecodeXMLComponentextractTextFromHTMLasciiconcatWithSeparator
Searching in strings
positionpositionCaseInsensitivepositionUTF8positionCaseInsensitiveUTF8multiSearchAllPositionsmultiSearchAllPositionsUTF8multiSearchFirstPositionmultiSearchFirstIndexmultiSearchAnymatchmultiMatchAnymultiMatchAnyIndexmultiMatchAllIndicesmultiFuzzyMatchAnymultiFuzzyMatchAnyIndexmultiFuzzyMatchAllIndicesextractextractAllextractAllGroupsHorizontalextractAllGroupsVerticallikeilikenotLikenotILikengramDistancengramSearchcountSubstringscountSubstringsCaseInsensitivecountSubstringsCaseInsensitiveUTF8countMatchesregexpExtract
Replacing in strings
replacereplaceAllreplaceOnereplaceRegexpAllreplaceRegexpOneregexpQuoteMetatranslatetranslateUTF8
Conditional
ifmultiIf
Mathematical
epiexploglnexp2log2exp10log10sqrtcbrterferfclgammatgammasincostanasinacosatanpowpowerintExp2intExp10coshacoshsinhasinhatanhatan2hypotlog1psigndegreesradiansfactorialwidth_bucket
Rounding
floorceiltruncroundroundBankersroundToExp2roundDurationroundAgeroundDown
Maps
mapmapFromArraysmapAddmapSubtractmapPopulateSeriesmapContainsmapKeysmapValuesmapContainsKeyLikemapExtractKeyLikemapApplymapFiltermapUpdate
Splitting strings
splitByCharsplitByStringsplitByRegexpsplitByWhitespacesplitByNonAlphaarrayStringConcatalphaTokensextractAllGroupsngramstokens
Bit
bitAndbitOrbitXorbitNotbitShiftLeftbitShiftRightbitRotateLeftbitRotateRightbitSlicebitTestbitTestAllbitTestAnybitCountbitHammingDistance
Bitmap
bitmapBuildbitmapToArraybitmapSubsetInRangebitmapSubsetLimitsubBitmapbitmapContainsbitmapHasAnybitmapHasAllbitmapCardinalitybitmapMinbitmapMaxbitmapTransformbitmapAndbitmapOrbitmapXorbitmapAndnotbitmapAndCardinalitybitmapOrCardinalitybitmapXorCardinalitybitmapAndnotCardinality
URLs
protocoldomaindomainWithoutWWWtopLevelDomainfirstSignificantSubdomaincutToFirstSignificantSubdomaincutToFirstSignificantSubdomainWithWWWportpathpathFullqueryStringfragmentqueryStringAndFragmentextractURLParameterextractURLParametersextractURLParameterNamesURLHierarchyURLPathHierarchyencodeURLComponentdecodeURLComponentencodeURLFormComponentdecodeURLFormComponentnetloccutWWWcutQueryStringcutFragmentcutQueryStringAndFragmentcutURLParameter
JSON
isValidJSONJSONHasJSONLengthJSONArrayLengthJSONTypeJSONExtractUIntJSONExtractIntJSONExtractFloatJSONExtractBoolJSONExtractStringJSONExtractKeyJSONExtractKeysJSONExtractRawJSONExtractArrayRawJSONExtractKeysAndValuesRaw
Geo
greatCircleDistancegeoDistancegreatCircleAnglepointInEllipsespointInPolygon
Nullable
isNullisNotNullcoalesceifNullnullIfassumeNotNulltoNullable
Tuples
tupletupleElementuntupletupleHammingDistancetupleToNameValuePairstuplePlustupleMinustupleMultiplytupleDividetupleNegatetupleMultiplyByNumbertupleDivideByNumberdotProduct
Time window
tumblehoptumbleStarttumbleEndhopStarthopEnd
Distance window
L1NormL2NormLinfNormLpNormL1DistanceL2DistanceLinfDistanceLpDistanceL1NormalizeL2NormalizeLinfNormalizeLpNormalizecosineDistance
Other
isFiniteisInfiniteifNotFiniteisNaNbartransformformatReadableDecimalSizeformatReadableSizeformatReadableQuantityformatReadableTimeDelta
Supported aggregations
The following aggregations are enabled. This list is ever expanding. Please submit a pull request if we're missing something obvious.
Standard aggregate functions
countcountIfminminIfmaxmaxIfsumsumIfavgavgIfanyanyIfstddevPopstddevPopIfstddevSampstddevSampIfvarPopvarPopIfvarSampvarSampIfcovarPopcovarPopIfcovarSampcovarSampIf
ClickHouse-specific aggregate functions
anyHeavyanyHeavyIfanyLastanyLastIfargMinargMinIfargMaxargMaxIfavgWeightedavgWeightedIfgroupArraygroupUniqArraygroupArrayInsertAtgroupArrayInsertAtIfgroupArrayMovingAvggroupArrayMovingAvgIfgroupArrayMovingSumgroupArrayMovingSumIfgroupBitAndgroupBitAndIfgroupBitOrgroupBitOrIfgroupBitXorgroupBitXorIfgroupBitmapgroupBitmapIfgroupBitmapAndgroupBitmapAndIfgroupBitmapOrgroupBitmapOrIfgroupBitmapXorgroupBitmapXorIfsumWithOverflowsumWithOverflowIfdeltaSumdeltaSumIfdeltaSumTimestampdeltaSumTimestampIfsumMapsumMapIfminMapminMapIfmaxMapmaxMapIfskewSampskewSampIfskewPopskewPopIfkurtSampkurtSampIfkurtPopkurtPopIfuniquniqIfuniqExactuniqExactIfuniqHLL12uniqHLL12IfuniqThetauniqThetaIfsimpleLinearRegressionsimpleLinearRegressionIfcontingencycontingencyIfcramersVcramersVIfcramersVBiasCorrectedcramersVBiasCorrectedIftheilsUtheilsUIfmaxIntersectionsmaxIntersectionsIfmaxIntersectionsPositionmaxIntersectionsPositionIf