Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > TOP n WITH TIES in MySQL
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

TOP n WITH TIES in MySQL

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 03 June 2004

I have this query:

select state, count(*) as customers   
from customer group by state 
order by customers desc limit 2

This works fine, but what if the query returns two values, meaning two records have the same number in the database?


>

Assuming from the LIMIT clause that this is MySQL, your query will return the "top two" rows (top two states by number of customers), and you seem to be concerned about ties. Rightly so. In your specific example, LIMIT 2 returns the top two rows, so if there's a two-way tie for top, you're okay. But if there's a two-way tie for second place, you're not.

Consider a quiz which has thirteen questions. Joe gets all thirteen questions right, Mary gets only five questions right, and everybody else gets twelve right. Who are the top two finishers in this quiz?

Joe13
Tom12
Dick12
Harry12
Curly12
Larry12
Moe12
...12

Notice that everybody is on the list except Mary. (The list is actually a lot longer, but showing all of it would be silly, wouldn't it.)

If we announce the results as "Joe won first prize with a perfect 13 and Tom won second prize with a 12," we will surely hear from some other angry contestants. Rightly so. Perhaps the number of customers by state is not likely to yield as many ties, but we should still address the problem.

So what to do? We can arbitrarily limit the results to two, which is certainly safe if we were showing, say, population by state, where the likelihood of a tie is vanishingly small. My preference is to show the top two results, no matter how many rows have them. In other words, to include ties.

In SQL Server it is possible to say TOP n WITH TIES. End of problem.

In MySQL, at least in versions prior to 4.1 which introduced support for subqueries, you'll have to use LIMIT. Set it high enough so that you feel confident you will return more than the necessary number of rows, or leave LIMIT off altogether and return all 50 states. Then loop over the resulting rows with a scripting language and programmatically detect ties with current/previous logic.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts