Question:
How to write a Query to result in the Maximum Value being displayed as a result in SQL.?
mahesh.paluru
2006-05-31 03:44:08 UTC
I am working on a SQL Query for a Table which contains 2 columns (Sno Number, Name Char). I need to get the name associated with the Maximum Sno as a Result.

The Query Which i use is

Select Sno, name from Temp where Sno=Max(Sno)
or
Select Max(Sno) as Sno, name from Temp.

Both are resukling in a Error....

Can u provide me a correct Query
Five answers:
noshyuz
2006-06-13 09:49:41 UTC
SELECT MAX(Sno), Name FROM Temp GROUP BY Name



you have to include the GROUP BY clause when using any of the group functions (MAX(), MIN(), SUM(), etc) so the database knows how to relate the data in the other columns to determine the groups.
Nemi
2006-05-31 11:31:14 UTC
Select Sno, name from Temp

hanving Sno=Max(Sno)
2006-05-31 11:54:53 UTC
Select top 1 [sno], [name] from [Temp] Order By [sno] desc
ShaolinTiger
2006-05-31 10:49:34 UTC
I don't know which DBMS you are using, but as far as pseudoSQL goes it should be something like:



SELECT Name Char from Temp ORDER BY Sno Number ASCEND LIMIT 1
man_at_the_third_row
2006-05-31 10:49:31 UTC
answer:



SELECT Max(Sno), Name FROM Temp GROUP BY Name


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...