1

Is it possible? I mean

Select * from tbl limit 100;

I want to replace with a query like

Select * from tbl WHERE ...some_condition...
4
  • @a_horse_with_no_name I want to print first 100 rows without using limit but with wherec lause Commented Apr 15, 2015 at 13:45
  • Technically you could use ROW_NUMBER() and subselect and all that, but not sure if it's smart. Commented Apr 15, 2015 at 13:46
  • 7
    So what's wrong using limit? Commented Apr 15, 2015 at 13:56
  • You want LIMIT but without using LIMIT. Why? Commented Apr 16, 2015 at 2:33

3 Answers 3

2

You could use row_number() for that:

select  *
from    (
        select  row_number() over () as rn
        ,       *
        from    YourTable
        ) as SubQueryAlias
where   rn <= 100

If you have a specific ordering in mind, you can add an order by clause:

select  row_number() over (order by date_column) as rn
Sign up to request clarification or add additional context in comments.

Comments

2

Do you want the where clause only applied to the first hundred rows? If so, use a subquery:

select . . .
from (select t.*
      from table t
      limit 100) t
where . . .

This will return less than 100 rows (presumably not all rows match the condition). Also, when using limit, you should have an order by condition.

Comments

1

You cannot put a limit clause inside a where clause. But you can of course append a limit clause after the where clause.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.