Selecting specific row number in sql [duplicate] - sql

This question already has answers here:
Row Offset in SQL Server
(17 answers)
T-SQL Skip Take Stored Procedure
(2 answers)
Closed 9 years ago.
Is any way that I could select specified number of rows in SQL Server? Like on my first query, I wanted to get rows 1-5, then next is rows 6-10, then onwards? Thank you in advance for your answers :)

For SQL Server 2005+ (set #startRow and #endRow):
SELECT OrderingColumn
FROM (
SELECT OrderingColumn, ROW_NUMBER() OVER (ORDER BY OrderingColumn) AS RowNum
FROM MyTable
) AS MyDerivedTable
WHERE MyDerivedTable.RowNum BETWEEN #startRow and #endRow
SQL fiddle example: http://sqlfiddle.com/#!3/b4b8c/4

For SQL Server 2012, try this (simply set the offset)
SELECT *
FROM MyTable
ORDER BY OrderingColumn ASC
OFFSET 0 ROWS
FETCH NEXT 5 ROWS ONLY
OFFSET:
Specifies the number of rows to skip before it starts to return rows from the query expression.
FETCH NEXT:
Specifies the number of rows to return after the OFFSET clause has been processed.
Definitions of OFFSET and FETCH NEXT are from here.
Query 1:
Offset 0 => 1-5
Query 2:
Offset 5 => 6-10, etc.
SQL fiddle example: http://sqlfiddle.com/#!6/b4b8c/2

Related

oracle find last row in table [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 2 years ago.
hey i have an database project and i want to get the last row from my table gadgets, for the first row i wrotte this and it worked
SELECT *
from Gadgets
where ROWNUM =1
but for the last index for example 100 it doesn't work
SELECT *
from Gadgets
where ROWNUM =100
how i have to writte my comand that i will work, i work on oracle
There is no such thing as a "first" or "last" row in the table. SQL tables represent unordered sets (technically multi-sets because duplicates are allowed). You need an order by.
If you have an incremental id or created date, then you can use that to define "first" or "last". For the "first" row:
select t.*
from t
order by <ordering column>
fetch first 1 row only;
And for the "last" row:
select t.*
from t
order by <ordering column> desc
fetch first 1 row only;

Query limit in oracle database [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 4 years ago.
I am trying to create query with kind of limit.
SELECT * FROM ALARMS WHERE OBJECT_ID=0 AND TIMESTAMP<=1525677504171 AND ROWNUM<=51 ORDER BY TIMESTAMP DESC
I use rownum but first it makes a limit and then it select ordered by. However I need to order first and then limit. I found here enter link description here that I should use FETCH FIRST 51 ROWS ONLY. Unfortunately it doesn't work.
SELECT * FROM ALARMS WHERE OBJECT_ID=0 AND TIMESTAMP<=1525677504171 ORDER BY TIMESTAMP DESC FETCH FIRST 51 ROWS ONLY;
It throws me following error:
SQL Error [933] [42000]: ORA-00933: SQL command not properly ended
oracle.jdbc.OracleDatabaseException: ORA-00933: SQL command not properly ended
You miss ONLY at the end. This syntax is available since 12c R1 if I remember correctly. Which Oracle version you use?
SELECT * FROM ALARMS WHERE OBJECT_ID=0 AND TIMESTAMP<=1525677504171 ORDER BY TIMESTAMP DESC FETCH FIRST 51 ROWS ONLY
;
Edit:
Since your version is 11g then try to use such syntax (I hope it will work;))
SELECT *
FROM (
SELECT a.*,ROW_NUMBER() OVER(ORDER BY TIMESTAMP DESC) rcnt
FROM ALARMS A WHERE OBJECT_ID=0 AND TIMESTAMP<=1525677504171) src
WHERE src.rcnt <= 51
ORDER BY src.TIMESTAMP desc;
FETCH FIRST is only available since Oracle 12c.
For the rownum approach, use a subquery that contains order by, then limit the rows in the enclosing query:
SELECT * FROM (
SELECT * FROM ALARMS WHERE OBJECT_ID=0 AND TIMESTAMP<=152567750417
ORDER BY TIMESTAMP DESC
) dt
WHERE ROWNUM<=51

SQL Command error: Incorrect syntax near '4' [duplicate]

This question already has answers here:
LIMIT 10..20 in SQL Server
(15 answers)
Closed 5 years ago.
I get a "Incorrect syntax near '4'." while executing this command :
#"SELECT * FROM [Table] OFFSET 4 LIMIT 2;"
what is the probem here?
The SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause. MySQL
supports the LIMIT clause to select a limited number of records, while
Oracle uses ROWNUM.
SQL Server / MS Access Syntax:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
Example
SELECT TOP 2 *
FROM [Table]
There is no LIMIT in SQL Server. You use FETCH:
Without an ORDER BY, the OFFSET is meaningless, so you can just do
SELECT TOP 2 t.*
FROM [Table] t;
If you do have an ORDER BY:
SELECT t.*
FROM [Table] t
ORDER BY ?
OFFSET 4 ROWS FETCH FIRST 2 ROWS ONLY;
The ? is a placeholder for the name of the column you want to sort by.

Getting limited result from oracle in order [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 8 years ago.
I have a set of 4000 records,I am only allowed to retrieve 300 records.
I am doing pagination on the resultset, but since we are limiting the result to 300 i am getting different results for each DB hit.
So is there any option to get the ordered first 300 of the 4000 records, without getting the entire result of 4000 records.
Below is the query:
select id from table where name='ronaldo' and rownum <= 300 order by id asc;
The problem is the query is processed in the following order:
The FROM/WHERE clause goes first.
ROWNUM is assigned and incremented to each output row from the FROM/WHERE clause.
SELECT is applied.
GROUP BY is applied.
HAVING is applied.
ORDER BY is applied.
For that reason, your query as written will return a random 300 records.
You'll need to do a nested query, something like this:
select * from
(select * from table
where name='ronaldo' order by id asc)
where ROWNUM <= 300;
Also, if you're using Oracle 12c or higher, you can use the completely non-standard FETCH FIRST syntax:
SELECT *
FROM table
WHERE name='ronaldo'
ORDER BY id asc
FETCH FIRST 300 ROWS ONLY;
Why they don't support the LIMIT keyword? Because they're Oracle.

Is LIMIT not allow in sql server 2008? [duplicate]

This question already has answers here:
How to use LIMIT keyword in SQL Server 2005?
(3 answers)
Closed 8 years ago.
I have a query
SELECT employeedept, execoffice_status, employee, COUNT(*) AS 'employeetotal', YEAR_cse1 =YEAR(execoffice_date)
FROM CSEReduxResponses
WHERE execoffice_status = 1
GROUP BY employeedept, execoffice_status, YEAR(execoffice_date), employee
order by [YEAR_cse1],
LIMIT 20
Which when I add the LIMIT it gives me a error "Incorrect syntax near '20'.".
Is there another way to get the top 20?
I have
Microsoft SQL Server Management Studio 10.0.2531.0, SQL server 2008.
you are probably looking for top
SELECT TOP 20 employeedept, execoffice_status, employee,
COUNT(*) AS 'employeetotal',YEAR_cse1 =YEAR(execoffice_date)
FROM CSEReduxResponses
WHERE execoffice_status = 1
GROUP BY employeedept, execoffice_status, YEAR(execoffice_date), employee
order by [YEAR_cse1]
If you read the documentation for select, you'll see that it uses the keyword top, whose documentation says that it
Limits the rows returned in a query result set to a specified number of rows or
percentage of rows in SQL Server 2012. When TOP is used in conjunction with the ORDER BY
clause, the result set is limited to the first N number of ordered rows; otherwise, it
returns the first N number of rows in an undefined order. Use this clause to specify the
number of rows returned from a SELECT statement or affected by an INSERT, UPDATE, MERGE,
or DELETE statement.
So you can get what you want by saying something like
select top 20
*
from foo