Medium sql

Average salary by department

Problem

Calculate average salary per department.

Hints
  • GROUP BY department, use AVG(Salary)

Your practice code

Ready — edit the code above and click Run.

Solution

-- Sample schema
CREATE TABLE Employees (Id INT, Name TEXT, DeptId INT, Salary INT);
CREATE TABLE Departments (Id INT, Name TEXT);
INSERT INTO Employees VALUES (1,'Ali',1,90000),(2,'Sara',2,120000),(3,'Raj',1,75000);
INSERT INTO Departments VALUES (1,'Engineering'),(2,'Sales');
SELECT d.Name, AVG(e.Salary) AS AvgSalary
FROM Employees e
JOIN Departments d ON e.DeptId = d.Id
GROUP BY d.Name;

Try solving on your own first, then reveal the official answer.

Explanation

GROUP BY with aggregate functions—SQL interview staple.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details