Skip to contentSkip to navigationSkip to topbar
On this page

Marketing Campaigns V2 Segmentation Query Reference


This document is a reference for the V2 Segmentation API. The V2 Segmentation API allows you to create and manage segments using a subset of SQL that includes statements and operations necessary to define any segment. In addition, this version of the Segmentation API exposes the contact_data and event_data tables to enable SQL-compatible queries.


General Query Format

general-query-format page anchor
1
SELECT contact_id, updated_at
2
FROM <table_reference> [ AS <table_alias> ]
3
[ [ INNER ] JOIN <table_reference> ON contact_id ]
4
[ WHERE <predicate> ]

In the previous example, the contact_id and updated_at columns are selected in the query. These two columns must be selected, and aggregate function MAX and its alias can be selected part of Last Event queries. Other than those columns, no additional columns can be selected. When joining two tables, they must be joined on the contact_id column only because it is the foreign key used to connect the contact_data and event_data tables.

All Available Query Keywords

all-available-query-keywords page anchor
  • SELECT
  • FROM
  • JOIN (defaults to INNER JOIN)
  • INNER JOIN
  • ON
  • WHERE

Query Operators for [predicate]

query-operators-for-predicate page anchor
  • [NOT] IN
  • IS [NOT]
  • AND
  • OR
  • NOT
  • =
  • >
  • >=
  • <
  • <=
  • !=
  • +
  • -
  • *
  • /
  • %

contact_data(required)

contact_datarequired page anchor
Column NameData Type
CONTACT_IDVARCHAR(36)
EMAILVARCHAR(254)
PHONE_NUMBER_IDVARCHAR
EXTERNAL_IDVARCHAR(254)
ANONYMOUS_IDVARCHAR(254)
FIRST_NAMEVARCHAR(50)
LAST_NAMEVARCHAR(50)
ALTERNATE_EMAILSVARCHAR ARRAY
ADDRESS_LINE_1VARCHAR(100)
ADDRESS_LINE_2VARCHAR(100)
CITYVARCHAR(60)
STATE_PROVINCE_REGIONVARCHAR(50)
POSTAL_CODEVARCHAR(60)
COUNTRYVARCHAR(55)
LIST_IDSVARCHAR ARRAY
CREATED_ATTIMESTAMP
UPDATED_ATTIMESTAMP
EMAIL_DOMAINSVARCHAR ARRAY
PHONE_NUMBERVARCHAR(24)
WHATSAPPVARCHAR(254)
LINEVARCHAR(254)
FACEBOOKVARCHAR(254)
UNIQUE_NAMEVARCHAR(254)
CUSTOM_FIELDS *Each custom field will appear as a column on the CONTACT table based on its name. Valid data types are: VARCHAR, NUMBER, TIMESTAMP

event_data(optional)

event_dataoptional page anchor

Event data that can be optionally be used to enhanced segments

Column nameData type
CONTACT_IDVARCHAR(36)
EVENT_SOURCEVARCHAR(256)
EVENT_TYPEVARCHAR(256)
TIMESTAMPTIMESTAMP
DATAJSON BLOB. Numeric and String type data
  • click - Whenever a recipient clicks one of the Click Tracked links in your email. In the Email History, SendGrid displays the date, time, and the URL for the link that was clicked.
  • blocked - When your IP address has been blocked by an ISP or messaging organization. Blocks are less severe than bounces and do not result in permanent suppressions: subsequent sends to blocked email addresses are not automatically suppressed.
  • bounce - The receiving server could not or would not accept the message. If a recipient has previously unsubscribed from your emails, your attempt to send to them is bounced.
  • deferred - The recipient mail server asked SendGrid to stop sending emails so fast.
  • delivered - The accepted response generated by the recipients' mail server.
  • dropped - Twilio SendGrid will drop an email when the contact on that email is in one of your suppression groups, the recipient email previously bounced, or that recipient has marked your email as spam.
  • group_resubscribe - When a recipient resubscribes themselves to a suppression group.
  • group_unsubscribe - Whenever a recipient unsubscribes from a suppression group.
  • open - The response generated by a recipient opening an email.
  • processed - Requests from your website, application, or mail client via SMTP Relay or the API that SendGrid processed.
  • spamreport - Whenever a recipient marks your email as spam and their mail server tells us about it.
  • unsubscribe - Whenever a recipient unsubscribes from your emails.

