Search code examples
sqltop-n

How could I query from the first n rows using SQL?


For example, I would like to query from the first 10 rows, how could I achieve that. (Not get the first 10 of the query results).

I tried using 'limit' first then 'where' but it doesn't work.


Solution

  • If I understand correctly you first want to fetch 10 rows of one query, then search within those 10 rows for data. To do that you can use a subquery:

    SELECT *
      FROM (SELECT *
              FROM YOUR_TABLE yt
              ORDER BY yt.SOME_COLUMN
              LIMIT 10) sq
      WHERE sq.SOME_OTHER_COLUMN > 25;
    

    Best of luck.