How to get the maximum value of a database table's column programmatically

You can use MySQL MAX function the following way:

https://github.com/magento/magento2/blob/720667e/app/code/Magento/Catalog/Model/ResourceModel/Category.php#L322-L335

Another example from the core:

https://github.com/magento/magento2/blob/d50ee5/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php#L118-L127

The usage of \Zend_Db_Expr is not required, you can just write MAX(sort_order) like in the first example.


See also: