Skip to content
Advertisement

How do I select all columns that belong to one columns MAX() value in SQL?

I need to get values from different columns (in one record) where one column has a maximum value. How do I select the maximum value of one column and all the other columns from the same record?

This only selects the highest value of the selected column

Advertisement

Answer

I can’t comment on other posters answers yet since my reputation isn’t high enough, Himanshu’s answer is definitely more robust. I’d add that you can use RANK() instead of ROW_NUMBER() in the event that you have two max bid prices that are the same.


You should probably be able to use group by –

The values brought in by var_1, var_2, var_3 would be distinct relative to the max value.

Unless you mean you want one record, you could do the same with the following additions –

This should return only the single highest record.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement