Originally posted by simply_dba This does not guarantee the first row.How about this? select col1 from t1 where rownum=1
In database there is no concept of first row, last row etc... If you are inserting records with timestamp or with sequence only then you can get the first row inserted or last row inserted.
In 8k blocksize database try this
create table test (a char(2000), b char(2000), c char(2000), d char(2000));
insert into test values ('1','b','c','d');
insert into test values ('2','b','c','d');
insert into test values ('3','b','c','d');
select a from test where rownum=1; -- what is value of a shown by this query?
delete from test where a='1';
commit;
insert into test values ('4','b','c','d');
select a from test where rownum=1; -- what is value of a now shown by this query?
Experience is a hard teacher because she gives the test first, the lesson afterwards.
Yes, but the statement is more likely to fetch the answer the original poster wanted than using min()
WHERE ROWNUM = 1 means get me one random row from the query. The row returned can and will change for any number of reasons. Bonker demonstrated one way (delete/insert), changing access paths is another (e.g. Oracle decides to use an index instead of a full scan). Another reason would be simply order of insertion.
Code:
SQL> CREATE TABLE t (col1 VARCHAR2(1));
Table created.
SQL> INSERT INTO t VALUES ('e');
1 row created.
SQL> INSERT INTO t VALUES ('d');
1 row created.
SQL> INSERT INTO t VALUES ('c');
1 row created.
SQL> INSERT INTO t VALUES ('b');
1 row created.
SQL> INSERT INTO t VALUES ('a');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT REPLACE(col1, ' ')
2 FROM (SELECT SYS_CONNECT_BY_PATH(col1,' ') col1, level
3 FROM t
4 START WITH col1 = (SELECT MIN(col1) FROM t)
5 CONNECT BY PRIOR col1 < col1
6 ORDER BY level DESC)
7 WHERE rownum = 1;
REPLACE(COL1,'')
-----------------
abcde
SQL> SELECT REPLACE(col1, ' ')
2 FROM (SELECT SYS_CONNECT_BY_PATH(col1,' ') col1, level
3 FROM t
4 START WITH col1 = (SELECT col1 FROM t WHERE rownum = 1)
5 CONNECT BY PRIOR col1 < col1
6 ORDER BY level DESC)
7 WHERE rownum = 1
REPLACE(COL1,'')
-----------------
e
Bookmarks