Question

How do I go about selecting a MAX value with a nested query?

This is the simple way

 SELECT MAX(Deliveries.QTY) AS QTY 
 FROM Deliveries

Additional info: I have this table Deliveries and one of the columns is Quantity (QTY) and I'd like to get the max value from that column but using some type of nested query.

Was it helpful?

Solution 2

I was trying to achieve the following:

SELECT Suppliers.SNAME
FROM Suppliers
INNER JOIN Deliveries
ON Deliveries.S=Suppliers.S
WHERE QTY=(SELECT MAX(Deliveries.QTY) FROM Deliveries)

Now that I've figured it out I understand that my question needed some more info. Thanks for the help!

OTHER TIPS

Reading between the lines, as you haven't provided many details, I'm assuming you mean you want to find the MAX (or MIN) and also info from the rest of the row. One pattern to do this is:

Select
   t.*
   mt.MaxColumnNane
from
MyTable t
join 
    (SELECT PrimaryKey, MAX(SomeColumnName) AS MaxColumnNane
     FROM MyTable) mt on t.PrimaryKey = mt.PrimaryKey

You would add the appropriate GROUP BY to the inner nested query, depending what you are finding the MAX of.

[Note: I've assumed table has a primary key, but it just needs to be a collection of columns that are unique.]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top