Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:37:48+00:00 2026-05-25T17:37:48+00:00

Today I came across a quirky behavior from what looked like a simple SQL

  • 0

Today I came across a quirky behavior from what looked like a simple SQL query. A SELECT, a few JOINs, ORDER BY a single field. What I am unable to explain right now is that the sort changes, for equal values, based on the LIMIT statement. Let me give you an example :

SELECT facture.ID, [ bunch a fields ... ] FROM facture 
CROSS JOIN contact [ a few JOINs ... ] 
WHERE facture.USER_ID=usager.ID AND usager.ID=contact.USER_ID 
ORDER BY contact.NAME DESC LIMIT 50;

The first results will be have the exact same name, and are organized like this : 166, 172, 167, 169 (the IDs). If I get rid of the LIMIT statement, I should have the same order, for those results … right? Nope : 172, 166, 167, 169. If I change the value of the limit, I also do get different order. With a LIMIT 60, I get : 167, 166, 172, 169.

So I’m trying to understand… why a different sorting order for same values? After sorting with the field specified in the ORDER BY, should the DB then sort based on IDs or something else? This just sounds random. BTW the database is PostgreSQL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:37:49+00:00Added an answer on May 25, 2026 at 5:37 pm

    The order of the returned rows is not deterministic. as you are only ordering by contact.Name and all rows returned have the same name, the order of the rows can change from query to query. You should order by the ID, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across a query today, SELECT col1,'yes' as col2 FROM myTable WHERE col2=TRUE
I came across some weird behavior today around creating an object from the return
I came across a strange one today, it goes like this: I'm setting up
Today I came across an SQL statement that contains an element I've never seen.
i came across a compatibility issue today, as a customer upgraded from Windows XP
Today I came across a problem where someone had accidentally committed a proj.user file
Today I came across this question: you have a code static int counter =
I have been using Perl for some time, but today I came across this
So today I just came across the 'live()' function that binds any future and
I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.