Postgres implicitly sorts row based on the group by column. In MySQL, we can change this behavior by setting the ORDER BY NULL. Is there any similar alternative way to achieve this in Postgres [Reference]?
Question posted in PostgreSQL
The official documentation can be found here.
The official documentation can be found here.
2
Answers
Yes, there is a way to achieve this in PostgreSQL, which is the SQL language used by PSQL. In PostgreSQL, you can use the
GROUP BY
clause to group rows based on the values of one or more columns. By default, the rows in the result set are sorted based on the values of the columns specified in theGROUP BY
clause. However, you can use theORDER BY NULL
clause to disable this default behavior and prevent the rows from being sorted based on the values of theGROUP BY
columns.PostgreSQL only sorts for grouping if it thinks that that is the best way to get the job done. It doesn’t just add sorts out of neurosis; so there is no point in telling it to stop doing that. PostgreSQL isn’t a MySQL clone, don’t treat it like it is one.