Skip to content
Advertisement

OFFSET doesn’t work when a column is sorted with all NULLs

I don’t understand why the OFFSET clause doesn’t work when I’m retrieving a table sorted by a column that contains all NULL values?

For example, let’s say I have a table People with ID, Gender, and Race columns. The Race column contains all NULL values.

When I run these to queries, why do they retrieve the same data?

I understand ordering is not functioning, since all values in Race column are NULL. I don’t understand why it doesn’t offset 15 rows in the second query. OFFSET functions properly only if the column contains some non-NULL values.

Advertisement

Answer

If Race is not unique then the order in that group is not guaranteed to repeat. Add ID to the sort to get a repeatable sort.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement