I have the following request in postgres:
SELECT * FROM "bookings" WHERE ("bookings".client_id = 50) ORDER BY session_time DESC LIMIT 20 OFFSET 0
The record in 20th place has identical session_time for the 21st record.
This query returns 20 results, however, if you compare the results with the entire database, the query returns the results of 1-19th and 21st, skipping the 20th.
This query can be fixed by adding "id" to the order:
SELECT * FROM "bookings" WHERE ("bookings".client_id = 50) ORDER BY session_time DESC, id LIMIT 20 OFFSET 0
However, I was wondering how this error occurred? How is Postgres order identical when using offsets and limits? Is this a coincidence? Is this a bug with postgres?
sql sql-order-by postgresql limit offset
Smickie
source share