Solved

ANSI Compliant keyword for TOP N clause

Posted on 2004-09-02
5
320 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…

828 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