Home > Software engineering >  How to use MAX() function in SELECT and not have that included in the results
How to use MAX() function in SELECT and not have that included in the results

Time:03-26

I have already asked this before, but as is tradition here in S.O., someone rushed in to automatically close my question without even paying attention to notice the similar questions had already been addressed, and DID NOT ANSWER MY QUESTION. For this reason I am opening it again, and hopefully this time it is actually clear.

I'm trying to run a query that returns a row result with the max value Y, but I don't want that extra column included in the results, and I want to use the MAX() function.

I've searched SO for a solution for this, but haven't found any that would fit my requirements, or the suggested similar answers in my previous one provided a solution to this.

Example query:

    SELECT *, MAX(Score)
    FROM Table1

Result:

Name                       Score    MAX(Score)
Werribee Open Range Zoo      6          6

Snippet example http://sqlfiddle.com/#!5/52b4e/3

My question is, how can I get the above result WITHOUT the max(score) column included and still use the MAX() function in the SELECT?

AGAIN to make sure this is clear; How can I use the MAX() function in the SELECT and not have that column included in the results returned?

I am not looking for workarounds that use the MAX in FROM, nor am I looking for workarounds that use ORDER BY LIMIT 1, etc. I am looking for a way to use MAX() in SELECT and that column specifically not show up in the results.

I also tried doing this, but it does not work:

    SELECT *
    FROM (SELECT * FROM Atable WHERE someValue = 'X') as SubQ
    WHERE score = MAX(SubQ.score) <-- does not accept this

Note: I am using this query in Android Room library, which is limited in what kind of queries can be done.

CodePudding user response:

Select *
From table
Where score=(select max(score) from table)

CodePudding user response:

Apparently there is no way of doing this the way I want, that much has been clear.

  • Related