0

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.

Danni Chen
  • 343
  • 1
  • 3
  • 14
  • SQL syntax is `WHERE ... LIMIT`, you can't change the order. What means `first 10 rows`? First by what? – pavel Jun 10 '19 at 23:50
  • @DanniChen . . . Tag your question with the database you are using. – Gordon Linoff Jun 10 '19 at 23:53
  • In PostgreSQL you just put LIMIT 10 at the end of the SELECT statement. Other DBMS might have different syntax. Which one are you using? – Deepstop Jun 10 '19 at 23:51
  • It's unclear to me if you want to get the first 10 rows, or the rows starting with the 11th row (as you wrote "not get the first 10" as well). Please **[edit]** your question (by clicking on the [edit] link below it) and add some [sample data](https://meta.stackexchange.com/questions/81852) and the expected output based on that data as [formatted text](https://meta.stackoverflow.com/a/251362) please. Also: there is no such thing as "the first 10 rows" in the table of a relational database unless you have a column to sort by. –  Jun 11 '19 at 01:16

2 Answers2

0

Based on your sort criteria you can create a SQL like this

SELECT column1, column2, ...
  FROM your table
 ORDER BY sort column
 LIMIT 10

This will give you the top 10 rows from your table based on your sort column

demircioglu
  • 3,069
  • 1
  • 15
  • 22
0

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.