user3857870 user3857870 - 3 months ago 14
SQL Question

I want to get a SQL query for my asp.net

Here's the table:

firstname |lastname |Gender | Age
fname1 |Lname1 | male | 23
fname2 |Lname2 | male | 22
fname3 |Lname3 | male | 20
fname4 |Lname4 | female | 19
fname5 |Lname5 | female | 22
fname6 |Lname6 | female | 17


I want to select only 1 value such that when I set
gander = male
I should get the first and last name of the male with lowest age.

If I want to select the second lowest age person of
gender = male
I should also get that one, and similarly for female.

Answer

For Lowest

SELECT * FROM `test` WHERE Gender='male' ORDER BY Age ASC LIMIT 0,1;

For Second Lowest

SELECT * FROM `test` WHERE Gender='male' ORDER BY Age ASC LIMIT 1,1;