Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: getting the max value from a DB column

People also ask

How do you find the maximum value of a database?

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 we use Max on a column?

MAX can be used with numeric, character, and datetime columns, but not with bit columns.

Which function returns maximum value of a field column?

The SQL MIN() and MAX() Functions The MAX() function returns the largest value of the selected column.

Which query can be used to extract the maximum?

The SQL MAX function is used to return the maximum value of an expression in a SELECT statement.


Assuming your model name is Bar and it has a column named bar, this should work:

Bar.maximum("bar")

See the excellent Rails Guides section on Calculations for more info.


one more way

Bar.select("Max(bar) as max_bar").first.max_bar