To solve the problem of finding the average salary of employees in each city, we can use SQL's GROUP BY clause combined with the AVG() aggregate function. Here's the step-by-step solution:
Approach
- Group by City: Use the
GROUP BYclause to group employees based on their city. - Calculate Average Salary: For each city group, compute the average salary using the
AVG()function. - Alias the Result: Rename the average salary column for clarity (e.g.,
AverageSalary).
SQL Query
Assuming the table is named Employees, the query is:
SELECT City, AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY City;
Explanation
GROUP BY City: Groups all rows with the same city together.AVG(Salary): Computes the average salary for each city group.AS AverageSalary: Renames the aggregated column to make the output more readable.
This query will return a list of cities along with their respective average employee salaries. Only cities with at least one employee will be included in the result.
Answer:
SELECT City, AVG(Salary) AS AverageSalary FROM Employees GROUP BY City;


作者声明:本文包含人工智能生成内容。