We help IT Professionals succeed at work.

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?
Comment
Watch Question

AnujSQL Server DBA
Top Expert 2011

Commented:
LIMIT 10
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
which level of ansi sql?

in sql2003 you get window functions like row_number()
sql2008 has fetch first
Top Expert 2011

Commented:
LIMIT 10 is mysql flavor

I would say,

Select top 10 fieldname from your table
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
anujnb,

 I don't think the LIMIT clause is part of any ANSI standard as yet.
 If it is, can you specify which one?
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
sammySeltzer,

the asker was looking for an ANSI equivalent to sqlserver TOP
Top Expert 2011

Commented:
LIMIT is also postgres db dialect
Top Expert 2011

Commented:
ok, I see sdstuber
AnujSQL Server DBA
Top Expert 2011

Commented:
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.
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
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.

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"?
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012
Commented:
DB2 supports FETCH FIRST


select  * from tableName
fetch first 10 rows only
Top Expert 2011

Commented:
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.
SQL Server DBA
Top Expert 2011
Commented:
For DB@ its Fetch First

  SELECT *
  FROM Table
  ORDER BY EmpID
  FETCH FIRST 10 ROWS ONLY
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
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
Top Expert 2011

Commented:
i missed the first in error, sorry.

typing fast

Author

Commented:
ahh...

SELECT column_name(s)
FROM table_name
LIMIT number

Yeah that's MySql

Author

Commented:
select * from (
select d.*, row_number() over(order by something)  as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4

..in this one is the "something" in over(order by something) supposed to be a unique field?>
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
it doesn't have to be unique, but if it is, it'll ensure your ordering is repeatable.


for example

X Y
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

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
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
use FETCH FIRST as shown above

http:#a37715797

Author

Commented:
Thanks, sorry didn't try that one at first.
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
wouldn't a split be in order?

I posted the same thing earlier.  

Also, the question asked was about ANSI SQL not db2
Top Expert 2011

Commented:
Actually, everyone posted similar solutions ; go figure!
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
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.

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.

Author

Commented:
_alias99,

Can the thread be reopened please for point reallocation?

Author

Commented:
Sorry this took so long...