Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL max() only returns 1 row if column has several max values

Tags:

sql

mysql

max

This feels like a stupid question since it should have a simple answer, but I just can't find it. I have a table looking like this:

|-----|---|
|  a  | b |
|-----|---|
| ALA | 2 |
| ASP | 1 |
| SER | 1 |
| VAL | 2 |
|-----|---|

What I need is to get the two rows with the maximum value (I don't know the values in advance) which means that my example above shoud give:

|-----|--------|
|  a  | max(b) |
|-----|--------|
| ALA |   2    |
| VAL |   2    |
|-----|--------|

I'm trying

SELECT a, max(b) FROM table;

but it only gives the first maximum row:

|-----|--------|
|  a  | max(b) |
|-----|--------|
| ALA |   2    |
|-----|--------|

What do I miss?

like image 739
BluePrint Avatar asked Oct 21 '25 15:10

BluePrint


1 Answers

SELECT a,b FROM table
WHERE b = (SELECT MAX(b) FROM table)
like image 114
Pavel Gatnar Avatar answered Oct 24 '25 05:10

Pavel Gatnar