Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: a query in Oracle Report
> mo wrote:
> > Why not just use: > > SELECT first_name, last_name > FROM students > WHERE rownum <3 > ORDER BY first_name; > > Saves on a subquery.. ? > > Anthony.
Because it doesn't. The original query orders the results THEN assigns ROWNUM to the ordered result set. Yours does no such thing; thus yours returns an erroneous result. To illustrate the point:
SQL> create table students(first_name varchar2(20), last_name varchar2(20), studentid number);
Table created.
SQL> insert into students values ('Pillsbury','Doughboy',3502375);
1 row created.
SQL> insert into students values ('David','Darwin',999223478);
1 row created.
SQL> insert into students values ('Raul','Ravioli',777777777);
1 row created.
SQL> insert into students values('Almonso','Wannamacher',123456789);
1 row created.
SQL> insert into students values ('Edgar','Quonset',876543321);
1 row created.
SQL> insert into students values ('Morfeo','Quesoverde', 333445555);
1 row created.
SQL> commit;
Commit complete.
SQL> select first_name, last_name
2 from students
3 where rownum < 3
4 order by first_name;
FIRST_NAME LAST_NAME
-------------------- -------------------- David Darwin Pillsbury Doughboy
SQL> select * from
2 (select first_name, last_Name
3 from students
4 order by first_name)
5 where rownum < 3;
FIRST_NAME LAST_NAME
-------------------- -------------------- Almonso Wannamacher David Darwin
SQL> The results are not the same, nor should they be.
David Fitzjarrell Received on Mon Oct 11 2004 - 17:37:28 CDT