Skip to Main Content
  • Questions
  • Calculate compound interest using SQL

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Prerak.

Asked: September 12, 2016 - 4:16 pm UTC

Last updated: April 19, 2022 - 3:15 am UTC

Version: Oracle 11 G

Viewed 10K+ times! This question is

You Asked

Hi,

I am trying to create a logic. Please help me out.

Suppose i have brought a fund in Jan 2015 which is going to mature in year 2018.I am receiving the interest Semi-Annually.
I want to check the interest received From OCT 16 to SEP 17.
Basically i should receive interest in DEC 16 and JUNE 17.
Can you help me to develop the logic.

Thanks.

and Chris said...

So you want to compound the interest and calculate the payments in two periods?

If so, you'll need to use the compound interest formula. This is:

A = P (1 + r/n) ^ nt


Where:

A = the future value of the investment/loan, including interest
P = the principal investment amount (the initial deposit or loan amount)
r = the annual interest rate (decimal)
n = the number of times that interest is compounded per year
t = the number of years the money is invested or borrowed for


http://www.thecalculatorsite.com/articles/finance/compound-interest-formula.php

To find the fund value at a given point, you could just plug the numbers in to the formula. To get the payment, subtract off the value at the previous payment.

For example, if the fund has a starting value of 100 and a 5% return, the following calculates the value of the forth payment (in Dec-16):

begin
  dbms_output.put_line(
    round(( 100 * power ( ( 1 + (0.05/2) ), 4) ), 2) -
    round(( 100 * power ( ( 1 + (0.05/2) ), 3) ), 2)
  );
end;
/

2.69


Or you could write some SQL to calculate the value for all periods. This needs a running product. There isn't an in-built function to do this. So you can cheat by exploiting that a product ~ the exponent of the sum of logarithms:

product ( 1 .. n ) ~ exp ( sum ( ln ( 1 .. n ) ) )


Giving SQL like (again starting 100, 5% annual return):

with rws as (
  select add_months(date'2015-01-01', ((rownum*6) - 1)) dt,
         100 price,
         1 + (0.05/2) return
  from dual connect by level <= 8
), interest as (
  select dt, 
         price * exp(sum(ln(return)) over (order by dt)) value
  from   rws
)
  select * from interest;

DT                    VALUE                                     
01-JUN-2015 00:00:00  102.499999999999999999999999999999999999  
01-DEC-2015 00:00:00  105.062499999999999999999999999999999998  
01-JUN-2016 00:00:00  107.689062499999999999999999999999999997  
01-DEC-2016 00:00:00  110.381289062499999999999999999999999995  
01-JUN-2017 00:00:00  113.140821289062499999999999999999999996  
01-DEC-2017 00:00:00  115.969341821289062499999999999999999994  
01-JUN-2018 00:00:00  118.868575366821289062499999999999999991  
01-DEC-2018 00:00:00  121.840289750991821289062499999999999991 


To calculate the payment at a given point, use lag() to subtract off the previous value. And filter the results as needed:

with rws as (
  select add_months(date'2015-01-01', ((rownum*6) - 1)) dt,
         100 price,
         1 + (0.05/2) return
  from dual connect by level <= 8
), interest as (
  select dt, 
         price * exp(sum(ln(return)) over (order by dt)) value 
  from   rws
)
  select * from (
    select dt, round(value - lag(value) over (order by dt), 2) payments
    from   interest
  )
  where  dt between date'2016-08-01' and date'2017-09-01';

DT                    PAYMENTS  
01-DEC-2016 00:00:00  2.69      
01-JUN-2017 00:00:00  2.76  


Note: These calculation produce a lot of decimals! Make sure you check for rounding issues. You'll need to refer to the calculation the fund provider uses to determine how they handle this.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

suka

pikachu, December 21, 2020 - 4:10 am UTC

The World Health Organization recommends that coronavirus is currently very complex, so prevent it by wearing a mask and washing your hands regularly to limit disease. [url= https://bmi-calculator.io/]bmi calculator[/url]

PCB trace resistance calculator

Resistance, April 17, 2022 - 8:04 am UTC

It's possible to use SQL to design PCB trace resistance calculator?
https://www.resistancecalculator.com/2020/09/pcb-trace-resistance-calculator.html
Connor McDonald
April 19, 2022 - 3:15 am UTC

Well...based on that site, the formula is:

resistance * length / area + thermal_coeff * ( temp - 20 )

so as long as you have those values in (say) a database table, then you're good to go.

I'm not sure really what else you need?

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.