With ANSI SQL you cannot make such a query, the best you can hope is select * from employees order by salary and take the first 5 records.
With propietary extensions is possible.
Not everyone knows where to post what when they first visit Javaranch. That is why we have the ability to move threads. Comments regarding such in any unhelpful way should be kept to yourself. We want everyone on Javaranch to have the best experience possible.
Ivan's got your answer. Go with that.
Originally posted by G.MOHAN REDDY:
I need a quary for which to displaying top 5 salary names from Employee Table
[ February 09, 2004: Message edited by: G.MOHAN REDDY ]
In MS Access there is a way to do it, also using T-SQL. Try this webpage.
Welcome to Javaranch. This question has been discussed a bunch of times in the past. Try searching this forum for excel. You will also want to look into ODBC.
Also, I think you hit the reply button instead of the new post button. Just something to be aware of for the future.
Originally posted by Paul Misoni:
If you'd like a more straightforward answer, try..
SELECT NAME, NUMBER
FROM TABLE1 A
WHERE 10 > (SELECT COUNT(*)
FROM TABLE1 B
WHERE A.NUMBER < B.NUMBER)
AND NUMBER IS NOT NULL
ORDER BY NUMBER DESC;
I still not able to get the above query ...can any body pls expalain it with example
it will be gr8 full to me
Originally posted by Amod Bhatia:
can any body pls expalain it with example
it will be gr8 full to me
Amod Bhatia, JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "gr8" instead of spelling out "great" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.
I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.
Please read this for more information.
[ June 04, 2006: Message edited by: Bear Bibeault ]
I still not able to get the above query
It's a really really slow, naive way of solving the problem; for a table with N rows, that query will take on the order of N**2 operations to produce an answer. In other words, it will work fine for a table with maybe 100 rows, and you will be waiting a long time on a table with 100,000 rows. It also is a query for top 10, when the original question was for top 5.
The problem is that, in plain language, that SQL says:
for every number in the table, count the numbers in the entire table that are greater than the current number, and if there are less than 10 numbers in the entire table that are greater than the current number, include the current number in the result.
It would be far better to simply select the rows in the correct order, and use Statement.setMaxRows() to limit the transfered result.
As earlier posts mention, there are also database-specific ways to have a SQL query limit itself. On the Oracle databse, it would be:
Other database has other ways....