SQL variables allow you to create configurable, reusable queries by defining placeholders that can be dynamically replaced with different values.
Using variables in SQL queries
You must first create a variable in the variable sidebar pane. Once created, variables in PostHog SQL are defined using the {variables.<variable-name>}
syntax. When you write a query with variables, you'll be prompted to provide values for each variable.
For example, this query uses a variable to filter events by a specific date range:
SELECTevent,count() as countFROM eventsWHERE timestamp >= {variables.startdate}AND timestamp <= {variables.enddate}GROUP BY eventORDER BY count DESCLIMIT 10
When you run this query, you'll be prompted to provide values for startdate
and enddate
.


Variable types
Variables can be used for various data types:
- String: Text values like event names or property values
- Number: Numeric values for thresholds or limits
- Date: Timestamp values for time-based filtering
- List: Lists of values for IN clauses
- Boolean: True/false values for conditional logic