Salesforce Marketing Cloud - SQL Features & Tips Part 2

After looking at the problem of time zones in queries with date values in the first part of this series, now on to a much-used method for merging data from multiple sources. With the UNION SQL function, subtleties also determine the correctness of results.

SQL in Salesforce Marketing Cloud — No mistakes, but still wrong results?

Case 2: Be careful when using UNION

The UNION function is used to merge data with a similar structure from two or more data extensions into a data table. Here is an example of a common query:

SELECT EmailAddress,
	SubscriberKey,  
	FirstName,
	LastName
	FROM [DataExtension1]
UNION
SELECT EmailAddress,   
	SubscriberKey,   
	FirstName,   
	LastName
	FROM [DataExtension2]

This query combines the fields from DataExtension1 and DataExtension2 into a new data extension. The UNION function automatically removes duplicate data records. However, there is also a pitfall to consider here: The number and order of fields in all UNION SQL queries must match.

Correctly handle different numbers of fields in data extensions for SQL queries

The solution is fairly simple. If a field is missing in one of the data extensions, you should either set a default value or specify that the field should remain empty. It is important that you do not simply omit a missing field such as firstName in the second data extension, as this will result in an error message. We will use an example to explain:

Let's assume that Data Extension 1 is structured as follows:

emailAddress | subscriberKey | firstName | lastName
And dataExtension2 has the fields in this order:

SubscriberKey | EmailAddress | LastName

You now want to merge the data from both data extensions and save it to DataExtension3, which has the same field layout as DataExtension1. To ensure that there are no issues, you should use the following query:

SELECT EmailAddress,
	SubscriberKey,
	FirstName,
    LastName
	FROM [DataExtension1]
UNION
SELECT EmailAddress,
	SubscriberKey,
	FirstName=NULL,
	LastName,
	FROM [DataExtension2]

Your result in Data Extension 3 will be as follows:

EmailAddress
SubscriberKey
FirstName
LastName
example@example.de
ABCDE12345
Max
Mustermann
beispiel@beispiel.de
VWXYZ67890
Musterfrau

Why is the order of fields so important in UNION queries?

Another special feature of using UNION is the order of the fields queried. This order must be exactly the same in all queries that belong to the UNION. Otherwise, the results won't be merged as desired, even though no error message is displayed. It is important to know that the order of the fields in the first query before the UNION command determines the order in which the fields are written to the target data extension. This order then remains the same for all subsequent queries in the UNION. We also have a small example of this:

For example, if you copy the order from the data extensions, but it is different, as in this example:

SELECT EmailAddress,
	SubscriberKey,
	FirstName,
	LastName
	FROM [DataExtension1]

UNION

SELECT SubscriberKey,   
	EmailAddress,   
	LastName,   
	FirstName = NULL

Then your result in Data Extension 3 would look like this:

EmailAddress
SubscriberKey
FirstName
LastName
example@example.de
ABCDE12345
Max
Mustermann
VWXYZ67890
beispiel@beispiel.de
‍Musterfrau

Therefore, make sure that the fields are queried in the same order in all queries to achieve the desired result.

UNION versus UNION ALL

For the sake of completeness, we would like to add the difference between UNION and UNION ALL. Both methods combine results from multiple queries. The key difference, however, is that, as already mentioned, UNION removes duplicate results, while UNION ALL shows all values, including duplicates.

We know that SQL isn't always easy to use in Salesforce Marketing Cloud. It often comes down to small but decisive details, such as the correct time zone or the correct sequence of queries. These subtleties can make the difference between successful and incorrect data processing. Should you encounter any problems or need assistance, do not hesitate to contact us as an expert. We are always happy to help you with challenges with SQL or other topics related to Salesforce Marketing Cloud.

Share this article now
link
blog

Even more about Salesforce Marketing Cloud

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Next: What you need to know about template creation

Salesforce Marketing Cloud Next, the new marketing automation solution built on the Salesforce Core platform, has been available since summer 2025. It features a completely revamped email editor. Those who have worked with the Marketing Cloud Engagement Content Builder will recognize many familiar elements, such as drag-and-drop functionality, flexible blocks, and familiar logic. However, much has changed beneath the surface. Without strategic planning, those who start creating templates will quickly encounter obstacles that are difficult to correct later on. Is investing in a custom-built Next template worthwhile, and what should you pay particular attention to?

salesforce-marketing-cloud
All Categories

Streamlining Salesforce Marketing Cloud: Delete Data Extensions and Efficiently Update Content

One of the biggest administrative challenges in Salesforce Marketing Cloud ) is the management of ever-growing inventories of data and assets. If complex data structures, automations and content assets (such as scripts, templates and images) are not regularly cleaned up, performance losses and operational inefficiencies are inevitable. This article highlights the limitations of the native delete function and data retention policy feature, presenting a complementary best-practice solution for housekeeping in 'Salesforce Marketing Cloud Engagement'

salesforce-marketing-cloud
All Categories

Performance optimization in Salesforce Marketing Cloud: When complexity delays delivery

Complex email campaigns with a high degree of personalization place special demands on the architecture of Salesforce Marketing Cloud Engagement. In practice, a specific issue arises time and again: response times within the SFMC user interface slow down noticeably. After the campaign launches as scheduled, the actual delivery process gets off to a slow start.

salesforce-marketing-cloud
All Categories

What's “Next,” Salesforce? What to consider before running a new and old SFMC platform in parallel

The switch to the new marketing cloud world is tempting: With the Growth and Advanced editions, Salesforce promises deeper platform integration and comprehensive AI support. Existing customers of Marketing Cloud Engagement and Account Engagement should use old and new solutions in parallel and thus migrate very gently. While new channels such as WhatsApp attract, the technical maturity of established platforms and the mandatory Data 360 as a new core raise important questions. The change towards consumption billing also requires precise calculation. Find out which factors should be considered before deciding on parallel operation.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud - Why Einstein Engagement Scoring alone isn't enough

Learn everything about the benefits and limits of Einstein Engagement Scoring and when customized scoring models are beneficial.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Release Highlights Winter '25

Salesforce has announced its Marketing Cloud release for winter 2025, which brings a number of interesting new features. We provide an overview of the most important highlights.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud - SQL Features & Tips Part 1

Part 1: Using SQL in Salesforce Marketing Cloud offers powerful options for data processing and analysis, but also brings with it some Marketing Cloud-specific features that users should be aware of.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Content Builder Templates

Investing in custom templates for Salesforce Marketing Cloud Content Builder is worthwhile. The result is easy-to-use, CI-compliant, consistent emails that are displayed correctly in all common email clients and save time and money in the long term.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Feature Friday: SFMC Companion

Our Salesforce Marketing Cloud experts present a useful add-on for everyday work in Marketing Cloud, which provides greater clarity and greater efficiency.