sfmc // AMPscriptPRODUCTION

Bounded LookupOrderedRows()

Retrieve a small, ordered rowset instead of scanning an unbounded one-to-many relationship at render time.

ampscriptlookuporderedrowsrowcountperformance
Context Pack →Open Workspace →
Trust & provenancePRODUCTION
Evidence tierPRODUCTIONOfficial sources2Other sources0Last verified2026-10-05

Evidence tier describes my hands-on relationship with the content. Official sources describe platform contracts/documentation. Production observations should not automatically be treated as official limits.

EvidencePRODUCTION
Difficultyintermediate
Verified2026-10-05
Use when

You need the latest N related rows during rendering.

%%[
VAR @rows, @count, @row
SET @rows = LookupOrderedRows(
  "Customer_Orders",
  5,
  "OrderDate DESC",
  "SubscriberKey",
  AttributeValue("_subscriberkey")
)
SET @count = RowCount(@rows)

IF @count > 0 THEN
  SET @row = Row(@rows,1)
  /* Field(@row,"OrderId") */
ENDIF
]%%
Why it works

LookupOrderedRows lets you cap and sort the returned rowset; Salesforce documents a maximum of 2,000 rows.

Gotchas

If the relationship can genuinely exceed a small bounded set, precompute it before send instead of pushing database work into rendering.

Sources / verification