Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return all rows with the "MAX" value in SQL?

Tags:

mysql

max

I have the following table:

Table: Scores What I have:

+----+-------+
| Id | value |
+----+-------+
|  1 |   300 |
|  2 |   300 |
|  3 |   300 |
|  4 |   100 |
|  5 |   200 |
+----+-------+

What I need:

+----+-------+
| Id | value |
+----+-------+
|  1 |   300 |
|  2 |   300 |
|  3 |   300 |
--------------

How would I grab "all" the top scores id 1, 2, 3 in SQL. I started off using MAX (in mysql) but that only returns one row.

like image 545
RidingRails Avatar asked Aug 28 '12 21:08

RidingRails


People also ask

How do I get all the max values in SQL?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

Can Max return multiple rows?

Answer. Typically, when you have more than one row that contains the minimum or maximum value in a column, the topmost row containing that value will be returned in the result.

What is Max () +1 SQL?

MAX(x) - 1 simply means the max value of x in the table minus one. You can always use parenthesis and aliases ( as some_cool_name ) to make thing clearer, or to change names in the result. But the first syntax is perfectly valid.


2 Answers

SELECT Id, value
    FROM Scores
    WHERE value = (SELECT MAX(value) FROM Scores);
like image 96
Joe Stefanelli Avatar answered Nov 08 '22 13:11

Joe Stefanelli


Use a quick variable:

SELECT @max := max(value) from scores;
SELECT id, value FROM scores WHERE value = @max;

or else: (and I am normally in staunch opposition to sub-queries, but this one's a no-brainer.

SELECT id, value FROM
scores
INNER JOIN (Select max(value) as value from scores) as max USING(value)

Note that these are both preferable to the more basic `WHERE value = (subquery) because for each of them, the query to find the MAX value is executed exactly once (the absolute guarantee of this is why I prefer the variable-based solution). With the subquery version (in the WHERE, not the JOIN), that query is likely to be executed once per row.

I have done some query analyzing with EXPLAIN EXTENDED, and the INNER JOIN method is probably the most succinct and optimal of all suggestions (supposing that you are in an environment where using MySQL variables is too cumbersome; I still think it is the cleanest).

performance:

Since some interesting discussion took place, I decided to really dig in and evaluate these things (overkill, I know, but fun and useful knowledge on bigger issues). There is a bit of an analysis trick for detecting full table scans; adding WHERE (@foo := @foo + 1) to the subqueries in question, then setting @foo to 0, running the query, and seeing what @foo is. It's not the end-all be-all query-toll metric, but it can be quite informative about how often you are asking MySQL to evaluate each row. Here are the "scores" with your sample data (lower is better):

  • @ctrahey (both): 5 (scans once to find MAX)
  • @Joe Stefanelli: 25 (scans once per row (5*5))
  • @Jocelyn : 17 (I can't explain this one, but I would love to learn why :-)
like image 20
Chris Trahey Avatar answered Nov 08 '22 11:11

Chris Trahey