e-mail mgbg25171 wrote:
> I know that ORDER BY sorts result but I want to sort a table BEFORE it gets
> queried and am not sure of the syntax.
> Here's my test program but...I'm not sure how to PRE-SORT tables t_x and t_y
> (by column pos)  BEFORE I do the SELECT BETWEEN on THEM
> i.e. I purposefully inserted t_d row 1,1,1 to see if it would come out first
> ie as 1. It doesn't any help much appreciated.
> Apologies if my question isn't clear.

Try using a subquery and put your order-by in there.

Rather than this:

   select ...
     from foo
     ...

You can say:

   select ...
     from (select ... from foo ... order by ...)
     ...

Then the order-by is done prior to what the outer query does.

One practical use for doing this is when you are using paged results, such as 
LIMIT/OFFSET gives you, and you just do that on a main recordset in the inner 
query, and then you do much more complicated joins or whatever in the outer 
query, and it is only going to the bother of all those joins/etc against the 
subset of main records you actually want.

If you aren't doing paging but rather some order-sensitive operation, then make 
your inner select return some extra column that contains an order number, such 
as using the RANK() SQL window function would give you.

-- Darren Duncan
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to