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 - 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.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Release Highlights Winter '23

Salesforce has announced its Marketing Cloud release for winter 2023, 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 — Marketing Cloud February 2022 Release

On February 19, the Salesforce Marketing Cloud spring 2022 release goes live. We have summarized our release highlights in a concise manner in our article.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud - Datorama Reports vs. Datorama Reports Advanced

Is a paid upgrade to Datorama Reports Advanced worth it? An overview of the most important functionalities.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Deployments with Package Manager and Deployment Manager

Wie helfen Package Manager und Deployment Manager beim Rollout standardisierter Prozesse in Marketing Cloud?