Home » SQL & PL/SQL » SQL & PL/SQL » How to fetch last N records from the table?
How to fetch last N records from the table? [message #611934] |
Thu, 10 April 2014 05:04 |
|
Bikash.p
Messages: 11 Registered: April 2014 Location: Banglore
|
Junior Member |
|
|
Suppose in a table 6 record is there.And I want to fetch last 2 records. What is the query?
create table student2(sid int,sname char(10),email varchar(15),fee number(10),dob date,city varchar(10));
insert into student2 values(1,'aaa','bk1@',200,'24-jan-1991','kkp1');
insert into student2 values(2,'bbb','bk2@',300,'04-feb-1981','kkp6');
insert into student2 values(3,'ccc','bk3@',400,'21-mar-1989','kkp5');
insert into student2 values(4,'ddd','bk4@',300,'28-apr-1992','kkp4');
insert into student2 values(5,'eee','bk5@',200,'29-may-1993','kkp3');
insert into student2 values(6,'fff','bk6@',700,'16-jun-1998','kkp2');
commit;
|
|
|
|
Re: How to fetch last N records from the table? [message #611937 is a reply to message #611934] |
Thu, 10 April 2014 05:09 |
John Watson
Messages: 8931 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Welcome to the forum. Please read our OraFAQ Forum Guide and please read How to use [code] tags and make your code easier to read
Is this a college homework question? If so, you need to show the SQL you have tried so far. And by the way, you need to use TO_DATE in your INSERT statements:
orclz> create table student2(sid int,sname char(10),email varchar(15),fee number(10),dob date,city varchar(10));
Table created.
orclz> insert into student2 values(1,'aaa','bk1@',200,'24-jan-1991','kkp1');
insert into student2 values(1,'aaa','bk1@',200,'24-jan-1991','kkp1')
*
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
orclz>
|
|
|
|
Re: How to fetch last N records from the table? [message #611947 is a reply to message #611946] |
Thu, 10 April 2014 06:06 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Bikash Patra wrote on Thu, 10 April 2014 11:54Please Ignore that Error. IN sql devloper it will work.
In your sql developer it might work, but that doesn't mean it's going to work in anyone elses, since it depends on default date formats which are language/region specific and can be changed. If you're learning you should learn to always to_date dates and include a format mask.
Read this http://edstevensdba.wordpress.com/category/nls_date_format/
Bikash Patra wrote on Thu, 10 April 2014 11:54
Anyways can you give me the solution?
Roachcoach already did.
|
|
|
|
|
Re: How to fetch last N records from the table? [message #611954 is a reply to message #611934] |
Thu, 10 April 2014 07:20 |
|
rc3d
Messages: 213 Registered: September 2013 Location: Baden-Württemberg
|
Senior Member |
|
|
You can add a column with "INSERT"-date.
for example
CREATE TABLE TEST
(
APPLICATIONNAME VARCHAR2(120 BYTE),
PRODUCTNAME VARCHAR2(70 BYTE),
HOSTNAME VARCHAR2(60 BYTE),
VIRTUALHOST VARCHAR2(60 BYTE),
HARDWARENAME VARCHAR2(60 BYTE),
HWMODEL VARCHAR2(60 BYTE),
STANDORT VARCHAR2(60 BYTE),
STATUS VARCHAR2(60 BYTE),
OS VARCHAR2(60 BYTE),
OSVERSION VARCHAR2(60 BYTE),
IMPORTED DATE DEFAULT sysdate
)
see
IMPORTED DATE DEFAULT sysdate
Then you can order with insert date column and limit output with rownum.
|
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Thu Apr 25 19:51:32 CDT 2024
|