Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the first 150 rows, then the next 150 and so on?

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...

like image 302
sabisabi Avatar asked Mar 09 '12 09:03

sabisabi


People also ask

How do I select next 100 rows in SQL?

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.

Which of the following queries will skip the first 50 rows and filter the next 25 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.

How do I select a certain number of rows?

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.


2 Answers

LIMIT 150 or LIMIT 0,150 : first 150 rows

LIMIT 150,150 : next 150 rows

LIMIT 300,150 : next 150 rows

and so on

like image 132
xbonez Avatar answered Sep 28 '22 09:09

xbonez


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.

like image 23
vulkanino Avatar answered Sep 28 '22 10:09

vulkanino