Why do NULL values come first when ordering DESC in a PostgreSQL query?

前端 未结 2 2081
野趣味
野趣味 2020-11-29 12:23

When would you ever want NULLS first when ordering a query descending or ascending?

In my opinion, the vast majority of the time the desired behavio

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 12:56

    The simple answer is because that's how the people who wrote Postgres designed it. To quote:

    The null value sorts higher than any other value. In other words, with ascending sort order, null values sort at the end, and with descending sort order, null values sort at the beginning.

    This assumes that you have specified an ORDER BY clause, if you haven't then the rows are returned randomly.

    If the ORDER BY clause is specified, the returned rows are sorted in the specified order. If ORDER BY is not given, the rows are returned in whatever order the system finds fastest to produce.

提交回复
热议问题