zrubenst
zrubenst

Reputation: 931

SQL order and limit on a result of a query

I have a query that gets rows ordered by column a and limited to 100. Then I want to run a query on that result that gets rows ordered by column b and limited by 50.

How can I do this?

Upvotes: 0

Views: 964

Answers (2)

Evgeny
Evgeny

Reputation: 4010

You should use select from select statement:

select a, b
from (
   select a, b
   from table1
   order by a
   limit 100
)
order by b
limit 50

Upvotes: 0

jarlh
jarlh

Reputation: 44796

Do the first order by/limit in a derived table. Then do the second order by/limit on the derived table's result:

select * from
(
select * from tablename
order by a
limit 100
) dt
order by b
limit 50

Upvotes: 3

Related Questions