๐ฏ ๐๐๐ ๐๐๐ซ๐ข๐๐ฌ | ๐๐ฉ. ๐ โ ๐๐๐๐๐๐: ๐ ๐ข๐ฅ๐ญ๐๐ซ๐ข๐ง๐ ๐๐ซ๐จ๐ฎ๐ฉ๐ฌ, ๐๐จ๐ญ ๐๐จ๐ฐ๐ฌ
๐๐ก๐ฒ ๐๐๐๐๐ ๐๐๐งโ๐ญ ๐๐๐ญ๐๐ก ๐๐ฏ๐๐ซ๐ฌ๐ฉ๐๐ง๐๐ข๐ง๐ ๐๐ซ๐จ๐ฃ๐๐๐ญ๐ฌ
๐ง ๐๐จ๐จ๐ค:
Imagine youโre a Data Analyst at LinkedIn.
Youโre reviewing project finances and need to ๐ข๐๐๐ง๐ญ๐ข๐๐ฒ ๐ฉ๐ซ๐จ๐ฃ๐๐๐ญ๐ฌ ๐ญ๐ก๐๐ญ ๐ฐ๐๐ง๐ญ ๐จ๐ฏ๐๐ซ ๐๐ฎ๐๐ ๐๐ญ.
Seems simple? You just need to compare:
Total employee cost per project vs. Project budget
But hereโs the twist โ employees work on different projects for different durations, and you need to ๐ฉ๐ซ๐จ๐ซ๐๐ญ๐ their annual salaries before summing them up.
And once youโve calculated total costs per project, you need to ๐๐ข๐ฅ๐ญ๐๐ซ ๐จ๐ง๐ฅ๐ฒ ๐ญ๐ก๐จ๐ฌ๐ ๐ฉ๐ซ๐จ๐ฃ๐๐๐ญ๐ฌ ๐ฐ๐ก๐๐ซ๐ ๐๐จ๐ฌ๐ญ๐ฌ ๐๐ฑ๐๐๐๐ ๐ญ๐ก๐ ๐๐ฎ๐๐ ๐๐ญ โ Thatโs when ๐๐๐๐๐๐ becomes your hero. ๐
๐ก ๐๐จ๐ง๐๐๐ฉ๐ญ:
The ๐๐๐๐๐๐ clause filters results ๐๐๐ญ๐๐ซ ๐๐ ๐ ๐ซ๐๐ ๐๐ญ๐ข๐จ๐ง, unlike ๐๐๐๐๐ , which filters rows ๐๐๐๐จ๐ซ๐ ๐๐ ๐ ๐ซ๐๐ ๐๐ญ๐ข๐จ๐ง.
๐งฉ ๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐ ๐๐ซ๐จ๐๐ฅ๐๐ฆ: ๐๐ข๐ฌ๐ค๐ฒ ๐๐ซ๐จ๐ฃ๐๐๐ญ๐ฌ โ https://lnkd.in/e92_5JKx
๐๐๐ฌ๐ค: Find projects where total prorated salary cost of assigned employees exceeds the projectโs budget.
๐๐จ๐ ๐ข๐ ๐๐ซ๐๐๐ค๐๐จ๐ฐ๐ง:
1๏ธโฃ Calculate how long each project lasted (in days).
2๏ธโฃ Prorate each employeeโs annual salary for their duration on that project.
3๏ธโฃ Sum total prorated cost per project.
4๏ธโฃ Compare that total cost with the projectโs budget.
5๏ธโฃ Keep only projects where cost > budget โ use HAVING.
๐ป๐๐๐ ๐๐จ๐ฅ๐ฎ๐ญ๐ข๐จ๐ง:
Get Somyaguptaโs stories inย yourย inbox
Join Medium for free to get updates fromย thisย writer.
with project_emp as (Select p.title,
p.budget,
(p.end_date โ p.start_date)*(e.salary/365.0) as employee_salary_perproject,
e.salary
from
linkedin_projects p join linkedin_emp_projects ep
on p.id=ep.project_id join linkedin_employees e on
e.id=ep.emp_id)
select title,budget,ceil(sum(employee_salary_perproject)) as prorated_employee_expense from project_emp
group by title, budget having
budget(ceil(sum(employee_salary_perproject)) order by title;
โ
๐๐๐๐๐ ๐๐ โ Groups data by project.
โ
๐๐๐() โ Aggregates prorated salary per project.
โ
๐๐๐๐๐๐ โ Filters projects exceeding budget.
โ
๐๐๐๐() โ Rounds cost up to the nearest dollar for reporting accuracy.
๐ฌ ๐๐ฎ๐ฌ๐ข๐ง๐๐ฌ๐ฌ ๐๐ง๐ฌ๐ข๐ ๐ก๐ญ:
The HAVING clause helps financial analysts, PMOs, and operations teams quickly:
โก๏ธ Spot departments exceeding cost limits
โก๏ธ Identify stores or campaigns performing above or below thresholds
This is how SQL turns data into action.
๐งฉ ๐๐ซ๐ฒ ๐๐ข๐ฆ๐ข๐ฅ๐๐ซ ๐๐ซ๐จ๐๐ฅ๐๐ฆ๐ฌ:
1๏ธโฃ Marketing Campaign Success โ https://lnkd.in/eAVZcfQU
2๏ธโฃ Actor Rating Difference Analysis โ https://lnkd.in/eAGx8QsG
๐ ๐๐จ๐ฆ๐ข๐ง๐ ๐๐๐ฑ๐ญ:
๐๐๐ ๐๐๐ซ๐ข๐๐ฌ | ๐๐ฉ. ๐ โ ๐๐๐๐๐ ๐๐๐๐: ๐๐จ๐ง๐ง๐๐๐ญ๐ข๐ง๐ ๐๐๐๐ฅ๐๐ฌ ๐ญ๐จ ๐๐๐ฅ๐ฅ ๐ ๐๐ญ๐จ๐ซ๐ฒ
Have you ever used HAVING to catch an anomaly or threshold breach in real data?




