I'll pose the question like this: I have an Access database, and a table with all the individual sales for five...
different salesmen. I want a query that will give me the top ten sales for each salesman.
This query, like so many other queries dealing with "top N" results, contains within it the thorny issue of what to do about ties. I'm not even going to mention the issue beyond saying that you should be prepared for the inevitable questions from your users.
In Access, you can use TOP syntax. This makes the necessary correlated subquery a lot easier to write.
select s.id , s.name , s.sales from salestable s where s.sales in ( select top 10 sales from salestable where id = s.id order by sales desc ) order by s.id , s.sales desc
For More Information
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, SQL Server, DB2, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig Deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.