Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions contents/docs/data-warehouse/query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ availability:
enterprise: full
---

import { ProductScreenshot } from 'components/ProductScreenshot'
import { CalloutBox } from 'components/Docs/CalloutBox'
import { ProductScreenshot } from "components/ProductScreenshot";
import { CalloutBox } from "components/Docs/CalloutBox";

PostHog provides the full flexibility of SQL to query your data warehouse using the [SQL editor](/docs/data-warehouse/sql).

To create a query, go to the [SQL editor](https://us.posthog.com/sql). Here you can see and search the schema of all available sources and [PostHog tables](/docs/data-warehouse/sources/posthog) as well as saved views.

<ProductScreenshot
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/w_1600,c_limit,q_auto,f_auto/Clean_Shot_2025_08_26_at_13_07_22_2x_f4bfc84ae3.png"
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/w_1600,c_limit,q_auto,f_auto/Clean_Shot_2025_08_26_at_13_07_22_2x_f4bfc84ae3.png"
imageDark="https://res.cloudinary.com/dmukukwp6/image/upload/w_1600,c_limit,q_auto,f_auto/Clean_Shot_2025_08_26_at_13_07_45_2x_751fc31300.png"
alt="SQL editor"
alt="SQL editor"
classes="rounded"
/>

Expand All @@ -29,9 +29,9 @@ Here you can:
3. Modify your query using [SQL commands](/docs/data-warehouse/sql) and [functions](/docs/data-warehouse/sql/useful-functions) as needed to get the data you want like `select amount / 100 as dollar_charge, status from stripe.prod.charge`.

<ProductScreenshot
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/w_1600,c_limit,q_auto,f_auto/Clean_Shot_2025_08_26_at_13_19_11_2x_b1d7ce399f.png"
imageDark="https://res.cloudinary.com/dmukukwp6/image/upload/w_1600,c_limit,q_auto,f_auto/Clean_Shot_2025_08_26_at_13_18_51_2x_e62db75b67.png"
alt="Querying a source in PostHog"
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/w_1600,c_limit,q_auto,f_auto/Clean_Shot_2025_08_26_at_13_19_11_2x_b1d7ce399f.png"
imageDark="https://res.cloudinary.com/dmukukwp6/image/upload/w_1600,c_limit,q_auto,f_auto/Clean_Shot_2025_08_26_at_13_18_51_2x_e62db75b67.png"
alt="Querying a source in PostHog"
classes="rounded"
/>

Expand All @@ -41,7 +41,7 @@ Here you can:

<CalloutBox icon="IconInfo" title="Having trouble writing SQL?" type="fyi">

[PostHog AI](/docs/posthog-ai) can help write SQL for you. Just click the icon in the corner of your screen and ask it to write or tweak your query for you.
[PostHog AI](/docs/posthog-ai) can help write SQL for you. Just click the icon in the corner of your screen and ask it to write or tweak your query for you.

</CalloutBox>

Expand All @@ -61,17 +61,21 @@ By clicking on the **Visualization** tab below the query, you can customize the
For example, with tables, you can add conditional formatting rules. These enable you to highlight cells based on their value and are set up in the **Conditional formatting** tab.

<ProductScreenshot
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/Clean_Shot_2025_07_01_at_13_01_00_2x_181ae75d28.png"
imageDark="https://res.cloudinary.com/dmukukwp6/image/upload/Clean_Shot_2025_07_01_at_13_02_28_2x_b3ba2d2d8a.png"
alt="Customizing a SQL visualization"
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/Clean_Shot_2025_07_01_at_13_01_00_2x_181ae75d28.png"
imageDark="https://res.cloudinary.com/dmukukwp6/image/upload/Clean_Shot_2025_07_01_at_13_02_28_2x_b3ba2d2d8a.png"
alt="Customizing a SQL visualization"
classes="rounded"
/>

## Saving queries as views

You can save frequently used queries as [views](/docs/data-warehouse/views) and reference them in subsequent queries. When saving a view, you can also assign it to a DAG (directed acyclic graph) to organize your data models by product area (like finance, marketing, or analytics) or by environment (like dev, staging, and prod). See the [creating views](/docs/data-warehouse/views) docs for more details.

## Querying multiple sources together

Much of the power of the data warehouse comes from combining multiple sources, like your Stripe or Hubspot data with your product analytics data. Two of the easiest ways of doing this are `WHERE IN` and `JOIN` SQL commands.
Much of the power of the data warehouse comes from combining multiple sources, like your Stripe or Hubspot data with your product analytics data. Two of the easiest ways of doing this are `WHERE IN` and `JOIN` SQL commands.

import QueryJoinExample from './_snippets/query-join-example.mdx'
import QueryJoinExample from "./_snippets/query-join-example.mdx";

<QueryJoinExample />

Expand All @@ -97,6 +101,6 @@ These metrics are helpful for optimizing queries, especially when working with l

## Optimizing queries

import OptimalQueries from '../_snippets/optimal-queries.mdx'
import OptimalQueries from "../_snippets/optimal-queries.mdx";

<OptimalQueries />
<OptimalQueries />
11 changes: 8 additions & 3 deletions contents/docs/data-warehouse/views/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ In the PostHog data warehouse, you can save your most used queries as **views**

## 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.
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](https://res.cloudinary.com/dmukukwp6/image/upload/v1710055416/posthog.com/contents/images/features/data-warehouse/valid-view.png)

## Organizing views with DAGs

Views are organized into directed acyclic graphs (DAGs) that represent the relationships and dependencies between your models. You can have multiple DAGs to separate your views by product area (like finance, marketing, or analytics) or by environment (like dev, staging, and prod).

When saving a view, you can select an existing DAG from the **Add to DAG** dropdown or create a new one by selecting **+ Create new DAG** and entering a name, description, and sync frequency. You can view and switch between DAGs in the **Data modeling** tab using the DAG selector.

## 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.
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](/docs/data-warehouse/sources/posthog), go to the [data warehouse section](https://app.posthog.com/data-warehouse/posthog), 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.

Expand All @@ -31,4 +37,3 @@ To link a view to a [PostHog table](/docs/data-warehouse/sources/posthog), go to
## 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](/docs/data-warehouse/views/materialize) guide.

Loading