List:General Discussion« Previous MessageNext Message »
From:Dan Nelson Date:January 22 2007 7:07am
Subject:Re: SQL Query Question
View as plain text  
In the last episode (Jan 22), Adam Bishop said:
> If I have a dataset as below:
> Name, Age, Word
> ----------------------------
> Bob, 13, bill
> Joe, 13, oxo
> Alex, 14, thing
> Jim, 14, blob
> Phil, 14, whatsit
> Ben, 15, doodah
> Rodney, 15, thingy
> I want to select the first block where the age is equal, i.e. return
> in the case of the above set, Bob and Joe, and with the same query if
> Bob and Joe didn't exist, return Alex, Jim, and Phil.
> In broken SQL, I want to "SELECT * FROM `table` WHERE 'Age'='Age 1st Row';".

How about:

SELECT * FROM mytable WHERE Age=MIN(age);

The smallest and largest values for a column are alawys available
via MIN() and MAX().  If you had wanted the 2nd smallest, or the top 3,
then you would have needed a subquery.

	Dan Nelson
SQL Query QuestionAdam Bishop22 Jan
  • Re: SQL Query QuestionDan Nelson22 Jan
    • RE: SQL Query QuestionAdam Bishop22 Jan