Average Salary By Department

MediumSQLDatabaseSorting

Description

Write a SQL query to return each department with its average salary rounded to 2 decimal places, ordered by department ascending. **Output columns (in order):** department, avg_salary

Table:employees
ColumnType
idINT
salaryINT
departmentTEXT

Examples

Input:CREATE TABLE employees (id INT, salary INT, department TEXT); INSERT INTO employees VALUES (1,100,'A'),(2,200,'A'),(3,300,'B');
Output:A|150.0 B|300.0
Explanation:

Salaries are averaged within each department and rounded, returned in department order.

Input:CREATE TABLE employees (id INT, salary INT, department TEXT); INSERT INTO employees VALUES (1,50,'X');
Output:X|50.0
Explanation:

Salaries are averaged within each department and rounded, returned in department order.

Input:CREATE TABLE employees (id INT, salary INT, department TEXT); INSERT INTO employees VALUES (1,10,'D'),(2,20,'D'),(3,40,'D');
Output:D|23.33
Explanation:

Salaries are averaged within each department and rounded, returned in department order.

Constraints

  • Use standard SQL (SQLite).

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.