title: Payroll application - Complex SQL query
body: <p>SQL FIDDLE HERE: <a href="http://sqlfiddle.com/#!2/6c7f7/5" rel="nofollow">http://sqlfiddle.com/#!2/6c7f7/5</a>**</p>
  
  <p>I am building a payroll database and until now everything was perfect. One vital concept that I did not address from the start was when an employee has a pay grade change, will that be reflected from the date the pay grade changed? Previously the query would just go by whatever pay grade the employee had next to his name, but that is inaccurate if his rate changes (for example) every day of the week?</p>
  
  <p>With the help of others, I have got as far as the SQL Fiddle above, however the result is not reflecting the correct answer. At present with the 'where' clause in that fiddle, it uses the incorrect pay rate from the historyemployeepay table. It should be selecting the 'null' ToDate row and using that as the base rate?? Can someone show me what is going wrong?</p>