Home
/
Blog
/
Guides
Guides

SFMC Abandoned Cart Audience: SQL Query and Setup Guide

Two ways to build an abandoned cart audience in SFMC: Behavioral Triggers, or your own cart data extension with a complete, deduplicated SQL query and suppressions.

Someone adds three items to their cart, gets distracted, and never checks out. You want that person in a sendable audience within the hour, without emailing people who already bought, already got the reminder, or already unsubscribed. That is the whole job of an SFMC abandoned cart audience, and it is harder to get right than it looks.

The short answer: Salesforce Marketing Cloud gives you two ways to build it. Behavioral Triggers capture carts from your website automatically, and a SQL query activity turns your own cart and order data extensions into a clean, deduplicated audience that Journey Builder can pick up on a schedule.

This guide covers both, with a complete abandoned cart SQL query for SFMC, the suppression logic that keeps the audience honest, and the Journey Builder settings that stop you from sending the same reminder twice.

‍

Two ways to build an abandoned cart audience in SFMC

‍

Before you write any SQL, decide where your cart data comes from. The answer changes the whole build.

‍

Option 1: Behavioral Triggers

Behavioral Triggers is the native SFMC feature for abandoned cart, abandoned browse and abandoned wishlist scenarios. It needs the Collect Tracking Code installed on your site and a product catalog uploaded to Marketing Cloud. According to Salesforce, it is free for Pro, Corporate and Enterprise editions.

When a visitor identified through the tracking code stops shopping and the session timeout passes, SFMC writes a row into a system data extension named abandoned_cart_ followed by a generated number. Each row holds a subscriber_key, a timestamp and an encoded data field that the email content block decodes at send time. Those behavioral data extensions keep data for 30 days.

The catch for anyone who likes SQL: the product detail lives inside that encoded field. You can query keys and timestamps, but not the cart contents.

‍

Option 2: your own cart data extension and SQL

Many teams do not run the Collect code, or they receive cart events from an ecommerce platform, a CDP or Data Cloud as plain rows in a data extension. In that case you own the fields, the time window and the suppressions, and a SQL query activity becomes the right tool.

This is the path most of this article follows, because it is where the SQL does the real work.

‍

The data you need before writing the query

‍

An abandoned cart query joins three or four tables, and each one has to answer a precise question. Here is the minimum setup, with generic names.

‍

A cart events data extension

Call it Cart_Events. One row per cart update, with SubscriberKey, EmailAddress, CartID, CartValue and a LastCartActivity date. If your source sends one row per cart line instead of one per cart, aggregate it upstream or add a GROUP BY in a staging query first.

Two things matter here. The date field must be a real Date type, not text, or DATEADD() comparisons will fail. And the SubscriberKey must be the same key you use everywhere else in the account, otherwise your suppressions will silently miss.

‍

An orders data extension and a sent log

Call them Orders (with SubscriberKey and OrderDate) and Abandoned_Cart_Sent (with SubscriberKey and SentDate). The first one proves the cart converted. The second one proves you already reminded this person recently.

The sent log is the piece teams forget. Without it, a cart that stays open for three days produces three reminders. Feed it from a Journey Builder update contact activity, or from a second query that appends today's audience after the send.

‍

Define what "abandoned" means for you

There is no universal answer. Behavioral Triggers exposes it as a session timeout limit in Setup. For a SQL build, pick two numbers: how long after the last activity a cart counts as abandoned (the lower bound), and how old a cart can be before a reminder is pointless (the upper bound). The query below uses one hour and 24 hours.

‍

The SFMC abandoned cart SQL query

‍

This query returns one row per subscriber with an open cart from the last 24 hours, untouched for at least an hour, with no order since, no reminder in the past week and a sendable subscriber status. It is valid for an SFMC query activity: SELECT only, no variables, no temp tables.

SELECT
    c.SubscriberKey,
    c.EmailAddress,
    c.CartID,
    c.CartValue,
    c.LastCartActivity
FROM (
    SELECT
        SubscriberKey,
        EmailAddress,
        CartID,
        CartValue,
        LastCartActivity,
        ROW_NUMBER() OVER (
            PARTITION BY SubscriberKey
            ORDER BY LastCartActivity DESC
        ) AS rn
    FROM Cart_Events
    WHERE LastCartActivity >= DATEADD(HOUR, -24, GETDATE())
      AND LastCartActivity <  DATEADD(HOUR, -1, GETDATE())
      AND CartValue > 0
) AS c
WHERE c.rn = 1
  AND NOT EXISTS (
      SELECT 1
      FROM Orders AS o
      WHERE o.SubscriberKey = c.SubscriberKey
        AND o.OrderDate >= c.LastCartActivity
  )
  AND NOT EXISTS (
      SELECT 1
      FROM Abandoned_Cart_Sent AS s
      WHERE s.SubscriberKey = c.SubscriberKey
        AND s.SentDate >= DATEADD(DAY, -7, GETDATE())
  )
  AND NOT EXISTS (
      SELECT 1
      FROM _Subscribers AS sub
      WHERE sub.SubscriberKey = c.SubscriberKey
        AND sub.Status IN ('unsubscribed', 'held', 'bounced')
  )

‍

Line by line

  • The inner SELECT numbers each subscriber's carts from newest to oldest with ROW_NUMBER, so a person with two open carts gets one row.
  • The two DATEADD filters define the window: activity in the last 24 hours, but not in the last hour.
  • CartValue greater than zero drops empty carts left behind when a customer removes every item.
  • The first NOT EXISTS removes anyone with an order dated on or after their last cart activity. That is the conversion check.
  • The second NOT EXISTS removes anyone who already received an abandoned cart email in the past seven days. Change the window to match your frequency rule.
  • The third NOT EXISTS checks the _Subscribers data view and excludes unsubscribed, held and bounced subscribers.

