I'm using Python and pandas to run a SQL query and filter the results. For example, I can fetch every order and filter afterward with `df[df["amount"] > 1000]`, or apply the condition in SQL with `WHERE amount > 1000`. These seem equivalent, but can they differ because of NULL handling, data-type conversions, date/time values, floating-point precision, database-specific behavior, or result ordering? In general, which logic is best kept in SQL, and what makes sense to handle in Python?
4 Answers
Check the type that pandas receives for `amount`. A database numeric column might be converted to a floating-point, decimal, object, or nullable dtype, and comparisons can behave differently around missing values or boundary values. Printing the values near 1000 and inspecting `df.dtypes` can reveal the cause.
For a simple predicate like this, filtering in SQL is usually preferable: the database can use indexes and avoids sending unnecessary rows across the connection. To debug the mismatch, compare the rows returned by each method and identify which rows appear only in one result. Also compare the actual values and dtypes, not just the row counts.
NULLs and conversions are worth checking first. In SQL, `amount > 1000` evaluates to UNKNOWN for NULL values, so those rows are excluded. Pandas comparisons involving missing numeric values generally produce a false mask too, but differences can appear if missing values are handled explicitly or arrive with a different dtype. Floating-point values can also change slightly during database-to-Python conversion, so a value near the boundary might be classified differently. In most cases, I keep straightforward filtering in SQL so the database does the work and less data has to be transferred, then use Python for more complicated transformations or logic involving multiple data sources.
An unspecified result order is an easy explanation if the contents are identical but the displayed order changes. SQL tables and query results have no inherent order unless you explicitly sort them. Add an `ORDER BY` clause when comparing outputs or when order matters.

It also matters whether the difference is in the rows, the values, or only their order. Neither query guarantees ordering without an `ORDER BY` clause, so the same rows may appear in different sequences.