November 16, 2011 at 6:13 am
Hi Friends,
how do i select last 5 rows without any sorting order?
Thanks,
Charmer
November 16, 2011 at 6:20 am
How will you validate they are last 5? (if there is no ORDER BY clause)
November 16, 2011 at 6:21 am
Charmer (11/16/2011)
Hi Friends,how do i select last 5 rows without any sorting order?
you cannot.
without an order by, there is no first and last.
SELECT TOP 5 * FROM SOMETABLE will give you 5 rows, like you ask, but no Order By = logical mistake.
Lowell
November 16, 2011 at 6:30 am
ok, i understood...could you temme how top 5 works? in which order it works..?
Thanks,
Charmer
November 16, 2011 at 6:36 am
Random Order...
INSERT TOP (2) INTO Table2 (ColumnB)
SELECT ColumnA FROM Table1
ORDER BY ColumnA;
The ORDER BY clause in the previous query references only the rows that are returned by the nested SELECT statement. The INSERT statement chooses any two rows returned by the SELECT statement. To make sure that the top two rows from the SELECT subquery are inserted, rewrite the query as follows.
INSERT INTO Table2 (ColumnB)
SELECT TOP (2) ColumnA FROM Table1
ORDER BY ColumnA;
November 16, 2011 at 6:41 am
All rows must always be treated as being in a random order.
Until sorted by "order by".
Rgds,
Dave.
November 16, 2011 at 6:43 am
Try this..
select * from book where id not in (select top((select count(*) from book) - 5) id from book )
Ryan
//All our dreams can come true, if we have the courage to pursue them//
November 16, 2011 at 6:45 am
this query does not work....it gives no records....
Thanks,
Charmer
November 16, 2011 at 6:50 am
Random Order...!!!?
but how come it gives same records every time when we execute the query..?
it must change the order right?
Thanks,
Charmer
November 16, 2011 at 6:53 am
Charmer (11/16/2011)
Random Order...!!!?but how come it gives same records every time when we execute the query..?
it must change the order right?
Fine.. Delete few rows & insert same rows again in your table. Query them again. You will see the difference.
November 16, 2011 at 6:54 am
Did you just copy and paste the query and ran?
You need to change based on tables available in your database. I ran it and worked fine for me as there was a table name book in my database.
Ryan
//All our dreams can come true, if we have the courage to pursue them//
November 16, 2011 at 6:58 am
yes....i understood now....Thanks Dev...
Thanks,
Charmer
November 16, 2011 at 7:00 am
Sumanta Roy (11/16/2011)
Did you just copy and paste the query and ran?You need to change based on tables available in your database. I ran it and worked fine for me as there was a table name book in my database.
yes i changed it according to my table set....but it ain't given the records....
Thanks,
Charmer
November 16, 2011 at 7:01 am
yes....i understood now....Thanks Dev...
Most Welcome 🙂
November 16, 2011 at 7:12 am
Hi Dev...did you check out with the query recommended by sumantaroy..?
i see no records as output...
Thanks,
Charmer
Viewing 15 posts - 1 through 15 (of 57 total)
You must be logged in to reply to this topic. Login to reply