1

In PostgreSQL - in the following query I perform an INNER JOIN on two tables - the 1st table (patient_bvi_p) is SORTED. I extract the gene name (a simple string) from the "id4" column and then using this value for performing the INNER JOIN with the 2nd table (geneexpressoin17p).

My issue is that after performing the INNER JOIN the result of my query is all scrambled. The rows are no longer being sorted based on the left hand table (patient_bvi_p) while I really need/want them to be.

Can someone please explain what is the behavior one should expect after performing an INNER JOIN? Shouldn't the output be sorted in the same way the the left (/first) table was sorted? Is there a way to maintain somehow the original order? OR - I should always assume that after INNER JOIN the resultant output is unsorted (=scrambled) - and therefore I should perform an extra sorting step AFTER the doing the the INNER join?... My motivation is basically to avoid an extra sorting step and to rely on the original order of my first table.

select
    t1.* ,
    bvi_d_exp,
    bvi_r_exp,
    bvi_exp.bvi_lr_rvd
into Patient_bvi_p_exp
from
    (
        select split_part(id4, '@', 3) genes, *
        from patient_bvi_p
    ) t1
    inner join (
        select
            genename,
            bvi_d_exp,
            bvi_r_exp,
            bvi_lr_rvd
        from geneexpression17p
    ) bvi_exp on lower(t1.genes) = lower(bvi_exp.genename)
2
  • Add an Order By clause. SQL, by its very nature, is unsorted. You need to specifically tell it how you want it sorted, otherwise, it won't be.
    – Siyual
    Aug 19, 2014 at 18:57
  • 1
    BTW: does it make any sense to store the gene names case sensitive ? (to me it does not) Aug 20, 2014 at 12:17

2 Answers 2

1

The order of rows in a query output is undefined if there is no order by clause. Postgres will output in any way it sees fit. If you want the output to be ordered you must specify an order by. In other words, you should not rely on output order like you describe, it could change if it is not specified. That said, in your example:

select t1.* ,bvi_d_exp,bvi_r_exp,bvi_exp.bvi_lr_rvd
  into Patient_bvi_p_exp
  from (select split_part(id4, '@', 3)genes,* from patient_bvi_p)
  t1 inner join (select genename,bvi_d_exp,bvi_r_exp,bvi_lr_rvd
  from geneexpression17p) bvi_exp on lower(t1.genes)= lower(bvi_exp.genename);

I think you are saying that if you do this:

select * from Patient_bvi_p_exp;

You get random ordering. Yes, that is true. Again, don't rely on order. However, you could:

select t1.* ,bvi_d_exp,bvi_r_exp,bvi_exp.bvi_lr_rvd
  into Patient_bvi_p_exp
  from (select split_part(id4, '@', 3)genes,* from patient_bvi_p)
  t1 inner join (select genename,bvi_d_exp,bvi_r_exp,bvi_lr_rvd
  from geneexpression17p) bvi_exp on lower(t1.genes)= lower(bvi_exp.genename)
  order by bvi_d;

And that will cause your table to be ordered by the bvi_d column (or whichever you want). So, a simple select on that table will probably return it in the correct order. Or, if you already ran your first query, you could:

create index whatever on Patient_bvi_p_exp(bvi_d);
cluster Patient_bvi_p_exp using whatever;

And this would physically reorder the table such that a simple select would return it in the order you desire.

I have to say again, you are safer doing:

select * from Patient_bvi_p_exp order by bvi_d;
2
  • 1
    sorting by bvi_d won't get me the result I want. My left-side table was originally sorted NOT by bvi_d, but by a rather convoluted manner which takes in account chromosome number and genomic position. This sorting step was performed as part of the making of table 'patient_bvi_p'. I don't have in patient_bvi_p a specific column that I can immediately refer to in order to re-sort by this table. The sorting of this table is a composite of multiple step process. It should probably add serial number column to my left side table so it'll use me after the INNER JOIN in order to resort by my results
    – Roy
    Aug 19, 2014 at 19:28
  • I could use: ALTER TABLE Patient_bvi_p ADD COLUMN columnName serial NOT NULL; BUT - I was wondering if I could add this serial number column during a 'live' query, rather than do this cosmetics before running my query...?
    – Roy
    Aug 19, 2014 at 19:29
0

the 1st table (patient_bvi_p) is SORTED

There is no "sorted" table in SQL. If you want a sorted result then use the order by clause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.