TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2671 1.9K
๐Ÿš€ SQL Project Series #35

HR & Employee Analytics ๐Ÿ‘ฅ

Analyze employees, departments, salaries, attendance, promotions, and attrition using SQL to understand workforce trends and improve HR decision-making.

๐ŸŽฏ Business Objectives

โœ… Analyze employee headcount

โœ… Track employee attrition

โœ… Analyze salary distribution

โœ… Measure department performance

โœ… Identify high-performing employees

โœ… Analyze promotions and tenure

โœ… Track attendance

โœ… Build HR analytics dashboards

๐Ÿ“‚ Step 1: Create Database

CREATE DATABASE hr_analytics_db;
USE hr_analytics_db;


๐Ÿ“‚ Step 2: Create Departments Table

CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100),
location VARCHAR(50)
);


๐Ÿ“‚ Step 3: Create Employees Table

CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
department_id INT,
job_title VARCHAR(100),
hire_date DATE,
salary DECIMAL(12,2),
employment_status VARCHAR(20),
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);


๐Ÿ“‚ Step 4: Create Attendance Table

CREATE TABLE attendance (
attendance_id INT PRIMARY KEY,
employee_id INT,
attendance_date DATE,
attendance_status VARCHAR(20),
FOREIGN KEY (employee_id) REFERENCES employees(employee_id)
);


๐Ÿ“‚ Step 5: Create Promotions Table

CREATE TABLE promotions (
promotion_id INT PRIMARY KEY,
employee_id INT,
promotion_date DATE,
old_job_title VARCHAR(100),
new_job_title VARCHAR(100),
FOREIGN KEY (employee_id) REFERENCES employees(employee_id)
);


๐Ÿ“‚ Step 6: Insert Sample Departments

INSERT INTO departments VALUES
(101,'Data Analytics','Mumbai'),
(102,'Finance','Delhi'),
(103,'Human Resources','Bangalore'),
(104,'Technology','Pune'),
(105,'Operations','Hyderabad');


๐Ÿ“‚ Step 7: Insert Sample Employees

INSERT INTO employees VALUES
(1,'Rahul Sharma',101,'Data Analyst','2022-01-10',850000,'Active'),
(2,'Priya Verma',102,'Financial Analyst','2021-05-15',920000,'Active'),
(3,'Amit Patel',104,'Software Engineer','2020-08-20',1200000,'Active'),
(4,'Sneha Joshi',103,'HR Specialist','2023-02-10',650000,'Active'),
(5,'Rohan Gupta',105,'Operations Analyst','2022-11-05',750000,'Resigned');


๐Ÿ“‚ Step 8: Insert Sample Attendance

INSERT INTO attendance VALUES
(1,1,'2025-01-02','Present'),
(2,1,'2025-01-03','Present'),
(3,2,'2025-01-02','Present'),
(4,2,'2025-01-03','Absent'),
(5,3,'2025-01-02','Present'),
(6,4,'2025-01-02','Present'),
(7,5,'2025-01-02','Absent');


๐Ÿ“‚ Step 9: Insert Sample Promotions

INSERT INTO promotions VALUES
(1,1,'2024-06-01','Junior Data Analyst','Data Analyst'),
(2,3,'2024-01-15','Software Engineer','Senior Software Engineer');


๐Ÿง  SQL Concepts You'll Practice

โœ” INNER JOIN

โœ” LEFT JOIN

โœ” GROUP BY

โœ” HAVING

โœ” CASE WHEN

โœ” Aggregate Functions

โœ” CTEs

โœ” Subqueries

โœ” Window Functions

โœ” RANK()

โœ” DENSE_RANK()

โœ” LAG()

โœ” Date Functions

โœ” Conditional Aggregation

๐Ÿ“Š Business KPIs You Can Build

๐Ÿ“ˆ Total Employees

๐Ÿ“ˆ Active Employees

๐Ÿ“ˆ Employee Attrition Rate

๐Ÿ“ˆ Monthly Hiring Rate

๐Ÿ“ˆ Monthly Attrition Rate

๐Ÿ“ˆ Employee Retention Rate

๐Ÿ“ˆ Average Salary

๐Ÿ“ˆ Median Salary

๐Ÿ“ˆ Salary by Department

๐Ÿ“ˆ Salary by Job Title

๐Ÿ“ˆ Gender Distribution

๐Ÿ“ˆ Employee Tenure

๐Ÿ“ˆ Average Tenure

๐Ÿ“ˆ Promotion Rate

๐Ÿ“ˆ Employees Promoted

๐Ÿ“ˆ Attendance Rate

๐Ÿ“ˆ Absenteeism Rate

๐Ÿ“ˆ Department Headcount

๐Ÿ“ˆ Department Attrition Rate

๐Ÿ“ˆ Highest Paid Employees

๐Ÿ“ˆ Salary Distribution

๐Ÿ“ˆ Hiring Trend

๐Ÿ“ˆ Employee Growth

๐Ÿ“ˆ Executive HR Dashboard

๐Ÿ’ก Example 1: Department-wise Employee Count
  • โค 4
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’