oracle find last row in table [duplicate] - sql

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;

Related

How to select only 10 records from the table in jsp?

I'm try to select only 10 row from a table by using limit but it gives me an error,
My query is
SELECT *
FROM table_name
ORDER BY CUSTOMER
LIMIT 10
It gives an error :
ORA-00933: SQL command not properly ended
Can anyone guide me.
You can use ROWNUM :
SELECT *
FROM ( SELECT *
FROM table_name
ORDER BY CUSTOMER) t
WHERE ROWNUM <=10
For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has a ROWNUM of 1, the second has 2, and so on.
Or, since Oracle 12c r1, you can use FETCH :
SELECT *
FROM table_name
ORDER BY CUSTOMER
FETCH FIRST 10 ROWS ONLY
FETCH
Use this clause to specify the number of rows or percentage of rows to return. If you do not specify this clause, then all rows are returned, beginning at row offset + 1.
FIRST | NEXT
These keywords can be used interchangeably and are provided for semantic clarity.

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.

SQL , SELECT `column` FROM `table` ORDER BY RAND() LIMIT 10

I have a table that contains a number of items with ids, if I clicked on one of these ids I'd like to display the one I clicked, followed by 9 random entries.
The following query will select 10 rows randomly:
SELECT column FROM table ORDER BY RAND() LIMIT 10
How do I first display the item of my choice, and then the 9 items that were chosen at random?
What is the suitable query for this problem ??
Sounds like you just want a union.
SELECT column FROM table LIMIT 1
UNION ALL
SELECT column
FROM table
ORDER BY RAND()
LIMIT 9
Two things you may want to consider. First, if you don't put a nonrandom order by on the first query there is no guarantee what you will get as your first row or even that it is consistent. Also, you could end up selecting the same first row twice. To avoid that you can try:
SELECT column FROM table ORDER BY SOME_COLUMN LIMIT 1
UNION ALL
SELECT column
FROM table
WHERE COLUMN NOT IN(SELECT column FROM table ORDER BY SOME_COLUMN LIMIT 1)
ORDER BY RAND()
LIMIT 9
I would suggest using multiple conditions in the order by:
SELECT `column`
FROM `table`
ORDER BY (id = SELECTEDID) DESC, RAND()
LIMIT 10;
This will ensure two things. First, your selected row will be first (because of he order by). And second, you will get nine other rows in the 10.

Selecting all columns using distinct against one specific column [duplicate]

This question already has answers here:
SELECT DISTINCT on one column
(7 answers)
Closed 8 years ago.
I appreciate that this question has been asked before but I am struggling to find an answer that will even run within Oracle 10g (10.2.0.5.0)
I have a table called BASIC which contains approximately 70 columns. Currently, I return a specified number of rows using the following code (as an example) - the result being the first 20 members who have a MEMBNO after 5000
SELECT * FROM BASIC WHERE MEMBNO>5000 AND ROWNUM <=20 ORDER BY MEMBNO;
Within the 20 rows returned, several of the rows have the same value in the NINO column
I would like to modify my SELECT statement to return the next 20 rows with distinct/unique NINO values
Simply wrapping a DISTINCT around the * gives me an ORA-00936: missing expression error, plus it would not be as precise as I would like.
Can you try the code below:- I have used analytical query concept to fetch only distinct nino values.
select * from
(SELECT b.*,row_number() over (partition by nino order by MEMBNO ) rn
FROM BASIC b WHERE MEMBNO>5000)
where rn =1 AND ROWNUM <=20 ORDER BY MEMBNO;
Let me know in case you encounter any issues.
I think I have found a solution via another source
This shows the rows where there are duplicates...
select * from basic where rowid not in (select min(rowid) from basic group by nino)
This shows the rows with the duplicate rows removed...
select * from basic where rowid in (select min(rowid) from basic group by nino)
Then I can add my row count and membno filters for the final result...
select * from basic where rowid in (select min(rowid) from basic where membno>6615 group by NINO) and rownum <=20 order by membno;

Using rownum in oracle SQL statement in combination with order by clause

Which of the following two SQL statements will return the desired result set (i.e. the ten rows with Status=0 and the highest StartTimes)?
Will both statements always return the same result set (StartTime is unique)?
SELECT *
FROM MyTable
WHERE Status=0
AND ROWNUM <= 10
ORDER BY StartTime DESC
SELECT *
FROM (
SELECT *
FROM MyTable
WHERE Status=0
ORDER BY StartTime DESC
)
WHERE ROWNUM <= 10
Background
My DBAdmin told me that the first statement will first limit the table to 10 rows and than order those random rows by StartTime, which is definitly not what I want.
From what I learned in that answer, the order by clause of the second statement is redundant and could be removed by an optimizer, which is also not what I want.
I asked a similar question concering the limit clause in a query to a SQLite database and am interested in understanding any differences to the above statements (using limit Vs rownum) used with an oracle db.
Your Second Query will work
Because in the first ,the first ten rows with Status 0 are selected and then the order by is done in that case the first ten rows fetched need not be in the highest order