Home > Blockchain >  How to ignore implicit order by in psql group by query?
How to ignore implicit order by in psql group by query?

Time:12-16

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]?

CodePudding user response:

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.

CodePudding user response:

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 the GROUP BY clause. However, you can use the ORDER BY NULL clause to disable this default behavior and prevent the rows from being sorted based on the values of the GROUP BY columns.

  • Related