Polars vs SQL: Null Ordering, Literals and Empty Sums Explained
By Matt Cullerton, founder of Mavric and Apso
Polars and SQL give different answers for the same logic in several places: row order, lit(1), where nulls sort and the sum of an empty column. A TikTok from @realpython.com summarises a Quansight Labs post on these differences, and most of its claims hold when run. Its one copyable instruction is wrong: Polars has no nulls_first argument. The real one is nulls_last, and it already defaults to nulls first.
Where is the clip from?
The clip comes from episode 311 of The Real Python Podcast, "Django Developers Survey Results & Reproducible Python Builds," published on 11 September 2026. It is a PyCoder's Weekly roundup with Christopher Trudeau, hosted by Christopher Bailey. The Polars segment runs from 9:56 to 16:55.
The segment summarises Marco Gorelli's Quansight Labs post "The Polars vs SQL differences nobody is talking about," published on 19 August 2026. The clip shows no code. The results below come from the original post's tests on Polars 2.0.0 and DuckDB 1.5.6.
How do Polars and SQL think about tables differently?
The clip's framing is that Polars DataFrames are collections of columns with a defined row order, while SQL tables are unordered bags of rows. Gorelli writes: "In Polars, not only is row-order well-defined, it is also preserved by select and with_columns operations." He adds that "Polars doesn't always promise to preserve it."
Does Polars sort nulls first or last?
First, by default. SQL puts them last. The clip gets this right. It then says that in Polars "you would put nulls underscore first equals true," and the caption shows NULLS_FIRST = TRUE.
That argument does not exist. The DataFrame.sort reference gives the signature:
DataFrame.sort(by, *more_by, descending=False, nulls_last=False, multithreaded=True, maintain_order=False)
Expr.sort also takes nulls_last. On Polars 2.0.0, passing nulls_first=True raises TypeError: DataFrame.sort() got an unexpected keyword argument 'nulls_first'.
To match SQL, pass nulls_last=True:
pl.DataFrame({"v": [2, None, 1]}).sort("v") # null, 1, 2
pl.DataFrame({"v": [2, None, 1]}).sort("v", nulls_last=True) # 1, 2, null
The mistake comes from the source post, which writes "(nulls_first=True / nulls_first=False)." The clip repeats it.
What does pl.lit(1) mean in Polars vs SQL?
The clip says: "In Polars, lit1 means a single row with a value of 1, whereas in SQL, it means repeat the value of 1 in every row in this table." That holds. Over a three-row frame, df.select(sum_1=pl.lit(1).sum()) returns 1. DuckDB's select sum(1) from df returns 3.
The Polars expressions guide explains why: expressions in select "must produce series that are all the same length or they must produce a scalar," and "Literals ... are also broadcast."
What is the sum of an empty column in Polars?
Zero. In SQL it is NULL. The clip says, "The sum of zero elements is null in SQL and zero in Polars," and that holds: pl.col("x").sum() on an empty Int64 column returns 0, and DuckDB returns NULL.
Why avoid Expr.drop_nulls in Polars?
Gorelli's advice, which the clip paraphrases: "if you want to write maximally safe Polars, then you're best off avoiding expressions which change length (e.g. Expr.drop_nulls) or which change order (e.g. Expr.sort). Instead, use the dataframe equivalents (DataFrame.drop_nulls and DataFrame.sort respectively) so that all rows get truncated or rearranged together."
The clip skips the example that shows why. In a three-row frame of musical theatre roles where Elsa has a null voice and Anna has a null role, df.select(pl.col('voice', 'role').drop_nulls()) drops each column's null separately. It returns a pairing of "Mezzo soprano" with "lead" that never existed in the data. df.drop_nulls() returns the one intact row.
Where available, the ignore_nulls and order_by arguments avoid the problem. pl.Expr.over takes order_by and nulls_last, and pl.Expr.first and pl.Expr.last take ignore_nulls.
Do SQL engines preserve row order?
The clip says "some SQL engines may maintain order." DuckDB is more specific. Its order preservation page guarantees original row order for COPY, single-table FROM, LIMIT, OFFSET, SELECT, UNION ALL, WHERE, window functions with an empty OVER clause, and CTEs built only from those. It does not guarantee it for JOIN, UNION, GROUP BY, ORDER BY, USING SAMPLE, whole-table aggregation or multi-table FROM. You can capture the original order with row_number() OVER ().
The portability advice holds. On Gorelli's partitioned cumulative-sum example, Polars returned rows as 1, 2, 3, while DuckDB returned 1, 3, 2 in one run and 2, 1, 3 in Gorelli's published run.
Can you just run SQL inside Polars?
Yes, and the clip does not mention it. Polars has a SQL interface: pl.SQLContext, pl.sql(), DataFrame.sql(), LazyFrame.sql(), Series.sql() and pl.sql_expr(). pl.read_database() and pl.read_database_uri() load query results from a real database.
On Polars 2.0.0, the SQL layer follows SQL semantics on all three differences: sum(1) returns 3, an empty sum returns null, and order by puts nulls last. On Polars 0.20.2, sum(1) through SQLContext returned 1, so the behaviour changed between versions.
None of these differences depend on eager versus lazy execution. They are semantic.
Bottom line
The clip's Polars vs SQL differences are accurate: lit(1) is one row, empty sums are 0, nulls sort first and column expressions can change length independently. To match SQL's null ordering, use nulls_last=True, since nulls_first does not exist. If SQL semantics matter, running the query through pl.SQLContext on current Polars keeps them.
Sources
- The Polars vs SQL differences nobody is talking about, Quansight Labs - the article the clip summarises
- The Real Python Podcast feed - episode 311 details and chapters
- polars.DataFrame.sort - sort signature and nulls_last
- polars.Expr.sort - expression-level sort
- Polars SQL interface - SQLContext and related functions
- Polars lazy API guide - eager vs lazy execution
- Polars expressions and contexts guide - broadcasting rules
- DuckDB order preservation - which clauses keep row order
Published October 17, 2026. Based on a TikTok by @realpython.com. Full video notes (markdown)