---
title: "Write an SQL query in Sitecore CDP"
slug: "write-an-sql-query-in-sitecore-cdp"
abstract: "How to write an SQL query to create a segment (Sitecore CDP)."
---
# Write an SQL query in Sitecore CDP

You can switch to advanced mode to write a complex SQL query if you know SQL and are familiar with the Sitecore CDP data model. We recommend switching to advanced mode if you want to apply regression logic, quartiles, and other advanced segmentation capabilities.

> [!note]
> **Before you begin**
> [Create a segment](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/introduction-to-batch-segmentation/using-advanced-mode-in-batch-segmentation-in-sitecore-cdp/create-and-add-rules-to-a-segment-in-sitecore-cdp.html "Create and add rules to a segment in Sitecore CDP") in basic mode before you write your SQL query in advanced mode.

To create a segment by writing an SQL query:

1. In the segment builder, under **Segment rules**, at the bottom right corner, click **Advanced Mode**
2. In the confirmation window, click **Yes, switch**. The **SQL editor** displays. Sitecore CDP converts the segment you created in basic mode into SQL statements that you can further edit.

    > [!note]
    > You must follow the [rules for writing SQL queries](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/introduction-to-batch-segmentation/using-advanced-mode-in-batch-segmentation-in-sitecore-cdp/rules-for-writing-sql-queries-in-the-sql-editor-in-sitecore-cdp.html "Rules for writing SQL queries in the SQL editor in Sitecore CDP") in the SQL editor as you write your SQL query.
    >
    > Amazon Athena, the query service that enables the SQL editor, has [query limitations](https://docs.aws.amazon.com/athena/latest/ug/service-limits.html) on complexity and resource usage. Simplify your queries where possible to avoid execution failures.

3. To copy and paste values and fields, on the **Data** tab, click the arrow ![cdp_seg_downarrow.png](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/cdp_seg_downarrow.png) icon on the entity block to see the available fields and values you can include in the SQL query. Click ![Copy_SQL.png](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/Copy_SQL.png) next to the field or value and paste it into the SQL editor.

    ![Viewing fields and values on the Data tab in the SQL editor.](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/Segment_builder_advanced.png)

4. To copy code snippets, on the **Snippets** tab, click the entity block, click the **Copy** link next to the code you want to copy, and paste the code snippet into the SQL editor.
5. Click **Save**. You can no longer switch back to basic mode after you save a segment in advanced mode.
6. To calculate the audience size of the segment and view a guest sample, [go to the **Calculate audience** tab](urn:resource:component:1574985/section-idm2348128760626) or [calculate it in the segment builder](urn:resource:component:1574985/section-idm1703481287675516).
7. [Schedule the segment](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/introduction-to-batch-segmentation/managing-segment-status-in-sitecore-cdp/schedule-a-segment-in-sitecore-cdp.html "Schedule a segment in Sitecore CDP") to be included in your organization's nightly segment build. You must do this to [use the segment](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/introduction-to-batch-segmentation/using-segments-within-sitecore-cdp.html "Using segments within Sitecore CDP") within Sitecore CDP.