ansi sql equivalent to t-sql "top"

How would you go about writing the ansi equivalent to t-sql's select top n * from tableName?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AnujSQL Server DBACommented:
which level of ansi sql?

in sql2003 you get window functions like row_number()
sql2008 has fetch first
LIMIT 10 is mysql flavor

I would say,

Select top 10 fieldname from your table
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.


 I don't think the LIMIT clause is part of any ANSI standard as yet.
 If it is, can you specify which one?

the asker was looking for an ANSI equivalent to sqlserver TOP
LIMIT is also postgres db dialect
ok, I see sdstuber
AnujSQL Server DBACommented:
Yes, that's right LIMIT is of mysql and it will not work for SQL Server, as @sdstuber suggested you have to use window functions like ROW_NUMBER(), RANK() etc. but these are supported only on SQL Server 2005 and above, for older versions you have to use TOP clause.
LIMIT is not part of ANSI/ISO SQL   depending on the version (up to 2008)

If the asker is looking for sql92  standards then there isn't a way except to procedurally iterate N times through a result set.

If the asker is looking for a later sql standard, then there might be options, which ones, again, depends on the version of ansi defined.

Then, of course, the real trick is whether that standard has been implemented in the target database

I'm sure we could all name umpteen different ways to do it and umpteen databases where each of those methods would fail.  However, none of that is particularly relevant to the question which is what are the ANSI methods.
_Wade_Author Commented:
I'm querying a DB2 database from .NET with oledb. Offhand I don't know what version of ANSI SQL it conforms to.

I've tried some of the suggestions though and none work so far. I might be using the wrong syntax.

select * from bsydtad.docdvlf8 where now_number() = 4 ...didn't work

How do I use "LIMIT"?
DB2 supports FETCH FIRST

select  * from tableName
fetch first 10 rows only
LIMIT will not work for DB2.

It is more like

select * from bsydtad.docdvlf8 where someid = someid order by someid
fetch 10 rows only;

something like that.
AnujSQL Server DBACommented:
For DB@ its Fetch First

  FROM Table

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
db2 does support row_number too,

using it would look something like this...

select * from (
select d.*, row_number() over(order by something)  as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4
i missed the first in error, sorry.

typing fast
_Wade_Author Commented:

SELECT column_name(s)
FROM table_name
LIMIT number

Yeah that's MySql
_Wade_Author Commented:
select * from (
select d.*, row_number() over(order by something)  as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4 this one is the "something" in over(order by something) supposed to be a unique field?>
it doesn't have to be unique, but if it is, it'll ensure your ordering is repeatable.

for example

2 a
3 a
5 a
7 a
11 a
13 a

if you order by Y, then your results could be anything.  If you order by X then your results will be consistent and meaningful

the ordering is required so the function can pick a row to be #1, #2, #3 etc
_Wade_Author Commented:
When I try that I'm getting the following error back. Apparently the row_number() is not supported in the version of DB2 we have. Is there another way?

ERROR: [IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0255 - Function
not supported for query.
Error Code: -255

Query = select
* from (
select d.*, row_number() over(order by doc_id)
 as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4
use FETCH FIRST as shown above

_Wade_Author Commented:
Thanks, sorry didn't try that one at first.
wouldn't a split be in order?

I posted the same thing earlier.  

Also, the question asked was about ANSI SQL not db2
Actually, everyone posted similar solutions ; go figure!
I agree, the db2 bit can be split, I happened to posted first, but they all came in about the same time.  Picking only the last one isn't appropriate.

However, while one of those was the accepted answer, none of db2 references really answer the question that was asked.  Those are really just supplemental info.

So, my point was, if the decision is to ignore the original question about ANSI SQL then I think a split is in order rather than picking the last post that mentioned the db2 feature.

Or, reevaluate the whole thread and split differently among the posts that actually discussed the original question.
_Wade_Author Commented:
I agree sdstuber. I've been under the weather these past few days and have some projects due and I awarded the points very hastily without reviewing the whole post again first. I'll have a look again and split the points. You gave a lot of useful feedback.
_Wade_Author Commented:

Can the thread be reopened please for point reallocation?
_Wade_Author Commented:
Sorry this took so long...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.