How to get the value before MAX in SQL?

Posted on

Question :

How can I get the value prior to MAX in SQL?

I have tried MAX -1 but it did not work, because the column values I want to select the value before MAX are not in ascending order.

    

Answer :

The concept of anterior can be relative. I think this solves what you want:

SELECT MAX(coluna) FROM tabela WHERE coluna NOT IN (SELECT MAX(coluna) FROM tabela)

You do a search for the maximum and then do another search in the whole without what you found, so the new maximum will be the previous one.

    

Leave a Reply

Your email address will not be published. Required fields are marked *