Simple SQL query for Min and Max - 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

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

Here you go
SELECT gender, AVG(age) as avgage, MAX(age) as maxage, MIN(age) as minage
FROM patients
group by gender;

Related

How to show the average and use it like condition in the same query?

I am working with a data base and I need to show the people who are older than the age average in a city comparing their age against that average. My code shows the people who is older than the average....but I can't show the average of all the people (it's allways the same number) in each line.
SELECT name, age FROM people
WHERE age > (SELECT AVG(age) FROM people);
I need to show something like this:
name age average
Mick 18 17.5
Sarah 25 17.5
Joe 38 17.5
Any help, please.
You can write the same subquery to calculate the average age within select list:
SELECT name, age, (SELECT AVG(age) FROM people) average FROM people
WHERE age > (SELECT AVG(age) FROM people);
Or if your database allows window function you can do this:
select name,age,average from
(
SELECT name, age, AVG(age) over() average FROM people
)t where age>average

Ignite: SQL query to calculate probability of a column

Gender
-------
Female
Male
Male
Male
Female
Female
Male
Female
Here i want to calculate probability of gender column and following query i tried, but it's not working.
SELECT (count(*)/(SELECT count(*) from DIABETIC_TOPIC) as probability from DIABETIC_TOPIC group by gender order by gender;
what i missed?
I'd cross join the grouped query on a non-grouped query, and divide them:
SELECT gender, cnt_gender / cnt * 100 AS probability
FROM (SELECT gender, COUNT(*)
FROM diabetic_topic
GROUP BY gender) a
CROSS JOIN (SELECT COUNT(*) AS cnt
FROM diabetic_topic) b

How to get the group that has the MAX value for the grouped column

I have a table KIDS that have a Column AGE.
I want to use an SQL query to get all the records of the oldest kids.
For example: If I have
Name Age
----------
David 10
Dan 10
Leah 8
Hannah 6
I want to get David's and Dan's records.
You can try below -
select * from tablename
where age in (select max(age) from tablename)
use max()
select * from t where age = (select max(age) from t)
You can apply the below code:
select * from old_Records where age =(select max(age) from old_Records)

Distinct on specific columns in SQL

I know someone on here already asked the similar questions. However, most of them still want to return the first row or last row if multiple rows have the same attributes. For my case, I want to simply discard the rows which have the same specific attributes.
For example, I have a toy dataset like this:
gender age name
f 20 zoe
f 20 natalia
m 39 tom
f 20 erika
m 37 eric
m 37 shane
f 22 jenn
I only want to distinct on gender and age, then discard all rows if those two attributes, which returns:
gender age name
m 39 tom
f 22 jenn
You could use the window (analytic) variant of count to find the rows that have a just one occurance of the gender/age combination:
SELECT gender, age, name
FROM (SELECT gender, age, name, COUNT(*) OVER (PARTITION BY gender, age) AS cnt
FROM mytable) t
WHERE cnt = 1
Use the HAVING clause in a CTE.
;WITH DistinctGenderAges AS
(
SELECT gender
,age
FROM YourTable
GROUP BY gender
,age
HAVING COUNT(*) = 1
)
SELECT yt.gender, yt.age, yt.name
FROM DistinctGenderAges dga
INNER JOIN YourTable yt ON dga.gender = yt.gender AND dga.age = yt.age
No matter what, you have to tell the database which value to pick for name. If you don't care an easy solution is to group:
SELECT gender, age, MIN(name) as name FROM mytable GROUP BY gender, age HAVING COUNT(*)=1
You can use any valid aggregate for name, but you have to pick something.

SQL query to that limits only a subset of the records

Let's say I have a table of people, with rows name, gender (M/F), and age.
What would a SQL query look like that returns:
all female people
a maximum of 5 male people
people sorted by age
NB. This is a contrived example. Also, Postgres-specific answers welcome.
SELECT name, gender, age
FROM ( SELECT name, gender, age
FROM people
WHERE gender = 'F'
UNION ALL
( SELECT name, gender, age
FROM people
WHERE gender = 'M'
LIMIT 5
)
) x
ORDER BY age
Note the above solution doesn't pick any particular males. Apply an ordering to the male subquery if you want that.
This one orders the males by age before the pruning takes place:
SELECT name, gender, age
FROM ( SELECT name, gender, age
,ROW_NUMBER() OVER (PARTITION BY gender ORDER BY age) gender_count
FROM people
) x
WHERE gender = 'F'
OR gender_count <= 5
BTW, I've found "gender" is usually used for grammatical references. In this case "sex" would have been the terminology I would have used.