title: MySQL's alternative to T-SQL's WITH TIES
body: <p>I have a table from which I want to get the top N records. The records are ordered by values and some records have the same values. What I'd like to do here is to get a list of top N records, including the tied ones. This is what's in the table:</p>
  
  <pre><code>+-------+--------+
  | Name  | Value  |
  +-------+--------+
  | A     | 10     |
  | B     | 30     |
  | C     | 40     |
  | D     | 40     |
  | E     | 20     |
  | F     | 50     |
  +-------+--------+
  </code></pre>
  
  <p>Now if I want to get the top 3 like so</p>
  
  <pre><code>SELECT * FROM table ORDER BY Value DESC LIMIT 3
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>+-------+--------+
  | Name  | Value  |
  +-------+--------+
  | F     | 50     |
  | C     | 40     |
  | D     | 40     |
  +-------+--------+
  </code></pre>
  
  <p>What I would like to get is this</p>
  
  <pre><code>+-------+--------+
  | Name  | Value  |
  +-------+--------+
  | F     | 50     |
  | C     | 40     |
  | D     | 40     |
  | B     | 30     |
  +-------+--------+
  </code></pre>
  
  <p>I calculate the rank of each record so what I would really like is to get the first N ranked records instead of the first N records ordered by value. This is how I calculate the rank:</p>
  
  <pre><code>SELECT Value AS Val, (SELECT COUNT(DISTINCT(Value))+1 FROM table WHERE Value &gt; Val) as Rank
  </code></pre>
  
  <p>In T-SQL something like this is achievable by doing this:</p>
  
  <pre><code>SELECT TOP 3 FROM table ORDER BY Value WITH TIES
  </code></pre>
  
  <p>Does anyone have an idea how to do this in MySQL? I understand it could be done with subqueries or temporary tables but I don't have enough knowledge to accomplish this. I'd prefer a solution without using temporary tables.</p>