Production SQL // SFMC

SQL Playbook.

The patterns I reach for repeatedly in Marketing Cloud: deterministic deduplication, ROW_NUMBER(), query grain, SARGable filtering, staging, target actions and audience segmentation.

9 deep guidesWhy the pattern works, not only what to paste.
24 SQL snippetsReusable building blocks with gotchas and evidence.
Production-firstRules earned from operating real SFMC workloads.
Deep dives

Know why the query works.

The goal is a reference I can open during real work, not a wall of copy-paste snippets.

001
intermediatePRODUCTION

ROW_NUMBER() — The Workhorse of SFMC SQL

Use ROW_NUMBER() when the business question is “which single row should survive inside each logical group?” The hard part is not the function — it is defining the partition and ordering rules correctly.

row_numberdeduplicationwindow functionsql
002
advancedPRODUCTION

Deduplication by Business Priority

A Golden Record rule often needs explicit source trust, status priority or permission precedence before recency. Put those rules in the window ORDER BY so the surviving row reflects the business contract.

deduplicationprioritycasegolden record
003
intermediatePRODUCTION

SARGable Filtering in Query Activities

On large Data Extensions and Data Views, where the function is applied can decide whether the database can use an index efficiently. Keep searchable columns “bare” on the left side of predicates whenever possible.

performancewheredateaddsargabletimeout
004
advancedPRODUCTION

Join Grain Before Join Syntax

Most “my counts exploded” problems are grain problems. Define what one row represents in every source before writing the JOIN.

joinsgraintrackingdata views
005
intermediatePRODUCTION

Stage Large Queries Instead of Being Clever

When one Query Activity has too many joins, scans and transformations, split the work into deterministic stages. You gain observability, smaller scans and easier failure isolation.

performanceintermediate tablesautomation studiotimeout
006
intermediatePRODUCTION

Target Action Is Part of Query Design

The SQL text is only half the Query Activity. The target action changes how the platform writes data and can become the bottleneck as the target grows.

query activityoverwriteupdateappendperformance
007
intermediatePRODUCTION

Audience Splitting: Precision vs Throughput

Use NTILE() when you need balanced random buckets. Use _CustomObjectKey modulo when you need a very cheap, approximately even split on large standard Data Extensions and exact balance is not required.

ntilecustomobjectkeyrandomsegmentationholdout
008
foundationPRODUCTION

ORDER BY Is Not a Storage Contract

A Data Extension is a relational table, not an ordered list. ORDER BY is useful when paired with TOP or window functions to select which rows win, but downstream consumers should never rely on physical row order.

order bytopdata extensionsql
009
foundationPRODUCTION

My SFMC SQL Operating Rules

These are the defaults I want to remember before promoting a query from exploration into a production automation.

sqlchecklistautomation studiogovernance
Quick rules

Before a query becomes infrastructure.

01 // GRAIN

One row means what?

Define the grain of every source and of the final target before joining anything.

02 // IDENTITY

Dedup is a business rule.

PARTITION BY is not syntax decoration. It is the identity definition you are enforcing.

03 // PERFORMANCE

Filter early. Stage deliberately.

A readable sequence of small queries often beats one heroic query that nobody wants to debug.