> For the complete documentation index, see [llms.txt](https://docs.predicthq.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.predicthq.com/integrations/third-party-integrations/snowflake/example-sql-queries-for-snowflake.md). Agents can also search and read this documentation through its [MCP server](https://docs.predicthq.com/~gitbook/mcp).

# Example SQL queries for Snowflake

Using SQL queries in Snowflake, you can manipulate the shared data to fit a variety of tasks. Feeding the data into analyses, dashboards, and machine learning models are only a few examples of what you can do with PredictHQ's data and SQL data manipulation in Snowflake.

The following SQL examples are based on the Sample Data Shares we provide. Here our database name is marketplace but in your account, it would be the custom name you chose when you added the sample data shares to your Snowflake account.

### Filter on labels

To find events with a `construction` label, filter on the `ARRAY` column `labels` using [ARRAY\_CONTAINS](https://docs.snowflake.com/en/sql-reference/functions/array_contains.html#array-contains):

```sql
SELECT event_id,
       title,
       category,
       phq_rank,
       labels
FROM   marketplace.predicthq.seattle_attended_events AS EVENTS
WHERE  ARRAY_CONTAINS('construction' :: variant, EVENTS.labels);
```

Example Results:

| **EVENT\_ID**      | **TITLE**                    | **CATEGORY** | **PHQ\_RANK** |                   **LABELS**                  |
| ------------------ | ---------------------------- | ------------ | :-----------: | :-------------------------------------------: |
| 8VmnC4u2qhHiYBnZQ7 | Zak World of Facades Seattle | conferences  |       0       |       \[ "conference", "construction" ]       |
| MPCit98VjwyuLikWZB | TecHome Builder Summit Fall  | conferences  |       42      |       \[ "conference", "construction" ]       |
| 7qmG3u42V3cVxNi9qD | Northwest Remodeling Expo    | expos        |       67      |  \[ "architecture", "construction", "expo" ]  |
| EuGFPmDdpLcHkfZwHa | Northwest Remodeling Expo    | expos        |       64      |     \[ "construction", "expo", "product" ]    |
| GxVVHErJWx83mizc9m | MCAA GreatFutures Forum      | conferences  |       28      | \[ "business", "conference", "construction" ] |

### Filter on latitude and longitude

Filtering the `GEOGRAPHY` column geo to find events around a 500-meter radius of [47.623257, -122.336498](https://goo.gl/maps/wxnLAufEpESLCcDj9) using [ST\_DISTANCE](https://docs.snowflake.com/en/sql-reference/functions/st_distance.html#st-distance) and [ST\_MAKEPOINT](https://docs.snowflake.com/en/sql-reference/functions/st_makepoint.html#st-makepoint-st-point):

```sql
SELECT event_id,
       title,
       category,
       phq_rank,
       labels,
       phq_rank,
       geo
FROM   marketplace.predicthq.seattle_attended_events AS EVENTS
WHERE  ST_DISTANCE(EVENTS.geo, ST_MAKEPOINT(-122.336498, 47.623257)) <= 500;
```

Example Results:

| **EVENT\_ID**      | **TITLE**                                                    | **CATEGORY**    | **PHQ\_RANK** |                 **LABELS**                 |                                  **GEO**                                 |
| ------------------ | ------------------------------------------------------------ | --------------- | :-----------: | :----------------------------------------: | :----------------------------------------------------------------------: |
| EYL7o5uE65TcQFpmQD | Sarah Christine and Taylor Tuke                              | concerts        |       0       |           \[ "concert", "music" ]          |       { "coordinates": \[ -122.34197, 47.62218 ], "type": "Point" }      |
| E4zrz8MTdb2UfNQfPi | South Lake Union Winter Market                               | festivals       |       54      |               \[ "festival" ]              |      { "coordinates": \[ -122.33621, 47.622634 ], "type": "Point" }      |
| X9rWASDN88DcRWhW8Z | AUG 26! An Evening w/ Sarah Christine The Collective Seattle | concerts        |       0       |           \[ "concert", "music" ]          | { "coordinates": \[ -122.34197340000003, 47.6221728 ], "type": "Point" } |
| FAmiEHQvanW3cfpRzB | Parlor Tricks Comedy Night                                   | performing-arts |       0       | \[ "concert", "music", "performing-arts" ] | { "coordinates": \[ -122.3421581, 47.62294079999999 ], "type": "Point" } |
| B7T6izj2gSJSr6Um5u | The Collective Patio Sets: Sarah Christine                   | community       |       0       |           \[ "concert", "music" ]          | { "coordinates": \[ -122.3421581, 47.62294079999999 ], "type": "Point" } |

### Filter with a polygon

Using the GEOGRAPHY column geo to find all events within a user defined area (eg: within a polygon) using [ST\_WITHIN](https://docs.snowflake.com/en/sql-reference/functions/st_within.html), [ST\_MAKEPOLYGON](https://docs.snowflake.com/en/sql-reference/functions/st_makepolygon.html), and [TO\_GEOGPRAPHY](https://docs.snowflake.com/en/sql-reference/functions/to_geography.html).

First, we define the shape we are interested in restricting the search to, as shown in the Seattle polygon map.

<figure><img src="https://2312509647-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtNhzHETmXsrWeVBndqqJ%2Fuploads%2FYmKGW1ig89ysQxsP2pBU%2Fseattle-polygon-snowflake-ex.png?alt=media&amp;token=7ea8f485-b734-42ba-9516-1d2063e0ee35" alt="Map of Seattle with a polygon drawn around the area used to restrict the event search"><figcaption></figcaption></figure>

The following query finds events inside the polygon:

```sql
SELECT seattle_events.event_id,
       title,
       category,
       phq_rank,
       geo
FROM marketplace.predicthq.seattle_attended_events seattle_events
WHERE st_within(seattle_events.geo, st_makepolygon(to_geography(
 ('LINESTRING(-122.30263710021973 47.62467785241324,-122.33980178833006 47.60442694445526,
  -122.34134674072267 47.58451555263637,-122.27954864501955 47.616925574159424,
  -122.30263710021973 47.62467785241324)'))))
```

Example Results:

| **EVENT\_ID**      | **TITLE**                                     | **CATEGORY** | **PHQ\_RANK** |                                  **GEO**                                 |
| ------------------ | --------------------------------------------- | ------------ | :-----------: | :----------------------------------------------------------------------: |
| 84xmSc634UTHwAkT5b | NWSL - OL Reign vs Kansas City Current        | sports       |       78      | { "coordinates": \[ -122.33163939999997, 47.5951518 ], "type": "Point" } |
| 3y8VCcVthX6dRk88aK | The Antlers                                   | concerts     |       43      |      { "coordinates": \[ -122.31962, 47.613938 ], "type": "Point" }      |
| BH7ffgQGiJx9yux2qd | Boris Brejcha                                 | concerts     |       59      |     { "coordinates": \[ -122.3322862, 47.5933082 ], "type": "Point" }    |
| BaqF5i5yYxBzFUGFLj | Kill The Noise                                | concerts     |       52      |     { "coordinates": \[ -122.3205827, 47.6137242 ], "type": "Point" }    |
| HpuMrnfLhdEgcBJu4q | Convent Bonfires, Bonsai Trees, and Good Job. | concerts     |       36      |     { "coordinates": \[ -122.3343786, 47.6005944 ], "type": "Point" }    |

Using the geo field of the events, these can be plotted on a map as shown in the results map.

<figure><img src="https://2312509647-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtNhzHETmXsrWeVBndqqJ%2Fuploads%2F4lBKHvkudzuy7JAJYzuo%2Fseattle-polygon-snowflake-results-ex.png?alt=media&amp;token=4d0116d3-8225-4b7b-ac6c-667830425e42" alt="Map of Seattle with the events returned by the polygon query plotted as points inside the polygon area"><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.predicthq.com/integrations/third-party-integrations/snowflake/example-sql-queries-for-snowflake.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
