đ§Ē SQL Practice: Salary Summaries & Department Counts
Try the following SQL queries using this sample employee data. Practice grouping by departments, counting employees, and calculating salary aggregates.
đ Sample employees
Table:
employee_id | name | department_id | salary |
---|---|---|---|
1 | Arif | 101 | 50000 |
2 | Nasrin | 101 | 52000 |
3 | Rafi | 102 | 47000 |
4 | Ritu | 103 | 60000 |
5 | Faruk | 101 | 51000 |
đ Practice Queries:
1️⃣ Total Salary Paid Per Department
Goal: Find the total salary for each department.
SELECT department_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id;
Expected Output:
department_id | total_salary |
---|---|
101 | 153000 |
102 | 47000 |
103 | 60000 |
2️⃣ Employee Count Per Department
Goal: Count the number of employees in each department.
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id;
Expected Output:
department_id | employee_count |
---|---|
101 | 3 |
102 | 1 |
103 | 1 |
3️⃣ Average Salary of Departments with More Than 1 Employee
Goal: Show average salary for departments with more than 1 employee.
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 1;
Expected Output:
department_id | avg_salary |
---|---|
101 | 51000 |
đ Tips:
- Use
SUM()
,AVG()
,COUNT()
for aggregation. GROUP BY
groups rows by column.HAVING
filters grouped results (like WHERE, but after grouping).
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