interview_preparation
interview_preparation
SELECT
emp_id,
department
FROM hr_db;
------------------------------------------------------------------------------------------------------
SELECT
department,
COUNT(emp_id) AS emp_count
FROM hr_db
GROUP BY 1;
-----------------------------------------------------------------------------------------------------
SELECT
emp_id,
age
FROM hr_db
-----------------------------------------------------------------------------------------------------
-- 4. Find the average MonthlyIncome of employees in the ‘Sales’ department.
SELECT
department,
ROUND(AVG(monthly_income),2) AS avg_income
FROM hr_db
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
------------------------------------------------------------------------------------------------------
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
----------------------------------------------------------------------------------------------------
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
SELECT emp_id,department,monthly_income
FROM
(SELECT
emp_id,
department,
monthly_income,
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;
---------------------------------------------------------------------------------------------------
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,
FROM hr_db
GROUP BY 1,2;
--15. Identify job roles with higher attrition rates than the company average.
FROM hr_db
),
roll_attrition AS(
SELECT
job_role,
FROM hr_db
GROUP BY job_role
---------------------------------------------------------------------------------------------------
--Analytical Query
--16. Which combination of department and education level has the highest attrition?
SELECT
department,
education,
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;
------------------------------------------------------------------------------------------------------
SELECT
age_group,
FROM hr_db
GROUP BY 1
ORDER BY 2 DESC;
------------------------------------------------------------------------------------------------------
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
AND
----------------------------------------------------------------------------------------------------
-- 20. Which job roles have the most dissatisfied employees (JobSatisfaction < 3)?
SELECT
job_role,
job_satisfaction,
COUNT(*) AS dissatisfied_emp
FROM hr_db
GROUP BY 1,2
ORDER BY 3 DESC;