SQL variables

Last updated:

|Edit this page|

SQL variables enable 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 panel. 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:

SQL
SELECT
event,
count() as count
FROM events
WHERE timestamp >= {variables.startdate}
AND timestamp <= {variables.enddate}
GROUP BY event
ORDER BY count DESC
LIMIT 10

When you run this query, you'll be prompted to provide values for startdate and enddate.

SQL Editor variable example

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

Questions? Ask Max AI.

It's easier than reading through 675 pages of documentation

Community questions

Was this page useful?

Next article

Creating insights from sources

The data warehouse enables you to visualize and query data from external sources with PostHog's tools. Visualizing source data with trends After linking your source , you can use its data in a trend like you would event or person data. To do this, create a new insight , click the series, go to the Data warehouse tables tab, hover over the table you want, and click Select . The main difference from events is that you must identify 3 fields from the data warehouse data to visualize it: ID…

Read next article