[英]How do I write SQL query for average?
I have recently attended an interview on SQL. Below is the question:最近参加了SQL的面试,问题如下:
Write a query to retrieve city name, state name, city population and return the following calculations along with the before mentioned fields.编写查询以检索城市名称、state 名称、城市人口并返回以下计算结果以及上述字段。
City Table:城市表:
City_Name![]() |
State_NAme ![]() |
Population![]() |
---|---|---|
Baltimore![]() |
Maryland![]() |
30000 ![]() |
College Park![]() |
Maryland![]() |
18000 ![]() |
Columbia![]() |
Maryland![]() |
20000 ![]() |
Boston![]() |
Massachusetts![]() |
35000 ![]() |
Malden![]() |
Massachusetts![]() |
10000 ![]() |
Dover![]() |
Delaware![]() |
20000 ![]() |
Jersey City ![]() |
New Jersey![]() |
35000 ![]() |
I have tried below query but I didnot get desired output. Can anyone help me with correct query?我试过下面的查询,但没有得到所需的 output。任何人都可以帮助我进行正确的查询吗?
select * from city_table;
select state_name, sum(population)/count(city_name) as average_city_pop
from city_table
group by state_name;
You need to explore sql more, learn about aggregation functions and joins.您需要进一步探索 sql,了解聚合函数和连接。 Your query is correct in obtaining the average population.
您的查询在获取平均人口方面是正确的。 You need to join it to the original table to get your result.
您需要将它加入到原始表中以获得结果。
with avg_city_pop as (select state_name, avg(population) as avg from city group by state_name)
select c.*, acp.avg as average_city_population, abs(acp.avg-c.population) as difference from city c inner join avg_city_pop acp on c.state_name = acp.state_name;
For MySQL versions before 8.0, we can use a correlated subquery to get the average population for each state, which can alss be used to calculate the gap between city population and state average population.对于8.0之前的MySQL版本,我们可以使用相关子查询得到每个state的平均人口,也可以用来计算城市人口与state平均人口的差距。 Note, the
abs
function is optionally used to always return a positive number.请注意,可选地使用
abs
function 始终返回正数。 Omit it if necessary.如有必要,请省略它。
select city_name,state_name,(select avg(population) from city where state_name=c.state_name group by state_name ) as average_pop ,
abs(population - (select avg(population) from city where state_name=c.state_name group by state_name )) as gap_pop
from city c;
select
city_name,
state_name,
population,
avg(population) over (partition by state_name) as Avg_pop,
population-avg(population) over (partition by state_name) as Difference
from city;
see sqlfilldle: https://www.db-fiddle.com/f/tVxdH6bQvsu48umWgLmrQB/0参见 sqlfilldle: https://www.db-fiddle.com/f/tVxdH6bQvsu48umWgLmrQB/0
This needs MySQL 8.0+, because over_clause
is available since 8.0.这需要 MySQL 8.0+,因为
over_clause
从 8.0 开始可用。
output: output:
city_name![]() |
state_name![]() |
population![]() |
Avg_pop![]() |
Difference![]() |
---|---|---|---|---|
dover![]() |
delaware![]() |
20000 ![]() |
20000.0000 ![]() |
0.0000 ![]() |
baltimore![]() |
maryland![]() |
30000 ![]() |
22666.6667 ![]() |
7333.3333 ![]() |
college park![]() |
maryland![]() |
18000 ![]() |
22666.6667 ![]() |
-4666.6667 ![]() |
columbia![]() |
maryland![]() |
20000 ![]() |
22666.6667 ![]() |
-2666.6667 ![]() |
boston![]() |
massachussets![]() |
35000 ![]() |
22500.0000 ![]() |
12500.0000 ![]() |
molden![]() |
massachussets![]() |
10000 ![]() |
22500.0000 ![]() |
-12500.0000 ![]() |
jersey city ![]() |
new jersey![]() |
35000 ![]() |
35000.0000 ![]() |
0.0000 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.