Solved

Top N query for all DBMS types

Posted on 2007-11-14
9
980 Views
Last Modified: 2012-05-05
I am looking for a JDBC sql call for a top N query that will work with any DBMS type.

How does one retrieve N first (or least) rows from a record set? For example, how does one find the top five highest-paid employees in a given department? This attached code snipper would work for Oracle, but not SQL Server

I am not looking for the SQL Server and DB2 equavolent, but insight into a cronic application SQL problem. How is this query written to perform well and be DBMS independent? Is the better to approach to code something in the java resultSet layer?

Cheers!
Michael
SELECT *

FROM   (SELECT   ROWNUM,

                 e.*

        FROM     emp e

        ORDER BY sal DESC)

WHERE  ROWNUM < 6

Open in new window

0
Comment
Question by:mbevilacqua
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20285647
SQL Server has no rownum and uses

SELECT   TOP 5
e.*
FROM     emp e
ORDER BY sal DESC

so I'm not sure how you would make that "work with any DBMS type" with a single statement pattern...
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20285681
in other words:

>How is this query written to perform well and be DBMS independent?
you don't

>Is the better to approach to code something in the java resultSet layer?
yes
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 20285819
This is a DBMS-specific answer as well:
1) With Pervasive.SQL V8, Pervasive PSQL v9, and Pervasive PSQL Summit v10, the same "SELECT TOP 5 * FROM ..." works.
2) With Pervasive.SQL 2000i, Pervasive.SQL 7, or Btrieve 6.15, this syntax does NOT work, as the TOP command is not supported in these older engines.

In short, if you don't want a DBMS-specific answer, then you can NOT do it within a SQL query, no matter what solution you are talking about.  This means that you'll need to ask the engine for the entire row set, and then throw away any elements beyond what you care about.

{comment edited: mbizup, Access ZAPE}
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20285881
BillBach - yes the shortcomings of the digital age. and they say cinemas are moving to digital screens...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mbevilacqua
ID: 20286863
The product is required to support Oracle, SQL Server, DB2, and Sybase. So the answer can be limited to approaches shared across these database management systems.

I am aware of the technique to do Statement.setMaxRows(int max) method to limit the size of the result set.  This has proven to significantly improve the application performance by limiting the number of rows returned in a large query but this still does not work for top n and pagination queries.

One approach we are pursuing is to obtain the database type from the driver connection and then call the DBMS specific TOP N query.

I am looking for others with similiar requirements to share their approach. What other approaches are there outside of using setMaxRows with order by clause?
0
 
LVL 92

Expert Comment

by:objects
ID: 20287993
you could use something like hibernate that handles the different dialetcs for you.
0
 

Author Comment

by:mbevilacqua
ID: 20291521
That is a insightful comment objects and perhaps the right solution. However, we abandoned the use of the hibernate ORM after running into serious performance issues in the DML and went to straight JDBC.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20293110
straight jdbc has no support for what you want, you'll need to implement it yourself.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now