How to segment data extensions in Salesforce Marketing Cloud using SQL queries (SFMC segmentation – part 2/3)​

How to segment data extensions in Salesforce Marketing Cloud using SQL queries (SFMC segmentation – part 2/3)​

This is the second part of a 3-part series on segmentation in Salesforce Marketing Cloud (SFMC):

If you want to create a campaign in Salesforce Marketing Cloud (SFMC) that sets you apart, there is a big chance you’ll need to face a complex segmentation process. One of the solutions that SFMC proposes is SQL, Structured Query Language, which is quite an advanced method of segmenting. The creation of queries in SFMC offers a possibility to filter the information in DEs and target in a more sophisticated way.

In short, a query is an activity of getting relevant information from one or multiple data extensions and saving it to a target data extension. The process of SQL query creation is a task that requires some level of experience and time. Moreover, the process of a query activity execution in the SFMC environment can take up to 30 minutes – though this would be the case only for very complex queries.

A 3-step guide to segmenting data in SFMC using SQL queries

Step 1. Create a Data Extension

You need to create a target data extension, which you would like to use for your future campaign. For instance, the data extension below contains four fields that need to be populated with specific information from other data extensions:

SQL Query data extension creation in Salesforce Marketing Cloud

Step 2. Create a SQL Query 

In order to create a SQL Query, you should follow the steps below:

  • Go to ‘Journey Builder’
  • Click on ‘Automation Studio’
  • Go to ‘Activities’ and click on ‘Create Activity’
  • Choose ‘SQL Query’ from the options
Creation on SQL query activity in Automation Studio in Salesforce Marketing Cloud

Now you can start writing a SQL query:

  • use ‘select’ from where you choose the field names you would like to define

Note: They have to be separated by a comma except for the last field. 

  • choose ‘from’, where you define from which DE you are extracting the data
  • validate syntax to check for the mistakes in the query

Note: Only selected fields that exactly match the name of the columns in your target DE will be populated.

basic SQL query scenario created by DESelect

How can you create a (slightly) more advanced SQL queries in SFMC?

SQL queries can be very advanced to fulfill all the requirements of a specific campaign. There are many possibilities to adapt the query to your needs.

For instance: 

  • You can rename fields using ‘as’. (e.g. your initial DE contains a field ‘email’ but your target DE ‘email address’)
creating a basic SQL query for a segmentation scenario
  • Use ‘where’ to indicate you will specify criteria. (e.g. you have a field with a type of a company and you would like to filter only on the ‘prospect’ type)
simple SQL Query example in Salesforce Marketing Cloud
  • You can combine criteria using ‘and’, ‘or’ and ‘not’ (e.g. you would like to filter not only on the ‘prospect’ companies but also on the ones who are classified as ‘clients’ in the ‘type’ field).
example of a simple SQL query created in Salesforce Marketing Cloud

Do you want to know how to filter on data in DE’s in SFMC without SQL? Read our previous article on how to filter on data using data filters in SFMC!

For creating even more advanced segmentation, you can also use joins. The ‘join’ clause is used for combining data from multiple DEs. Here are four different kinds of joins:

Visual representation of different JOINs in DESelect
  • (Inner) join: Returns records that have matching values in both tables
  • Left (outer) join: Return all records from the left table, and the matched records from the right table
  • Right (outer) join: Return all records from the right table, and the matched records from the left table
  • Full (outer) join: Return all records when there is a match in either left or right table

Note: The ‘on’ clause is used to describe how the DEs are related to each other, usually based on an ID or sometimes an Email Address.

Inner JOIN using SQL in Salesforce Marketing Cloud

Don’t want to bother with joins and writing SQL queries manually? You can also create advanced segments with simple drag-and-drop, using DESelect.

Step 3. Target Data Extension

After your query is finished and it is validated you can move on to the next step. Here, you need to find your target data extension, which you want to populate with the query. Then decide how you want to add the data: 

  • Appending data means adding new data to target data extension 
  • Updating the data means matching data when it is found or adding new data if there is none
  • Overwriting means replacing or overwriting the current data in the target data extension

Should you use SQL queries in Salesforce Marketing Cloud?

If you want to do more advanced segmentation on data extensions in SFMC then SQL queries can be an option for you. They allow a lot of freedom and can cover very specific needs. However, it also means that you need to have sufficient technical skills to write those queries. Otherwise, you would need to ask for the help of more technical colleagues (e.g. those in the IT department), which slows down the process of campaign creation or consider a drag-and-drop alternative like DESelect. 

Latest Articles

  • Top 10 Marketing Operations Predictions for 2025

    We’re excited to unveil our Top 10 Marketing Operations Predictions for 2025. The exponential growth of marketing automation, the impact of AI on strategy and execution, and the drive to become more intentional and data-driven means we’re going to see a lot of interesting dynamics and progress in marketing. As 2024 closes out, we took a step back to think about what the future holds for marketing operations and came up with our first-ever list of predictions. Let’s dive in!
  • The Ultimate Guide to Salesforce Marketing Cloud Advanced Edition

    At the Marketing Keynote at Dreamforce 2024, Salesforce announced an exciting expansion to its suite of marketing solutions — the Marketing Cloud Advanced Edition, which builds upon the existing Marketing Cloud Growth Edition and offers even more powerful tools to help small and medium-sized businesses (SMBs) engage their customers in a more personalized and efficient way. This article explores the features, benefits, and impact of Advanced Edition and what it means for SMBs striving to innovate.
  • Unlocking the Potential of Salesforce Data Cloud: A Comprehensive Guide

    Introduction to Salesforce Data Cloud In recent years, Salesforce has been making a strong push into the Customer Data Platform (CDP) space with Salesforce Data Cloud. Data Cloud enables businesses to manage and leverage their customer data across multiple touchpoints, enabling marketers to leverage their data better than ever. By bringing together data from various […]
  • Zandra Marquez, Senior Customer Success Manager

    Company Culture Meet our team: Senior Customer Success Manager, Zandra Marquez We have a superstar in our Customer Success Team and her name is Zandra: a true gem with top-tier Salesforce Marketing Cloud knowledge. Get her take on her DESelect journey →   Zandra started at DESelect as a Customer Success Manager, where she quickly […]

Join our newsletter to receive updates and helpful SFMC guides.

Reach the most targeted<br/> audiences in half the time

Reach the most targeted
audiences in half the time

Deselect icon
Subscribe to the Newsletter
Receive Salesforce Marketing Cloud tips,
tricks, and DESelect updates

By clicking the “Subscribe now” button, you agree to the DESelect Terms of Use and Privacy Policy.