------------------------------------------------------------------------------------------------------
--1. List all employees and their departments.
SELECT
emp_id,
department
FROM hr_db;
------------------------------------------------------------------------------------------------------
-- 2. Show the number of employees in each department.
SELECT
department,
COUNT(emp_id) AS emp_count
FROM hr_db
GROUP BY 1;
-----------------------------------------------------------------------------------------------------
-- 3. Retrieve the list of employees who are older than 40.
SELECT
emp_id,
age
FROM hr_db
WHERE age > 40;
-----------------------------------------------------------------------------------------------------
-- 4. Find the average MonthlyIncome of employees in the ‘Sales’ department.
SELECT
department,
ROUND(AVG(monthly_income),2) AS avg_income
FROM hr_db
WHERE department = 'sales'
GROUP BY 1;
-----------------------------------------------------------------------------------------------------
--5. List employees who have been with the company for more than 5 years.
SELECT
emp_id,
years_at_company
FROM hr_db
WHERE years_at_company > 5;
------------------------------------------------------------------------------------------------------
--6. Find the count of male and female employees.
SELECT
gender,
COUNT(emp_id) AS total_count
FROM hr_db
GROUP BY 1;
-----------------------------------------------------------------------------------------------------
--7. List the average salary by job role.
SELECT
job_role,
ROUND(AVG(monthly_income),0) AS avg_salary
FROM hr_db
GROUP BY 1;
-----------------------------------------------------------------------------------------------------
--8. Retrieve the number of employees who have left the company.
SELECT
COUNT(*) AS attrition_count
FROM hr_db
WHERE attrition = 'yes';
----------------------------------------------------------------------------------------------------
-- 9. Find the department with the highest average JobSatisfaction.
SELECT
department,
ROUND(AVG(job_satisfaction),0) AS avg_job_satisfaction
FROM hr_db
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
--------------------------------------------------------------------------------------------------
--10. Get the top 5 highest-paid employees.
SELECT
emp_id,
monthly_income
FROM hr_db
ORDER BY 2 DESC
LIMIT 5;
---------------------------------------------------------------------------------------------------
--Advanced Query
--11. Rank employees within each department by MonthlyIncome (highest first).
SELECT emp_id,department,monthly_income
FROM
(SELECT
emp_id,
department,
monthly_income,
RANK() OVER(PARTITION BY department ORDER BY monthly_income DESC) AS rn
FROM hr_db
GROUP BY 1,2,3)
WHERE rn = 1;
----------------------------------------------------------------------------------------------------
--12. Get average MonthlyIncome and JobSatisfaction grouped by MaritalStatus and Gender.
SELECT
gender,
marital_status,
ROUND(AVG(monthly_income),0) AS avg_income,
ROUND(AVG(job_satisfaction),0) AS avg_job_satisfaction
FROM hr_db
GROUP BY 1,2;
---------------------------------------------------------------------------------------------------
--13. Find the relationship between Attrition and YearsAtCompany.
SELECT
attrition,
ROUND(AVG(years_at_company),2) AS avg_yat
FROM hr_db
GROUP BY 1;
----------------------------------------------------------------------------------------------------
--14. Show the percentage of employees who left vs. stayed, broken down by department.
SELECT
department,
attrition,
ROUND(COUNT(*) * 100/SUM(COUNT(*)) OVER(PARTITION BY department),2) AS
attrition_percentage
FROM hr_db
GROUP BY 1,2;
--15. Identify job roles with higher attrition rates than the company average.
WITH overall_avg AS(
SELECT AVG(CASE WHEN attrition = 'yes' THEN 1 ELSE 0 END) AS avg_attrition
FROM hr_db
),
roll_attrition AS(
SELECT
job_role,
AVG(CASE WHEN attrition = 'yes' THEN 1 ELSE 0 END) AS attrition_rate
FROM hr_db
GROUP BY job_role
SELECT r.job_role, r.attrition_rate
FROM roll_attrition r, overall_avg o
WHERE r.attrition_rate > o.avg_attrition;
---------------------------------------------------------------------------------------------------
--Analytical Query
--16. Which combination of department and education level has the highest attrition?
SELECT
department,
education,
COUNT(*) FILTER(WHERE attrition = 'yes') AS attrition_count
FROM hr_db
GROUP BY 1,2
ORDER BY 3 DESC
LIMIT 1;
------------------------------------------------------------------------------------------------------
--17. Does salary impact attrition? Compare average MonthlyIncome between those who left
and stayed.
SELECT
attrition,
ROUND(AVG(monthly_income),2) AS avg_income
FROM hr_db
GROUP BY 1;
------------------------------------------------------------------------------------------------------
--18. Which age group has the highest attrition rate?
SELECT
age_group,
ROUND(AVG(CASE WHEN attrition = 'yes' THEN 1 ELSE 0 END) * 100,2) AS attrition_rate
FROM hr_db
GROUP BY 1
ORDER BY 2 DESC;
------------------------------------------------------------------------------------------------------
--19. Find employees with below-average performance and above-average income.
WITH stats AS (
SELECT
AVG(performance_rating) AS avg_performance,
AVG(monthly_income) AS avg_income
FROM hr_db
SELECT
emp_id,
performance_rating,
monthly_income
FROM hr_db,stats
WHERE performance_rating < stats.avg_performance
AND
monthly_income > stats.avg_income;
----------------------------------------------------------------------------------------------------
-- 20. Which job roles have the most dissatisfied employees (JobSatisfaction < 3)?
SELECT
job_role,
job_satisfaction,
COUNT(*) AS dissatisfied_emp
FROM hr_db
WHERE job_satisfaction < 3
GROUP BY 1,2
ORDER BY 3 DESC;