Solved

ANSI Compliant keyword for TOP N clause

Posted on 2004-09-02
5
330 Views
Last Modified: 2012-08-14
Need to find ANSI-92 compliant keyword for the TOP (n) keyword used in SQL Server Transact SQL?
0
Comment
Question by:dbatrainer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 10

Expert Comment

by:Jay Toops
ID: 11962776
select foo, bar
  from yourtable X
 where ( select count(*)
           from yourtable  
          where foo > X.foo ) < 10

Jay
0
 
LVL 11

Expert Comment

by:SweatCoder
ID: 11965117
use LIMIT in MySQL and maybe others as well.
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 11965963
Which Database are you using??

Jay
0
 

Author Comment

by:dbatrainer
ID: 11966443
comment to JLtoops from dbatrainer.

You're right a subquery would work, but it's resource intensive and I was hoping to find a universal ANSI compliant clause that works the same as TOP (n) for a hetergenous environment including SQL Server 2000, Oracle, Dbase, and others. One that might also allow (with ties).

Thanks for the reply
0
 
LVL 10

Accepted Solution

by:
Jay Toops earned 500 total points
ID: 11968164
There isn't one at least in ANSI-92 ..
thats why i gave you the count query.
thats the most UNIVERSALLY likely to work
... unfortunately its a resource pig.

the other way i thought of was to use a cursor.
But I very much doubt that it would be less expensive than
the query i just gave you.

Jay
0

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question