Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple SQL query for Min and Max

Tags:

sql

So I am trying to find the age of the oldest and youngest male and female patients along with the average age of male and female patients in the clinic I work. I am new to SQL but essentially it all comes from one table I believe which is named "Patients". Inside the Patients table there is a column for Gender which has Either M for male or F for female. There is also an age column. I am guessing this is really simple and I am just making this to complicated but could someone try to help me out?

My Query is pretty limited. I know that if you do something along the lines of:

Select 
    Min(AGE) AS AGEMIN, 
    MAX(AGE) AS AGEMAX
From Patients
like image 579
Zi0n1 Avatar asked Feb 18 '23 14:02

Zi0n1


1 Answers

Use the GROUP BY clause:

    select * from @MyTable
  • M 10
  • M 15
  • M 20
  • F 30
  • F 35
  • F 40

    select Gender, MIN(Age), MAX(Age), AVG(Age)
    from @MyTable
    group by Gender
    
  • F 30 40 35

  • M 10 20 15
like image 79
congusbongus Avatar answered Feb 20 '23 04:02

congusbongus