Creating views

Last updated:

|

In the PostHog data warehouse, you can save your most used queries as views and reference them in subsequent queries.

Creating a view

Query views are created directly inside SQL insights. If the query has valid view characteristics, the Save as view button is enabled. When clicked, you are prompted to give the view a name which can then be referenced in other queries.

For a query to be a valid view, all fields being accessed must be aliased (with the SQL as keyword). The alias names are how you access the fields on the view. This also means that we cannot use SELECT *... type SQL syntax for a query we would like to be a view since none of the columns are aliased.

Note: Nested views are supported. You can create a view based on other views.

valid view

Extending PostHog models with views

Views are a powerful tool for extending existing PostHog models for easier data access. For example, if you wanted to associate your Stripe customer data with product usage data of your users, you would normally need to manually set up a join. With views, you can attach views to PostHog models so that you can directly access those fields on the PostHog table.

To link a view to a PostHog table, go to the data warehouse section, select the PostHog tab, and click Link table to view. Select your tables, keys to join, and press save. Once done, when you query that PostHog table, you can access the data from your view.

view link

Materializing views

Views can also be materialized and stored in the PostHog data warehouse, offering significant query performance benefits and improvements. You can learn more in the materializing views guide.

Questions? Ask Max AI.

It's easier than reading through 800 pages of documentation

Community questions

Was this page useful?

Next article

Accessing data using SQL

Strings and quotes Quotation symbols work the same way they would work with ClickHouse, which inherits from ANSI SQL: S ingle quotes ( ' ) for S trings literals. D ouble quotes ( " ) and B ackticks ( ` ) for D ata B ase identifiers. For example: Types Types (and names) for the accessible data can be found in the database , properties tabs in data management as well as in the SQL tab for external sources. They include: STRING (default) JSON (accessible with dot or bracket notation…

Read next article