Essential SQL Queries for HR
1. Employee Demographics
- List all employees with department and job role:
SELECT EmployeeID, FirstName, LastName, Department, JobRole
FROM Employees;
- Count employees by department:
SELECT Department, COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department;
- Average age of employees by department:
SELECT Department, AVG(Age) AS AvgAge
FROM Employees
GROUP BY Department;
2. Attrition & Retention
- Employees who left the company:
SELECT EmployeeID, Attrition, Department
FROM Employees
WHERE Attrition = 'Yes';
- Attrition rate by department:
SELECT Department,
SUM(CASE WHEN Attrition = 'Yes' THEN 1 ELSE 0 END) * 100.0 / COUNT(*) AS AttritionRate
FROM Employees
GROUP BY Department;
- Average tenure of employees:
SELECT AVG(YearsAtCompany) AS AvgTenure
FROM Employees;
3. Salary & Compensation
- Average salary by job role:
SELECT JobRole, AVG(MonthlyIncome) AS AvgSalary
FROM Employees
GROUP BY JobRole;
- Top 10 highest-paid employees:
SELECT EmployeeID, FirstName, LastName, MonthlyIncome
FROM Employees
ORDER BY MonthlyIncome DESC
LIMIT 10;
- Salary distribution by department:
SELECT Department, MIN(MonthlyIncome) AS MinSalary,
MAX(MonthlyIncome) AS MaxSalary,
AVG(MonthlyIncome) AS AvgSalary
FROM Employees
GROUP BY Department;
4. Performance & Training
- Employees with high performance rating:
SELECT EmployeeID, FirstName, LastName, PerformanceRating
FROM Employees
WHERE PerformanceRating >= 4;
- Training hours by department:
SELECT Department, AVG(TrainingHours) AS AvgTrainingHours
FROM Employees
GROUP BY Department;
5. Advanced Joins (HR + Department Tables)
- Employee details with department manager:
SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName, d.ManagerName
FROM Employees e
JOIN Departments d ON e.DepartmentID = d.DepartmentID;
- Attrition analysis with job satisfaction:
SELECT Department, JobRole, AVG(JobSatisfaction) AS AvgSatisfaction,
SUM(CASE WHEN Attrition = 'Yes' THEN 1 ELSE 0 END) AS AttritionCount
FROM Employees
GROUP BY Department, JobRole;
Key Considerations
- Data Privacy: Always anonymize sensitive employee data before sharing.
- Validation: Ensure HR datasets are cleaned (no duplicates, consistent values).
- Use Cases: These queries support dashboards in Power BI/ for HR analytics.
Comments
Post a Comment