Skip to content
Advertisement

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.

I need to show something like this:

Any help, please.

Advertisement

Answer

You can write the same subquery to calculate the average age within select list:

Or if your database allows window function you can do this:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement