Snowflake MAX_BY can name one top seller even when two tie
A single returned ID does not establish a unique winner. Test whether the generated SQL preserves ties, applies an approved tie-breaker, or picks an unspecified row.
Ask a Snowflake-backed analytics agent, “Which product had the most net sales last week?” and inspect how the SQL handles a tie. MAX_BY(product_id, net_sales) can return a single product ID even when two products share the highest sales total. Snowflake documents that the choice is nondeterministic when multiple rows contain the maximum. The returned product is a leader, but the result does not establish that it was the only leader.
Test what happens at the cutoff
Start with a synthetic weekly summary that already has one row per product: A and B each have net sales of 120, C has 90, and D has a null total. The scalar MAX_BY can return A or B and ignores D's null score. The optional third argument limits the number of rows represented in the returned array; it does not set a tie policy. With a limit of two, this fixture yields A and B in an unspecified order. Add a third product at 120 and the two-element result can no longer show every co-leader. The function reference also specifies that if every score is null, the function returns null, even with the array argument. That fixture should produce “no known leader,” not “all products tied.”
Put the tie rule into the query
If the business question means “show every product tied for first,” filter out null scores and use a rank that gives equal scores the same position. In this example, weekly_product_sales is the already-aggregated weekly summary described above. Snowflake's DENSE_RANK reference documents that equal values share a rank.
WITH ranked AS (
SELECT product_id, net_sales,
DENSE_RANK() OVER (ORDER BY net_sales DESC) AS sales_rank
FROM weekly_product_sales
WHERE net_sales IS NOT NULL
)
SELECT product_id, net_sales
FROM ranked
WHERE sales_rank = 1
ORDER BY product_id;
The final sort controls how the co-leaders are displayed; it does not break the tie. If a downstream workflow requires exactly one winner, use a business-approved secondary criterion and disclose it in the answer. A stable product ID can order a display, but it should not become a sales tie-breaker by accident. For an acceptance test, assert the returned set and the wording: “A and B tied at 120” passes; “A was the top seller” alone does not. The fixture illustrates documented behavior; it is not a measured warehouse run.
sources
- Snowflake SQL reference: MAX_BYdocs.snowflake.com
- Snowflake SQL reference: DENSE_RANKdocs.snowflake.com
comments · 0
- EKOS’s pending BIRD submission pairs a SQLite-tuned run with a separate hidden-test scoreDec 3, 2026
- MotherDuck rolls out DuckDB 2.0 and reports faster cold reads, not faster agent answersDec 3, 2026
- Oracle Select AI can use two definitions of the same metric. Source order won’t break the tieDec 3, 2026