Skip to content

πŸ“Š Analyze employee salary growth, promotions, and rank using complex SQL queries with CTEs, window functions, and performance optimization.

Notifications You must be signed in to change notification settings

its-ekanshi/SQL-Employee-Salary-Insights

Repository files navigation

πŸ“Š Complex SQL Reporting β€” Employee Salary Insights

Welcome to a hands-on SQL project where I analyzed an Employee Salary History dataset and built a real-world reporting system using advanced SQL techniques like CTEs, window functions, and query optimization.


πŸ“ Dataset Description

This project uses two tables:

πŸ§‘β€πŸ’Ό employees

Column Data Type Description
employee_id INT Unique identifier
name VARCHAR Employee name
join_date DATE Date of joining
department VARCHAR Department of the employee

πŸ’° salary_history

Column Data Type Description
employee_id INT Foreign key to employees table
change_date DATE Date of salary change or promotion
salary DECIMAL Salary after the change
promotion VARCHAR 'Yes' or 'No' indicating promotion

❓ Business Questions Solved

  1. βœ… What is the latest salary of each employee?
  2. πŸ“ˆ How many promotions have they received?
  3. πŸš€ What is their maximum salary growth %?
  4. πŸ“‰ Did their salary ever decrease?
  5. πŸ•’ What's the average time between salary changes?
  6. πŸ† How do they rank based on salary growth ratio?

🧱 Step-by-Step SQL Approach

βœ”οΈ CTE-Based Query

Structured the logic into multiple CTEs (Common Table Expressions) to calculate:

  • Latest salary
  • Promotion count
  • Salary growth
  • Salary never decreased flag
  • Average months between changes
  • Rank by salary growth

πŸ“Ž View Full Query: CTE Version


βš™οΈ Optimized Version (Without CTEs)

Rewrote the same logic using a single query:

  • Removed intermediate CTEs
  • Used CASE, LEAD(), and RANK() smartly
  • Improved readability and performance

πŸ“Ž View Full Query: Optimized Version

πŸ“Έ Output Preview

Here’s a snapshot of the final report generated:

employee_id name no_of_promotions salary_growth never_decreased avg_months_between_changes RankByGrowth
1 Alice 1 27.27% Y 17.5 2
... ... ... ... ... ... ...

(This is a sample. Full output is visible in the notebook/script.)


πŸŽ“ Key Learnings

  • Practical application of Window Functions: RANK(), LEAD()
  • Usage of aggregations across change history
  • Comparison of query design patterns: CTEs vs Flat SQL
  • Real-life reporting KPIs using SQL only

πŸ”— Connect with Me


About

πŸ“Š Analyze employee salary growth, promotions, and rank using complex SQL queries with CTEs, window functions, and performance optimization.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published