Calculating Average Salary per Department Using a Cursor

This technical problem deals with calculating the average salary for each department using a cursor. Given a table of employees and their salaries, the cursor will iterate over the table and calculate the average salary for each department. The output will be a table with the department ID and the corresponding average salary.

Problem

Write a cursor that will iterate over a table of employees and calculate the average salary for each department.
Example input:
DEPARTMENT_ID | SALARY
1 | 30000
2 | 40000
3 | 50000
Example output:
DEPARTMENT_ID | AVERAGE_SALARY
1 | 30000
2 | 40000
3 | 50000

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
/*

SELECT DEPARTMENT_ID, AVG(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID;

This is optimal because it uses a single query to calculate the average salary for each department.

*/

A.I. Evaluation of the Solution

This solution is correct and demonstrates a good understanding of how to calculate averages in SQL.

Evaluated at: 2022-11-11 16:15:19