---
schema_version: "1"
id: e757457155e12ba88cfe259978645d050c3408eea51ef5c550ecf9f18d2b0f4b
title: Retrieving and Segmenting Data with a SQL Query Activity
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/mktg/264-0-0/mc_as_using_the_query_activity.md
canonical_url: https://help.salesforce.com/s/articleView?id=mktg.mc_as_using_the_query_activity.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 879a7321c1d51513f39b765077151c0723469064237440e1c8849fa34153960e
description: Retrieve and unify data across data extensions and system data views with SQL query activities. For example, you can prepare a subscriber list, view open and send history, or review automation and journey health.
products:
  - Marketing
  - Marketing Cloud Engagement
release: "264"
---
# Retrieving and Segmenting Data with a SQL Query Activity

Retrieve and unify data across data extensions and system data views with SQL query activities. For example, you can prepare a subscriber list, view open and send history, or review automation and journey health.

A query activity is ideal for clean, structured datasets where audiences have already been created. If you want to unify and segment a large dataset, we recommend using your own internal data warehouse or an application such as Salesforce Customer Data Platform.

## Planning Your SQL Query Activity

Consider the data that you want to collect and how you plan to use it. Review our query examples for ideas on how to get started.

- [Data Views](https://help.salesforce.com/s/articleView?id=mktg.mc_as_data_views.htm&release=264.0.0&type=5 "Query system data views using Automation Studio for up to six months of Marketing Cloud Engagement subscriber and journey information.")
- [SQL Query Examples](https://help.salesforce.com/s/articleView?id=mktg.mc_as_sql_query_examples.htm&release=264.0.0&type=5 "Learn how to build effective SQL queries in Automation Studio by following use cases.")

## Building Your SQL Query Activity

In Automation Studio, build a SQL query activity that retrieves the data that you’re interested in, and add it to an automation. Then optimize your query so that it doesn’t time out and fail.

- [Build a SQL Query Activity](https://help.salesforce.com/s/articleView?id=mktg.mc_as_use_the_sql_query_activity.htm&release=264.0.0&type=5 "In Automation Studio, build a SQL query activity that retrieves the data that you’re interested in, and then add it to an automation.")
- [Optimizing a SQL Query Activity](https://help.salesforce.com/s/articleView?id=mktg.mc_as_optimize_the_query_activity.htm&release=264.0.0&type=5 "SQL query activities time out after 30 minutes. You can prevent timeouts and failures by optimizing your SQL query activities. If your query consistently runs longer than 10 minutes, we recommend using a different tool, such as Salesforce Customer Data Platform, to transform your data.")
- [SQL Reference](https://help.salesforce.com/s/articleView?id=mktg.mc_as_sql_reference.htm&release=264.0.0&type=5 "Get SQL query information for Automation Studio’s SQL Query activity at a glance.")
- [Trailhead: Query Data with SQL](https://trailhead.salesforce.com/content/learn/modules/marketing-cloud-data-management/query-data-with-sql)

## Monitoring Query Health

Build SQL query activities and automations to review the health of all your automations, including SQL query activities. You can also set up email or Slack notifications for automation errors so that you can react quickly to failures.

- [Data View: Automation Instance](https://help.salesforce.com/s/articleView?id=mktg.mc_as_data_view_automation_instance.htm&release=264.0.0&type=5 "Get a big-picture view of all automations within a tenant, and improve the efficiency and success rate of your automations by reviewing automation health. Query the _AutomationInstance data view in Automation Studio to prevent failures by identifying long-running processes, and fix failed automations by addressing the cause of failure.")
- [Data View: Automation Activity Instance](https://help.salesforce.com/s/articleView?id=mktg.mc_as_data_view_automation_activity_instance.htm&release=264.0.0&type=5 "Improve the efficiency and success rate of your automations by reviewing automation activity health. Query the _AutomationActivityInstance data view in Automation Studio to prevent failures by identifying activities that often fail or run long.")
- [Get Automation Studio Notifications](https://help.salesforce.com/s/articleView?id=mktg.mc_as_get_automation_studio_notifications.htm&release=264.0.0&type=5 "Add your email address to get notified when an automation run is skipped, encounters an error, or completes. You can also receive alerts through the Event Notification Service (ENS).")

## Fixing Failed Queries

Understand why your query failed and then troubleshoot it. If the query timed out, optimize it.

- [Automation Studio Errors](https://help.salesforce.com/s/articleView?id=mktg.mc_as_automation_studio_errors.htm&release=264.0.0&type=5 "Find out what Automation Studio error messages mean and how to resolve them. After applying the suggested action, run the activity or automation again to make sure that the error was addressed.")
- [Optimizing a SQL Query Activity](https://help.salesforce.com/s/articleView?id=mktg.mc_as_optimize_the_query_activity.htm&release=264.0.0&type=5 "SQL query activities time out after 30 minutes. You can prevent timeouts and failures by optimizing your SQL query activities. If your query consistently runs longer than 10 minutes, we recommend using a different tool, such as Salesforce Customer Data Platform, to transform your data.")
- [Restart a Suspended SQL Query Activity](https://help.salesforce.com/s/articleView?id=mktg.mc_as_restart_suspended_sql_query.htm&release=264.0.0&type=5 "When a SQL Query activity encounters a system error or fails 24 consecutive times, we suspend it. To find out why your query is suspended, check the action log.")

## Resources

There are other ways to retrieve data from Marketing Cloud Engagement, such as data extracts and API calls. If you want to unify and segment a large dataset, we recommend checking out Salesforce Customer Data Platform.

- [Knowledge Article: Best Practices for Gathering Data in Marketing Cloud Engagement](https://help.salesforce.com/s/articleView?id=000388879&type=1&language=en_US)
- [Data Extract Activity](https://help.salesforce.com/s/articleView?id=mktg.mc_as_use_a_data_extract_activity.htm&release=264.0.0&type=5 "The Automation Studio data extract activity creates one or more zipped files for your use outside of Marketing Cloud Engagement. It can also convert an XML file into a comma-delimited, tab-delimited, or pipe-delimited file for import into your account.")
- [Intro to Marketing Cloud Engagement APIs](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/apis-overview.html)
- [Trailhead: Automation Studio Activities](https://trailhead.salesforce.com/content/learn/modules/automation-studio-activities)
- [Build a Customer Data Platform with Salesforce](https://help.salesforce.com/s/articleView?id=data.c360_a_customer360_lm.htm&language=en_US)

- **[Build a SQL Query Activity](https://help.salesforce.com/s/articleView?id=mktg.mc_as_use_the_sql_query_activity.htm&release=264.0.0&type=5)**  
    In Automation Studio, build a SQL query activity that retrieves the data that you’re interested in, and then add it to an automation.
- **[Optimizing a SQL Query Activity](https://help.salesforce.com/s/articleView?id=mktg.mc_as_optimize_the_query_activity.htm&release=264.0.0&type=5)**  
    SQL query activities time out after 30 minutes. You can prevent timeouts and failures by optimizing your SQL query activities. If your query consistently runs longer than 10 minutes, we recommend using a different tool, such as Salesforce Customer Data Platform, to transform your data.
- **[Use Intermediate Tables to Optimize a Query](https://help.salesforce.com/s/articleView?id=mktg.mc_as_use_intermediate_tables_to_optimize_a_query.htm&release=264.0.0&type=5)**  
    Split large queries into smaller components to use with Automation Studio’s query activity.
- **[Data Views](https://help.salesforce.com/s/articleView?id=mktg.mc_as_data_views.htm&release=264.0.0&type=5)**  
    Query system data views using Automation Studio for up to six months of Marketing Cloud Engagement subscriber and journey information.
- **[SQL Query Examples](https://help.salesforce.com/s/articleView?id=mktg.mc_as_sql_query_examples.htm&release=264.0.0&type=5)**  
    Learn how to build effective SQL queries in Automation Studio by following use cases.
- **[SQL Reference](https://help.salesforce.com/s/articleView?id=mktg.mc_as_sql_reference.htm&release=264.0.0&type=5)**  
    Get SQL query information for Automation Studio’s SQL Query activity at a glance.
- **[Restart a Suspended SQL Query Activity](https://help.salesforce.com/s/articleView?id=mktg.mc_as_restart_suspended_sql_query.htm&release=264.0.0&type=5)**  
    When a SQL Query activity encounters a system error or fails 24 consecutive times, we suspend it. To find out why your query is suspended, check the action log.