How can I select in oracle sql in a Table the first x rows, then the next x and so on? I know I could use TOP/LIMIT, then I get the first x
select a from b limit 150
=> get the first 150 rows.
Edit: Why? I would like to copy the first 150 outputs into a file, then the next 150 into another file and so on...
All replies. select <column list you want> from <your table name> order by ProductName offset 100 rows fetch next 100 rows only; That will skip the first 100 rows (in order by ProductName) and return the next 100 rows.
For example, based on the indicated order, the following query skips the first 50 rows and filters the next 25 rows: SELECT orderid, orderdate, custid, empid FROM Sales. Orders ORDER BY orderdate DESC, orderid DESC OFFSET 50 ROWS FETCH NEXT 25 ROWS ONLY; In other words, the query filters rows 51 through 75.
Select one or more rows and columns Or click on any cell in the column and then press Ctrl + Space. Select the row number to select the entire row. Or click on any cell in the row and then press Shift + Space. To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.
LIMIT 150
or LIMIT 0,150
: first 150 rows
LIMIT 150,150
: next 150 rows
LIMIT 300,150
: next 150 rows
and so on
In Oracle you have the nice rownum
: it is a pseudo column. It numbers the records in a result set. The first record that meets the where criteria in a select statement is given rownum=1
, and every subsequent record meeting that same criteria increases rownum
.
SELECT
a, b
FROM
(SELECT rownum rn, a, b from table WHERE c=some_value ORDER BY some_column)
WHERE
rn BETWEEN 150 AND 300;
(thanks to @Mark Bannister)
If you embed the ORDER BY clause in a subquery and place the ROWNUM condition in the top-level query, then you can force the ROWNUM condition to be applied after the ordering of the rows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With