A note on GETDATE(): it returns SFMC server time (Central Standard Time, no daylight saving). If your cart timestamps arrive in UTC, shift one side with DATEADD() before comparing. The SFMC SQL date functions guide covers that trap.

‍

Target data extension and data action

Write the result to a sendable data extension such as AUD_Abandoned_Cart_Daily, with SubscriberKey as the primary key and EmailAddress as the send relationship. Keep CartID, CartValue and LastCartActivity as regular fields for personalisation.

Use the Overwrite data action. Each run should produce the complete audience for that moment, not accumulate yesterday's carts on top of today's. If you also want a history table, add a second query with Append into a separate log, which can double as your Abandoned_Cart_Sent source.

‍

Suppressions and edge cases that break abandoned cart emails

‍

The query handles the big three suppressions. These are the cases practitioners hit after launch.

‍

Late orders and timing gaps

Order data often lands in SFMC on a nightly import, while cart events arrive hourly. In that gap, a customer who bought at 14:00 still looks abandoned at 15:00. If you cannot speed up the order feed, widen the lower bound so the query runs only after orders have caught up.

‍

Unidentified visitors

Neither approach can email someone it cannot identify. Behavioral Triggers only attributes a cart once the visitor is identified through the tracking code. With your own feed, filter out rows with an empty SubscriberKey or EmailAddress before they reach the sendable data extension.

‍

Carts that never close

Some shoppers keep a cart alive for weeks. The 24 hour upper bound stops them from re-entering every day. If you run a second-touch reminder, build it as a separate query with its own window (for example, 48 to 72 hours). Our guide to dynamic suppression lists in SFMC covers keeping those exclusions in one place.

‍

Querying the Behavioral Triggers data extension

‍

If your cart data comes from Behavioral Triggers, you can still shape the audience with SQL. The system data extension has subscriber_key and timestamp as a composite primary key, which means one subscriber can have several rows.

SELECT
    subscriber_key AS SubscriberKey,
    MAX([timestamp]) AS LastCartActivity
FROM abandoned_cart_123456789
WHERE [timestamp] >= DATEADD(HOUR, -24, GETDATE())
GROUP BY subscriber_key

‍

What this does and what it cannot do

  • Replace the number in the table name with the one generated in your account; the exact name is visible in Contact Builder.
  • The column timestamp is a reserved word in SQL Server, so it is wrapped in square brackets.
  • MAX with GROUP BY keeps the latest cart event per subscriber, which is usually what the journey needs.
  • The product details stay inside the encoded data field. Pull them in the email with the Behavioral Triggers content block, not in SQL.

Most teams on Behavioral Triggers point a journey straight at the system data extension. The query is useful when you want your own suppressions or a join with other first-party data before the send.

‍

Wire the audience into Journey Builder

‍

An abandoned cart audience is only useful if something sends from it quickly.

‍

Automation and entry source

Put the query in Automation Studio on a schedule that matches your window: hourly if the lower bound is one hour, every 15 minutes if you need faster follow-up. Then use the target data extension as a Journey Builder data extension entry source, with contact evaluation set to new records only and re-entry disabled, or allowed only after exit.

Because the query uses Overwrite, a subscriber who disappears and reappears looks like a new record to Journey Builder. Your sent log and the seven day rule in the query are what prevent duplicates, not the journey alone.

‍

Quick checklist

StepWhat to confirm
1. SourceCart_Events has one row per cart, a real Date field and the account-wide SubscriberKey.
2. WindowLower and upper bounds chosen (default: 1 hour to 24 hours).
3. SuppressionsOrders since cart, reminder in the last 7 days, status in _Subscribers.
4. TargetSendable data extension, SubscriberKey primary key, Overwrite data action.
5. ScheduleAutomation runs at least as often as the lower bound.
6. JourneyData extension entry source, new records only, sent log updated after the email.
7. TestSeed a test cart, wait past the lower bound, confirm one row and one email.

‍

FAQ

‍

How long should you wait before sending an abandoned cart email in SFMC?

Long enough that the shopper has really stopped, short enough that the intent is still warm. Behavioral Triggers exposes this as a session timeout setting; a SQL build exposes it as the lower bound of the window. Test it against your own conversion data rather than copying a number.

‍

Can you build an abandoned cart journey in SFMC without Behavioral Triggers?

Yes. If cart events reach a data extension from your ecommerce platform, Data Cloud or an integration, a scheduled SQL query plus a data extension entry source does the job. The email then pulls product details from your own fields.

‍

Why does my abandoned cart query return the same subscriber twice?

Almost always because the cart table has more than one row per subscriber, one per line or one per update. The ROW_NUMBER() pattern above keeps the newest row per key, and a SubscriberKey primary key on the target data extension blocks any duplicate that slips through.

‍

How long is Behavioral Triggers data kept?

Salesforce states that behavioral data extensions store data for 30 days. If you need a longer history for reporting, copy the keys and timestamps into your own data extension with a scheduled query.

‍

Build this audience in seconds with QAiry

‍

Every abandoned cart audience is the same shape: a window, a dedupe, and three or four exclusions. QAiry takes a request like customers with a cart updated in the last 24 hours but not the last hour, no order since, no reminder this week and returns production-ready SFMC SQL plus the target data extension.

You can install QAiry for free in your SFMC account or watch a short product demo to see how a plain-language request becomes a query activity.

Share this article
QAiry for SFMC

Skip the SQL. Build segments by chatting.

QAiry turns plain English requests into Salesforce Marketing Cloud audience segments and data extensions — no SQL, no IT ticket, no waiting.

Built for SFMC · ISV Partner · GDPR-ready