Learn how to write SQL for Salesforce Marketing Cloud (SFMC). This beginner tutorial explains Data Extensions, Query Activities, Data Views, filtering, and segmentation with practical examples you can adapt to your account.
8 beginner-friendly lessonsPractical SFMC SQL examplesFor marketers, analysts, and developers
What you will learn
These are the building blocks that make SQL useful in SFMC for segmentation, reporting, and data cleanup.
SELECT the right fields
Choose fields from a Data Extension or a Salesforce Marketing Cloud Data View.
Filter with WHERE
Filter subscriber and engagement data by status, date, region, and behavior.
Join tables together
Connect related Data Extensions or Data Views with matching subscriber and job keys.
Group and summarize
Summarize sends and engagement with GROUP BY, COUNT, and other aggregate functions.
Start with the essentials
A good SQL workflow in SFMC usually starts with a simple query and grows from there.
1. Select data
Begin by choosing the columns you need from a Data Extension.
2. Filter rows
Use WHERE to narrow results to the right audience or timeframe.
3. Plan the output
Match the selected fields to the columns in the target Data Extension.
4. Limit and sort rows
Use ORDER BY and TOP to inspect a smaller, ordered set of records while building a query.
5. Join related data
Match subscriber and send records with a shared SubscriberKey, and use JobID when you need to identify a send.
6. Summarize results
Group rows and use COUNT or other aggregates to create campaign and audience reports.
7. Query Marketing Cloud Data Views
Explore system Data Views such as _Subscribers and _Sent for subscriber and send activity.
8. Validate a Query Activity
Check field names, target Data Extension columns, and primary keys before scheduling an automation.
Helpful SFMC SQL examples
Example 1: Select subscribers from a Data Extension
SELECT TOP 20
SubscriberKey,
Email,
Tier
FROM ENT.CustomerDE
WHERE Country = 'India'
ORDER BY SubscriberKey;
Replace ENT.CustomerDE and its fields with the names used in your account. The SQL Playground uses this sample Data Extension.
Example 2: Find recent sends in a Data View
SELECT
JobID,
SubscriberKey,
EventDate
FROM _Sent
WHERE EventDate >= DATEADD(day, -7, GETDATE());
Data Views are system views. Available history and fields depend on the view and your Marketing Cloud account.
Example 3: Join send activity to subscriber data
SELECT
s.JobID,
s.EventDate,
sub.EmailAddress
FROM _Sent AS s
INNER JOIN _Subscribers AS sub
ON s.SubscriberKey = sub.SubscriberKey;
Use the fields and retention window available in your account, and test the query before using it in an automation.
SFMC SQL best practices
Select only the columns your audience or report needs instead of using SELECT *.
Use a clear filter and a suitable target Data Extension before scheduling a Query Activity.
Ensure the query output matches the target Data Extension's field names and data types.
Use SubscriberKey and, where applicable, JobID to join the intended records and avoid accidental row multiplication.
Start with a small result set, then validate counts and sample records before an overwrite or automation run.
Frequently asked questions about SFMC SQL
Where do I run SQL in Salesforce Marketing Cloud?
Use Automation Studio's SQL Query Activity to query Data Extensions and supported Data Views, then write the results to a target Data Extension.
Can I use standard SQL in Marketing Cloud?
Marketing Cloud SQL supports many familiar SELECT, filtering, join, and aggregate patterns, but it is not a full general-purpose SQL environment. Check feature compatibility and test queries in your account.
How can I practice without a Marketing Cloud account?
Use the SFMC SQL Playground to try basic queries against sample data. The free playground is a learning aid and does not connect to a live account.
Mini roadmap for your first week
Day 1: Understand SELECT, FROM, and Data Extension field names.
Day 2: Practice WHERE filters for status, date, and region.
Day 3: Sort and limit records, then check your sample results.
Day 4: Join subscriber records to send activity with SubscriberKey.
Day 5: Summarize results and validate a Query Activity target.
Ready to practice?
Practice basic queries against sample Marketing Cloud data, then adapt the patterns to your account.