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| Column | Type |
|---|---|
| id | INT |
| salary | INT |
| department | TEXT |
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.0Explanation:
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.0Explanation:
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.33Explanation:
Salaries are averaged within each department and rounded, returned in department order.
Constraints
- •
Use standard SQL (SQLite).