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 617857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:28:52+00:00 2026-05-13T18:28:52+00:00

This question has a little history — Is there a way to make a

  • 0

This question has a little history — Is there a way to make a query respect the order of the inputted parameters?

I’m new to building “specialized” queries, so I assumed that if I supply an IN clause as part of a SELECT query, it’ll return results in the same order. Unfortunately that’s not the case.

SELECT * FROM artists WHERE id IN (8, 1, 2, 15, 14, 3, 13, 31, 16, 5, 4, 7, 32, 9, 37)
>>> [7, 32, 3, 8, 4, 2, 31, 9, 37, 13, 16, 1, 5, 15, 14]

(Didn’t include the step where I used Python to loop through the result and append the IDs to a list.)

So the question is, is there a way to make Postgres respect the ordering of the parameters given in an IN clause by returning results the same order?

  • 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-13T18:28:52+00:00Added an answer on May 13, 2026 at 6:28 pm

    Query results will be returned in non-deterministic order unless you specify an ORDER BY clause.

    If you really want to do the query in the manner you are requesting, then you could construct such a clause. Here’s an example using part of your data.

    create table artists (
    id integer not null primary key,
    name char(1) not null);
    
    insert into artists
    values
        (8, 'a'),
        (1, 'b'), 
        (2, 'c'),
        (15, 'd'),
        (14, 'e'),
        (3,  'f'),
        (13, 'g');
    
    select *
    from artists
    where id in (8, 1, 2, 15, 14, 3, 13)
    order by
        id = 8 desc,
        id = 1 desc,
        id = 2 desc,
        id = 15 desc,
        id = 14 desc,
        id = 3 desc,
        id = 13 desc;
    

    Based on this and on your other question, I think there is something wrong with your model or the way you are trying to do this. Perhaps you should post a more generic question about how to do what you are trying to do.

    If you do have artists and ranking tables, you should be able to do something like this (or the equivalent through your ORM).

    select
        a.*
    from
        artists a,
        rankings r
    where
        a.id = r.artist_id
    order by
        r.score desc;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked before a little over three years ago. There was
This question has been asked before, but I would like a little more detail
I know this question has been possed before, but the explanation was a little
I know this question has been partly answered here on S.O. , but there
I search this question in site but my problem has little differences. I am
I know this question has been asked before, but I've had little success even
maybe this question is a little too general but maybe someone has experience the
This question has been a little difficult for me to formulate but I think
This question has developed off an answer here . My question therefore is what
This question has two parts. Part 1. Yesterday I had some code which would

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.