Ask Experts Questions for FREE Help !
Ask
    alfredlionel's Avatar
    alfredlionel Posts: 9, Reputation: 1
    New Member
     
    #1

    Aug 2, 2007, 06:41 AM
    How to get only the highest and the lowest number as output in a set of data
    Dear sir,
    I would like to know how to get the highest and the lowest ages as answer in a set of data with many range of ages.the output should be with the name place and age of the person with the highest and the lowest age.kindly let me me know the answer.
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #2

    Aug 2, 2007, 06:49 AM
    You can't do it on one query.

    Try SELECT TOP 1 Name, Place, Age
    FROM table
    ORDER BY Age Desc

    Drop the Desc to get the youngest.
    Nosnosna's Avatar
    Nosnosna Posts: 434, Reputation: 103
    Full Member
     
    #3

    Aug 2, 2007, 03:29 PM
    SELECT name, place, age from TABLE order by age DESC LIMIT 1

    This will give the entry with the highest value.

    As Scott said above, remove the DESC to get the lowest value.

    Note that there is a severe limitation to this approach: in any non-trivial data set, there are going to be multiple people with the maximum and minimum ages. To get all of the entries with the highest or lowest value for age in a single query, you can use:

    SELECT name, place, age FROM table WHERE age = (SELECT MAX(age) FROM table) OR age = (SELECT MIN(age) FROM table)
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #4

    Aug 2, 2007, 03:42 PM
    Comments on this post
    alfredlionel disagrees: not got the output needed I need only the highest and the lowest numbet to be displayed

    Excuse me, but that's what the TOP 1 qualifier does. In other SQL dialects TOP might be replaced by a different keyword. To get both highest and lowest you change the sort order.

    You might try understanding the answer before you mistakenly rate it. And James has a very valid point. There is very likely to be multiple people with the same age. Its not a good idea to store age since that changes. Better to store DOB.
    pandydurai's Avatar
    pandydurai Posts: 12, Reputation: 1
    New Member
     
    #5

    Sep 10, 2007, 12:29 PM
    Nosnosa's query is easy.. the other way would be use a union operator which simply combines both queries and returns required values

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Subtract the fraction 7 4/5 from 18 6/15 and reduce to lowest terms [ 2 Answers ]

How do I subtract the fraction 7 4/5 from 18 6/15 and reduce to lowest terms:eek:

How to get the highest number only as output in sql [ 2 Answers ]

Dear sir , I would like to know how to get the highest number as display.Fpr example a set of ages are in the database.I need only the highest age as output

Lowest Common Demoninator [ 5 Answers ]

What is the lowest common demoninator of 1/3, 1/5, 1/20, and 1/50?

Why 6" above flood rim of highest fixture? [ 7 Answers ]

hey tom, how goes it? I just completed my cast to pvc conversion of my attic reomodel. I did revent all my existing fixtures up so that they are 6" above the flood rim of my 2nd floor lav. My question is why? :confused: I can't seem to come up with a decent reason. Could you enlighten me? Also,...

Jordan's lowest scoring game with Bulls? [ 1 Answers ]

Does anyone know: a) when this was? b) how many he scored? c) who was the other team? And d) who played defense on him? It's bugging the heck out of my roommate and I and Googled for about 30 minutes trying to find the answer. Thanks.


View more questions Search