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 using MIN with other attributes to match

This is likely a very simple solution, but I just have no clue. I know I probably have to use GROUP BY, but I don’t know what comes after that. Anyway, the code below should output an item that has the lowest available quantity, and show the itemID and name of item that has the lowest quantity.

SELECT ItemID, Inventory_Name, MIN(AvailableQuantity) FROM Inventory;

>Solution :

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

You have two options:

 SELECT ItemID, 
        Inventory_Name, 
        AvailableQuantity 
 FROM Inventory
 ORDER BY AvailableQuantity  ASC
 LIMIT 1;

But this doesn’t handle ties, so you could use a subquery:

SELECT ItemID, 
        Inventory_Name, 
        AvailableQuantity 
 FROM Inventory
 WHERE AvailableQuantity in (SELECT MIN(AvailableQuantity) FROM Inventory i2);
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