Event_Data JSON Struct Example

event_data-json-struct-example page anchor
(information)

Info

Combining more than one singlesend_id and mc_auto_id/mc_auto_step_id in a single statement using an AND clause is not allowed. In order to segment contacts that have events corresponding to more than one single sends and automations use the JOIN clause. See Multi-Engagement Events for examples.

1
{
2
"payload": {
3
"unique_args": {
4
"mc_auto_id": "02bc41ca-07f9-11eb-9c1f-422f9e0f60d1",
5
"mc_auto_step_id": "0478e207-07f9-11eb-9c1f-422f9e0f60d1",
6
"singlesend_id": "b1ff0108-0994-11eb-915a-7ab0feb0ce95",
7
},
8
},
9
}

Supported Functions and examples

supported-functions-and-examples page anchor

Example query

current_timestamp

The following query will return all contacts that were created before this run of the segment:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE created_at < CURRENT_TIMESTAMP

timestampadd(interval, count, timestamp)

timestampaddinterval-count-timestamp page anchor

timestampadd allows you to specify a time interval, a number of those intervals as a count, and a timestamp that you can use to compare Date fields against.

Parameters

ParameterSupported values
intervalyear, quarter, month, day, hour, minute, second
countany integer (negative or positive)
timestampA supported date/time format (RFC3339(link takes you to an external page)), an existing date field, or a custom field of type Date

Example Query

timestampadd(interval, count, timestamp)

The following query will return all contacts that were created before one year prior to 2020-10-15 6PM GMT:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE created_at > timestampadd(day, 1, '2020-10-15T18:00:12Z')

array_contains(field_name, array)

array_containsfield_name-array page anchor

array_contains checks for at least one of the values provided in an array. It does not hard check for the presence of all the values or an exact match of the values given. It also doesn't support pattern-matches like '%gmail.com%' as array values. array_contains will instead check for the exact string, including pattern match characters. For example, when searching '%gmail.com%' the % characters are considered part of the string literal.

Parameters

ParameterSupported values
field_name"alternate_emails", "list_ids", "email_domains"
arrayalternate emails, list IDs, and email domains that correspond to the specified field_name as strings

Example Query

array_contains(field_name, array)

The following query will return all contacts with email domains equal to gmail.com or yahoo.com:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE array_contains(email_domains, ['gmail.com', 'yahoo.com'])

contains_word(word_value, field_name)

contains_wordword_value-field_name page anchor

contains_word is a custom function provided by the Marketing Campaigns V2 Segments API. contains_word accepts two arguments: a word_value that will be searched for in the field_name values stored for each contact.

The field_name you specify must be of type Text, and the value stored in field_name must be a comma-separated string. For example, if the function evaluates the word_value alice against a first_name field where the value is bob,alice,sue, it will find alice and return the contact. However, if the first_name value is stored as bobalicesue, bob.alice.sue, or any other non comma separated string, alice will not be found, and the contact will not be returned. This is important when thinking about how to enter the string values associated with your contacts. The check is case sensitive.

  • All special characters and spaces within the comma separated string will be included when looking for a match. For Example, a field with a value of bob,(alice),sue and a match value of (alice) will return a contact
  • Single quotes in a field value must be escaped in the match value using double single quotes. For Example, a field value of bo'b,alice will require a match value of bo''b.

Parameters

ParameterSupported values
word_valueA string
field_nameAny String type fields in your contact data or custom fields. Stored strings must be comma separated.

Example Query

The following query will return all contacts where first_name contains the word bob :

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE contains_word('bob', first_name)

