GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Database  >  SQL
Go To First  |  Previous Question  |  Next Question 
 SQL  |  Question 125 of 143    Print  
last n rows or first n rows
How do you print the last n rows or the first n rows of a table..?


  
Total Answers and Comments: 18 Last Update: January 17, 2010     Asked by: bharaniprasanth 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
March 05, 2008 23:06:04   
ghaffari Member Since: October 2006   Contribution: 1    

RE: last n rows or first n rows
T-SQL :

SELECT TOP n FROM Table_Name;

MySQL:
SELECT * FROM Table_Name LIMIT 0 n;

 
Is this answer useful? Yes | No
March 06, 2008 17:29:21   
Praveen Vaddadi Member Since: January 2008   Contribution: 6    

RE: last n rows or first n rows
For last n rows:

SELECT qty sales
FROM table
WHERE ROWNUM < (n+1)
ORDER BY sales DESC

For first n rows:

SELECT qty sales
FROM table
WHERE ROWNUM < (n+1)
ORDER BY sales

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
March 06, 2008 21:39:02   
bharaniprasanth Member Since: February 2008   Contribution: 46    

RE: last n rows or first n rows
If I use LIMIT it is saying "SQL command not ended properly"; what is the solution..?
 
Is this answer useful? Yes | No
March 06, 2008 21:43:16   
bharaniprasanth Member Since: February 2008   Contribution: 46    

RE: last n rows or first n rows
WEll Order by is not hte solution because you donot want the top 10 values of colums..like top 10 rankers in a class. I want the records to be in place as it is and not sorted...now the last 10 records/rows and first ten rows(this can be acheived throuw row num). please give answer for last ten rows...
 
Is this answer useful? Yes | No
March 10, 2008 19:20:41   
mehta_dipali Member Since: March 2008   Contribution: 8    

RE: last n rows or first n rows
As per my understanding oracle first fetch result set and then it will number records by rownum. So rownum can not be used to get first/last n rows

select *

from tab (select count(*) row_seq_no <select all columns and alias them to column name_1 > from tab group by <all columns>) tab1 (select count(*) total_rows from tab) tab2

where tab.<column_name tab1.<column_name_1> (for all columns) and row_seq_no < 10

(for last 10 rows (tab2. total_rows - row_seq_no <10)

 
Is this answer useful? Yes | No
March 11, 2008 13:55:40   
mehta_dipali Member Since: March 2008   Contribution: 8    

RE: last n rows or first n rows
checked it ...its not working...sorry
 
Is this answer useful? Yes | No
March 12, 2008 01:18:02   
bharaniprasanth Member Since: February 2008   Contribution: 46    

RE: last n rows or first n rows
Got the working one but partial one indeed

select empno from (select empno rownum l from emp) where l > (select max(rownum) - 10 from emp);

this will give you the last ten rows...but there is a problem

we have to specify individual column name or field name we want to extract...like empno empid..But I donot know how to extract all of them at once like we do with

select * from emp; unfortunately select * rownum from emp doesn't work...Hope to find a soultion for this...

 
Is this answer useful? Yes | No
March 12, 2008 10:51:52   
mehta_dipali Member Since: March 2008   Contribution: 8    

RE: last n rows or first n rows
Congrats for getting partial solution...

Doesn't it take like select empno empname (list all columns) rownum from emp?

 
Is this answer useful? Yes | No
March 12, 2008 12:11:26   
bharaniprasanth Member Since: February 2008   Contribution: 46    

RE: last n rows or first n rows
Yes It would But what my table is big and has say about 50 columns...It would waste your time typing them all...there must have been a solution...by the way did u test the query
 
Is this answer useful? Yes | No
March 12, 2008 12:18:22   
mehta_dipali Member Since: March 2008   Contribution: 8    

RE: last n rows or first n rows
yes thats true...there should be alternative..

I can not check your query as I don't have oracle in my system. I have only teradata.

There is nothing like ROWNUM in teradata

 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape