← Back to portfolioGREG MAYS
SQL & Data Extensions · Case study

Refinance Opportunities: the query behind the campaign

The campaign
Bi-weekly emails from each UMortgage Loan Originator to their own past clients — new refi and purchase programs, wealth-building education, and staying top-of-mind for the next transaction or referral. Lifetime: 226,164 sends · 53.6% open · 0.51% unsub (Oct 2023–Aug 2026).
The build
One SQL query in Automation Studio assembles the entire audience: seven Salesforce objects joined into a single Data Extension that knows the client, their latest loan, and exactly which LO should appear in the from line.
My role
Query design and every iteration of it — the joins, the windowing logic, the eligibility gates — plus the dynamic sender profile the output powers.
SFMC SQL (Query Studio)Automation StudioWindow functions7-object joinDynamic sender profilesAMPScript

The pipeline

Source

Salesforce CRM

Synced objects: accounts, loans, users, campaign membership

Transform

SQL in Automation Studio

The query below, on a recurring schedule

Audience

Refinance Opportunities DE

One row per recipient, LO and rate attached

Send

Dynamic sender profile

Every email arrives from the recipient’s own LO

The join map · what each object contributes
Spine · one row per campaign membership

Marketing_Cloud_Campaign_Member__c

A custom junction object: this person, in this campaign, owned by this LO. Everything else hangs off it.

JOIN

Marketing_Cloud_Campaign__c

Scopes rows to Campaign_Key__c = 'REFINANCE_OPPORTUNITIES' — one query pattern, reusable across campaigns.

JOIN

Account (Person)

The recipient: name, email, and role flags (borrower, co-borrower, realtor).

LEFT JOIN

User → Contact

The owning LO’s name and email for the sender profile, chained to their Contact record for the NMLS ID compliance requires.

JOIN

MC_Campaign_Toggle__c

A custom per-LO, per-campaign opt-in switch. Opted_In__c = 'true' means marketing never sends on an LO’s behalf without their say-so.

LEFT JOIN

Loan__c (windowed)

ROW_NUMBER() partitioned by account, ordered by close date, keeps only each client’s most recent closed loan — and the LEFT keeps co-borrowers without their own loan record in the audience.

The query, annotated

SELECT
    a.PersonContactId AS 'ContactId',
    a.FirstName,
    a.PersonEmail,
    a.Name,
    u.Id    AS 'OwnerId',
    u.Name  AS 'Reply Name',      -- ④ feeds the dynamic sender profile
    u.Email AS 'Reply Email',     -- ④
    ct.NMLS_ID__c AS 'NMLS',
    a.Is_Borrower__c,
    a.Is_Co_Borrower__c,
    a.Is_Realtor__c,
    l.Loan_Rate__c                -- ③ rate, for segmentation
FROM Marketing_Cloud_Campaign_Member__c_Salesforce m
JOIN Marketing_Cloud_Campaign__c_Salesforce c
    ON m.Marketing_Cloud_Campaign__c = c.Id
JOIN Account_Salesforce a
    ON m.Person_Account__c = a.Id
LEFT JOIN User_Salesforce u
    ON m.Owning_LO__c = u.Id
LEFT JOIN Contact_Salesforce ct
    ON u.ContactId = ct.Id
JOIN MC_Campaign_Toggle__c_Salesforce t          -- ① LO opt-in gate
    ON t.User__c = m.Owning_LO__c
   AND t.Campaign__c = m.Marketing_Cloud_Campaign__c
LEFT JOIN (                                      -- ② latest closed loan only
    SELECT
        Account__c,
        Loan_Rate__c,
        ROW_NUMBER() OVER (PARTITION BY Account__c
                           ORDER BY Loan_Closed__c DESC) AS rn
    FROM Loan__c_Salesforce
    WHERE Loan_Closed__c IS NOT NULL
) l
    ON l.Account__c = a.Id
   AND l.rn = 1
WHERE c.Campaign_Key__c     = 'REFINANCE_OPPORTUNITIES'
  AND m.Audience_Active__c  = 'true'   -- ⑤ eligibility flags
  AND m.Send_Eligible__c    = 'true'   -- ⑤
  AND t.Opted_In__c         = 'true'   -- ①
LO consent gate. The toggle join plus Opted_In__c means every send is opt-in at the Loan Originator level — the audience assembles itself only for LOs who raised their hand for this campaign.
One loan per person. Clients close multiple loans over the years. The windowed subquery ranks each account’s closed loans by close date and keeps rank 1, so segmentation always reads the current rate — and the LEFT JOIN keeps co-borrowers (who rarely have their own Loan__c row) in the send with a null rate rather than silently dropping them.
Rate as a segmentation handle. With every recipient’s rate in the DE, targeting is a WHERE clause away. When rates dipped below 6% in early 2026, one filtered send reached the 5,887 past clients holding 7%+ — “rates dropped, and you could save big with a refinance.” 53.5% opened, and UMortgage locked 187 loans that week, up 85% from 101 the week prior. The email was deliberately built to read like a personal note from the LO — no marketing links, replies as the only CTA — so conversations, not clicks, were the success metric.
Sender data in the audience row. Reply Name and Reply Email ride along in the DE so the sender profile can personalize the from line with zero lookups at send time.
Eligibility flags. Audience_Active__c and Send_Eligible__c are maintained upstream on the membership object, keeping suppression logic (active-loan status, recency windows) out of every campaign query instead of duplicated across them. Earlier iterations also gated entry until a loan cleared its ~6-month seasoning period; that logic later moved upstream with the rest.
The from line — deliverability by design Every LO-to-client campaign shares one sender profile powered by this query’s output. The from name is the LO’s; the from address is constructed in AMPScript:
From Name:  %%Reply Name%%
From Email: %%=Concat(Substring(AttributeValue('Reply Email'), 1,
            Subtract(IndexOf(AttributeValue('Reply Email'), '@'), 1)),
            '@email.umortgage.com')=%%
Reply-To:   %%Reply Name%% <%%Reply Email%%>
It takes the localpart of the LO’s real address and rebuilds it on @email.umortgage.com — so it aligns with the Sender Authentication Package’s authenticated domain (SPF/DKIM pass), mimics the LO’s real address (gmays@umortgage.com renders as gmays@email.umortgage.com), and keeps the corporate @umortgage.com domain’s reputation insulated from bulk sending. Replies still route to the LO’s real inbox.