Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySql max is returning minimum value

enter image description here

I have table rank and inside I have five different columns I want to fetch row record where I have maximun value for instance I have a column phd_ratio so I said:

SELECT * FROM rank HAVING max(phd_ratio)

Meaning I wanted to fetch row record where value was maximum but it returned me very first row where phd_ratio value was minimum so is there any way to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You may either use a subquery to find the maximum phd_ratio:

SELECT *
FROM `rank`
WHERE phd_ratio = (SELECT MAX(phd_ratio) FROM `rank`);

Or use a LIMIT query:

SELECT *
FROM `rank`
ORDER BY phd_ratio DESC
LIMIT 1;

The second version is not suitable if two or more records could be tied for the highest phd_ratio, and you require all ties. In that case, use the first version. Also, as of MySQL 8+, RANK is a reserved keyword, and you should avoid naming your database tables and objects using it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading