MahiA MahiA - 5 months ago 7
SQL Question

Query for current salary based on date

id name salary date
1 aaa 10000 10/2/2014
1 aaa 15000 06/04/2015
1 aaa 20000 16/07/2016
1 aaa 25000 10/04/2017


If an employee got salary hike every year,
how can I get an employee's current year salary?
That query should be working fine for every year.

If I execute it in 2013 it displays 2013 year salary, if execute in 2016 it displays 2016 year salary, similarly, for 2017 also it would be display 2017 salary.

Answer

Check this SQL Fiddle for MySQL

SELECT
  SUM(salary) as salary
FROM salary
WHERE YEAR(STR_TO_DATE(date, '%d/%m/%Y')) = YEAR(CURDATE())
GROUP BY id