Sample Queries for Different Kinds of Segments

sample-queries-for-different-kinds-of-segments page anchor

Segmentation Examples From All Contacts

segmentation-examples-from-all-contacts page anchor
(information)

Info

Segmentation on a list is supported by providing a list ID value to the optional parent_list_ids field in the v2 API request. This parameter currently accepts only one list ID. Segmentation of contacts from multiple list ids is supported through the use of array_contains function. See example below.

All contacts:

1
SELECT contact_id, updated_at
2
FROM contact_data

All contacts with first_name 'Dave':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE first_name = 'Dave'

All contacts where state_province_region is 'Colorado':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE state_province_region = 'CO'

All contacts with primary email with the substring 'gmail.com':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE email like '%gmail.com%'

All contacts with a text type custom field my_text_custom_field value 'abc':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE my_text_custom_field = 'abc'

All contacts with primary email with 'gmail.com' as domain name, and a text type custom field my_custom_field value 'abc':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE email like '%gmail.com' and my_custom_field = 'abc'

All contacts with a number type custom field my_number_custom_field value 12:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE my_number_custom_field = 12

All contacts with a date type custom field my_date_custom_field value `2021-01-01T12:46:24Z':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE my_date_custom_field = '2021-01-01T12:46:24Z'

All contacts where alternate email is equal to 'alternate@gmail.com':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE array_contains(alternate_emails, ['alternate@gmail.com'])

All contacts where alternate email is equal to 'alternate@gmail.com' or 'alternate2@gmail.com':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE array_contains(alternate_emails, ['alternate@gmail.com','alternate2@gmail.com'])

All contacts present in a specific list "list_id":

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE array_contains(list_ids, ['02bc41ca-07f9-11eb-9c1f-422f9e0f60d1'])

All contacts present in either of the list_ids:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE array_contains(list_ids, ['02bc41ca-07f9-11eb-9c1f-422f9e0f60d1', '02bc41ca-07f9-11eb-9c1f-422f9e0f62e4', '042a8e48-6e31-11eb-a8bc-7656c249c550'])

All contacts with specific email domain(s) gmail.com:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE array_contains(email_domains, ['gmail.com'])

All contacts where created_at is after 2021-01-01 12 PM GMT:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE created_at > '2021-01-01T12:00:00Z'

All contacts where created_at is equal to 2021-01-01 12 PM GMT:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE created_at = '2021-01-01T12:00:12Z'

All contacts with external id that starts with '123':

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE external_id like '123%'

When a contact's reserved fields are not set during creation, they default to a NULL value. Not setting a value for custom fields when creating a contact will save the contact without those custom fields. A segment query using these fields does not return contacts having NULL as a value. Additional conditions may be used to specify the field is null so that contacts having a null value will be included in the segment.

All contacts where first_name does not equal Dave, also including contacts without a value for first_name:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE first_name != ‘Dave' OR first_name is null

All contacts where my_custom_field does not contain abc, also including contacts without a value for my_custom_field:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE my_custom_field like ‘%abc%' OR my_custom_field is null

Only JOIN and INNER JOIN are allowed and INNER JOIN will be internally converted to JOIN. LEFT JOIN and RIGHT JOIN are restricted for performance, and the same functionality can be achieved using JOIN. The examples below show how LEFT JOIN / RIGHT JOIN can be replaced with JOIN and UNION.

The following queries return all contacts that have an event "delivered" for a particular Single Send or whose state is Colorado. While both queries return the same set of contacts, using JOIN and UNION is more performant than using LEFT JOIN.

LEFT JOIN (not supported)

1
SELECT c.contact_id, c.updated_at
2
FROM contact_data c
3
LEFT JOIN event_data e
4
ON c.contact_id = e.contact_id
5
WHERE e.event_source = 'mail'
6
AND e.event_type = 'delivered'
7
AND e.DATA:payload.unique_args.singlesend_id = '042a8e48-6e31-11eb-a8bc-7656c249c550'
8
OR c.state_province_region = 'CO'

JOIN and UNION

UNION can be used instead of "OR" to return contacts based on distinct criteria that requires use of a JOIN.

For example, a query for contacts that were 'delivered' a Single Send OR the contacts whose state_province_region is in "CO" can be written as follows:

1
SELECT c.contact_id, c.updated_at
2
FROM contact_data c
3
JOIN event_data e
4
ON c.contact_id = e.contact_id
5
WHERE e.event_source = 'mail'
6
AND e.event_type = 'delivered'
7
AND e.DATA:payload.unique_args.singlesend_id = '042a8e48-6e31-11eb-a8bc-7656c249c550'
8
UNION
9
SELECT contact_id, updated_at
10
FROM contact_data
11
WHERE c.state_province_region = 'CO'

Engagement and Multi-engagement Examples

engagement-and-multi-engagement-examples page anchor

All contacts that have at least one event ( i.e. an attempt was made to send them an email):

1
SELECT c.contact_id, c.updated_at
2
FROM contact_data c
3
JOIN event_data e ON c.contact_id = e.contact_id

All contacts that have opened emails from two different single sends:

1
SELECT e2.contact_id, c2.updated_at
2
FROM event_data e2
3
INNER JOIN (
4
SELECT c1.contact_id, c1.updated_at
5
FROM contact_data AS c1
6
INNER JOIN event_data e1 ON c1.contact_id = e1.contact_id
7
WHERE e1.event_source = 'mail'
8
AND e1.event_type = 'open'
9
AND e1.DATA:payload.unique_args.singlesend_id = '02bc41ca-07f9-11eb-9c1f-422f9e0f60d1'
10
) AS c2 ON c2.contact_id = e2.contact_id
11
WHERE e2.event_source = 'mail'
12
AND e2.event_type = 'open'
13
AND e2.DATA:payload.unique_args.singlesend_id = '00163f67-7211-4363-ab4e-12dd6f313b3a'

All contacts that have opened ANY single sends within 3 days:

1
SELECT c.contact_id, c.updated_at
2
FROM contact_data as c
3
JOIN event_data as e on c.contact_id = e.contact_id
4
WHERE e.event_source = 'mail'
5
AND e.event_type = 'open'
6
AND e.DATA:payload.unique_args.singlesend_id is not null
7
AND e.timestamp >= timestampadd(day, -3, current_timestamp())

All contacts that have both of the two events - click and open:

1
SELECT e2.contact_id, c2.updated_at
2
FROM event_data e2
3
INNER JOIN (
4
SELECT c1.contact_id, c1.updated_at
5
FROM contact_data AS c1
6
INNER JOIN event_data e1 ON c1.contact_id = e1.contact_id
7
WHERE e1.event_source = 'mail' AND e1.event_type = 'open'
8
) AS c2 ON c2.contact_id = e2.contact_id
9
WHERE e2.event_source = 'mail' AND e2.event_type = 'click'

All contacts that have both of the two events - bounce and deferred for a particular automation:

1
SELECT e2.contact_id, c2.updated_at
2
FROM event_data e2
3
INNER JOIN (
4
SELECT c1.contact_id, c1.updated_at
5
FROM contact_data AS c1
6
INNER JOIN event_data e1 ON c1.contact_id = e1.contact_id
7
WHERE e1.event_source = 'mail'
8
AND e1.event_type = 'bounce'
9
AND e1.DATA:payload.unique_args.mc_auto_id = '02bc41ca-07f9-11eb-9c1f-422f9e0f60d1'
10
) AS c2 ON c2.contact_id = e2.contact_id
11
WHERE e2.event_source = 'mail'
12
AND e2.event_type = 'deferred'
13
AND e2.DATA:payload.unique_args.mc_auto_id = '02bc41ca-07f9-11eb-9c1f-422f9e0f60d1'

All contacts that have no event data (i.e. all contacts with where there has been no attempt to send an email to):

1
SELECT c.contact_id, c.updated_at
2
FROM contact_data c
3
WHERE c.contact_id NOT IN (
4
SELECT e.contact_id
5
FROM event_data e
6
)

All contacts that have not opened any mail in the last two months worth of seconds from the time when the segment is run:

1
SELECT c.contact_id, c.updated_at
2
FROM contact_data c
3
WHERE c.contact_id NOT IN (
4
SELECT e.contact_id
5
FROM event_data e
6
WHERE e.event_source = 'mail'
7
AND e.event_type = 'open'
8
AND e.timestamp < timestampadd(MONTH, -2, CURRENT_TIMESTAMP)
9
)
10

All contacts that have been 'delivered' the ANY single sends but have not 'open' ANY single sends within 1 month:

1
SELECT c.contact_id, c.updated_at
2
FROM contact_data c
3
JOIN event_data AS e1 ON c.contact_id = e1.contact_id
4
WHERE e1.event_source = 'mail'
5
AND e1.event_type = 'delivered'
6
AND e1.DATA:payload.unique_args.singlesend_id is not null
7
AND c.contact_id NOT IN (
8
SELECT e2.contact_id
9
FROM event_data AS e2
10
WHERE e2.event_source = 'mail'
11
AND e2.event_type = 'open'
12
AND e2.DATA:payload.unique_args.singlesend_id is not null
13
AND e2.timestamp >= timestampadd(month, -1, current_timestamp()))
14
)

All contacts that have been 'delivered' the second email from automation but have not 'open' the email (mc_auto_id = 'fa3e0f6e-d397-11eb-87ce-22ffc6ed50f5' mc_auto_step_id = 'fc9ead9f-d397-11eb-92fe-02aa17a6534e'):

1
SELECT c.contact_id, c.updated_at
2
FROM contact_data c
3
JOIN event_data AS e1 ON c.contact_id = e1.contact_id
4
WHERE e1.event_source = 'mail'
5
AND e1.event_type = 'delivered'
6
AND e1.DATA:payload.unique_args.mc_auto_id = 'fa3e0f6e-d397-11eb-87ce-22ffc6ed50f5'
7
AND e1.DATA:payload.unique_args.mc_auto_step_id = 'fc9ead9f-d397-11eb-92fe-02aa17a6534e'
8
AND c.contact_id NOT IN (
9
SELECT e2.contact_id
10
FROM event_data AS e2
11
WHERE e2.event_source = 'mail'
12
AND e2.event_type = 'open'
13
AND e2.DATA:payload.unique_args.mc_auto_id = 'fa3e0f6e-d397-11eb-87ce-22ffc6ed50f5'
14
AND e2.DATA:payload.unique_args.mc_auto_step_id = 'fc9ead9f-d397-11eb-92fe-02aa17a6534e'
15
)

Email Activity - Last Event Examples

email-activity---last-event-examples page anchor

There are three email activities that support last events: "Last Clicked", "Last Opened", and "Last Emailed". The last event queries are helpful to see the contacts whose last mail activity is in a certain period. The following queries are used to segment contacts based on their last mail activity.

All contacts that have not opened contacts since January of 2021 (last opened activity is before 2021):

1
SELECT c1.contact_id, c1.updated_at
2
FROM contact_data as c1
3
JOIN (
4
SELECT e1.contact_id, e1.max_timestamp
5
FROM (
6
SELECT e.contact_id, MAX(e.timestamp) max_timestamp
7
FROM event_data e
8
WHERE e.event_source = 'mail' and e.event_type = 'open'
9
GROUP BY e.contact_id
10
) as e1
11
WHERE e1.max_timestamp < '2021-01-01T00:00:00.000Z'
12
) as e2 on c1.contact_id = e2.contact_id

All contacts that last clicked an email within 7 days (last clicked activity is within 7 days):

1
SELECT c1.contact_id, c1.updated_at
2
FROM contact_data as c1
3
JOIN (
4
SELECT e1.contact_id, e1.max_timestamp
5
FROM (
6
SELECT e.contact_id, MAX(e.timestamp) max_timestamp
7
FROM event_data e
8
WHERE e.event_source = 'mail' and e.event_type = 'click'
9
GROUP BY e.contact_id
10
) as e1
11
WHERE e1.max_timestamp >= timestampadd(day, -7, current_timestamp())
12
) as e2 on c1.contact_id = e2.contact_id

All contacts that were last emailed in the past three months and last opened in the previous month (last emailed is within 3 months and last opened is within 1 month):

1
SELECT c1.contact_id, c1.updated_at
2
FROM (
3
SELECT c.contact_id, c.updated_at
4
FROM contact_data as c
5
JOIN (
6
SELECT e1.contact_id, e1.max_timestamp
7
FROM (
8
SELECT e.contact_id, MAX(e.timestamp) as max_timestamp
9
FROM event_data e
10
WHERE e.event_source = 'mail'
11
AND e.event_type = 'delivered' group by e.contact_id
12
) as e1
13
WHERE e1.max_timestamp >= timestampadd(month, -3, current_timestamp())
14
) as e2 on c.contact_id = e2.contact_id
15
) as c1
16
JOIN (
17
SELECT c.contact_id, c.updated_at
18
FROM contact_data as c
19
JOIN (
20
SELECT e1.contact_id, e1.max_timestamp
21
FROM (
22
SELECT e.contact_id, MAX(e.timestamp) as max_timestamp
23
FROM event_data e
24
WHERE e.event_source = 'mail'
25
AND e.event_type = 'open' group by e.contact_id
26
) as e1
27
WHERE e1.max_timestamp >= timestampadd(month, -1, current_timestamp())
28
) as e2 on c.contact_id = e2.contact_id
29
) as c11 on c1.contact_id = c11.contact_id
30
(information)

Info

NOTE: Even if the aggregate function MAX is supported along with GROUP BY, the usage is restricted to last event queries only. MAX function accepts only the "timestamp" field from event_data and GROUP BY clause accepts only the "contact_id" field. An alias name can be used only for the aggregate function MAX, but not for "contact_id" or "updated_at".

JOINing More Than 2 Data Sets

joining-more-than-2-data-sets page anchor

Multiple data sets are allowed to be joined together if done in a specific manner even though SQL supports both JOINs expressed linearly and JOINs expressed using subqueries. Each data set's criteria must be listed alongside the corresponding table reference as it makes for better readability by having the queries nested explicitly by using subqueries. Following is an example of a segment query for both types. Here, the query defines a segment of all contacts that have the event of type 'processed' from the list of contacts which have the event of type 'delivered'.

Concatenated JOINs (not supported)

concatenated-joins-not-supported page anchor
1
SELECT c1.contact_id, c1.updated_at FROM event_data AS e2
2
JOIN contact_data AS c1 ON e2.contact_id = c1.contact_id
3
JOIN event_data as e1 ON e1.contact_id = e2.contact_id
4
WHERE e1.event_source = 'mail' AND e1.event_type = 'delivered' AND e2.event_source = 'mail' AND e2.event_type = 'processed'

You can represent this logic in a more readable way using JOINs with subqueries.

1
SELECT e2.contact_id, c2.updated_at
2
FROM event_data AS e2 JOIN (
3
SELECT c1.contact_id, c1.updated_at
4
FROM contact_data AS c1 JOIN event_data AS e1
5
ON c1.contact_id = e1.contact_id
6
WHERE e1.event_source = 'mail' AND e1.event_type = 'delivered') AS c2
7
ON c2.contact_id = e2.contact_id
8
WHERE e2.event_source = 'mail' AND e2.event_type = 'processed'
(information)

Info

NOTE: There is no reduction in functionality as both generate the exact same result.

When creating or changing a segment query using curl command, escape any single quotes present in any parameter:

1
SELECT contact_id, updated_at
2
FROM contact_data
3
WHERE first_name = '\''Dave'\''

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.