views:

125

answers:

5

hi

how to do this: select top 1 Fname from MyTbl

in oracle 11g ?

thank's in advance

+4  A: 

Have a look at How does one select the TOP N rows from a table

del.ave
+3  A: 

If you want just a first selected row you can:

select fname from MyTbl where rownum = 1

you can also use analytic functions to order and take the top x

select max(fname) over (rank() order by some_factor) from MyTbl
mcpeterson
Not sure what the second example is supposed to achieve...
Patrick Marchand
This is good if you only want 1 row and don't care which. If you want specific rows, like the most recent record, you need to do the sort in a subselect, like Vash's answer. Oracle assigns rownums before the sort.
Scott Bailey
@Scott yup. that is correct. And Patrick, good point I think the syntax is incorrect on that. It really should be a keep over (dense_rank() last... )
mcpeterson
The difference between the first and second example is that the first one selects A row (any row, with no order). The second example gets the value of the first row, without doing an order inner query (as per examples below).
JulesLt
+1  A: 
select * from (
    select FName from MyTbl
)
where rownumn <= 1;
ar
+3  A: 
SELECT *
  FROM (SELECT * FROM MyTbl ORDER BY Fname )
 WHERE ROWNUM = 1;
Vash
This answer correctly gets the TOP row (orders the results before restricting on ROWNUM).
JulesLt
This answer is not an exact translation - the original query doesn't have an ORDER BY, nor does it return all columns in the table.
OMG Ponies
I stand corrected (see below). Will switch votes once time is up.
JulesLt
+2  A: 

Use:

SELECT x.*
  FROM (SELECT fname 
          FROM MyTbl) x
 WHERE ROWNUM = 1

If using Oracle9i+, you could look at using analytic functions like ROW_NUMBER() but they won't perform as well as ROWNUM.

OMG Ponies
Nice answer but contains a tiny typo. Where you say Oracle9i+ shouldn't that be 8i? http://download-west.oracle.com/docs/cd/A87860_01/doc/server.817/a76994/analysis.htm#11237
carpenteri
@carpenteri: True, analytics were available in 8i - can't remember the details of, but analytics weren't really available to the public until 9i.
OMG Ponies
Small comment - Vash's answer below includes an ORDER BY on the inner query which is critical if you want the TOP value of fname, rather than 'first' (which can be anything, most likely first row inserted).Might be worth an edit?
JulesLt
@JulesLt: The query provided by the OP doesn't include an ORDER BY, so this is answer represents and exact translation to Oracle syntax.
OMG Ponies
My misunderstanding of the SQL SERVER TOP syntax (erroneously presumed that it was similar to FIRST in RANK, not ROWNUM). Voted up.
JulesLt
@JulesLt: No worries, likely part of why `TOP` wasn't accepted as the ANSI means of limited the resultset outside of filtration.
OMG Ponies
@OMGPonies: analytics were available in `SQL` but not in `PL/SQL` in `8i`. You had to use `EXECUTE IMMEDIATE` to use them in a `PL/SQL` block.
Quassnoi
@Quassnoi: Oh? I think it was APC who said that analytics were only available on a particular version of 8i. So the dynamic SQL would circumvent the PLSQL evaluation?
OMG Ponies
@OMGPonies: `8` and `8i` were different versions, and only the latter supported the analytics (since `R2`, AFAIR). `SQL` and `PL/SQL` had different parsers, the procedural one did not understand analytics and some other things. `EXECUTE IMMEDIATE` was parsed with `SQL` parser. They were merged in `9i`.
Quassnoi
@OMGPonies: when `8i R2` was released in 2000, we had a meeting on which I insisted on upgrading *all* our clients using previous versions (over `200` installations by that time) and stop supporting `8`. Developers in that company still consider it the most important solution, since the next end of support happened only in `2009`.
Quassnoi
@Quassnoi: Yeah, I can see how a consistent approach would be best--for both development and support. I can't remember if it was 8 or 8i that we used in school, but we certainly weren't using analytics or dynamic SQL.
OMG Ponies