title: SQL function table interpolation
body: <p>I have an SQL table of (x,y) values.</p>
  
  <pre><code>x     y
  0.0   0.0
  0.1   0.4
  0.5   1.0
  5.0   2.0
  6.0   4.0
  8.0   4.0
  10.0  5.0
  </code></pre>
  
  <p>The x column is indexed. I am using sqlite.</p>
  
  <p>My ultimate goal is to get a y(x) for any x value. I will use linear interpolation using table values. Similar as shown in the plot below.</p>
  
  <p><a href="https://i.stack.imgur.com/cxxug.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cxxug.png" alt="enter image description here"></a></p>
  
  <p>Is there a way to perform the linear interpolation directly using a select query?
  Otherwise getting the interval values where the x belongs, would be enough.</p>
  
  <p>Is there a query that will give me the last smaller and the first bigger pair of a given x, so that I can compute the interpolated y(x) value?</p>
  
  <p>For example if <code>x=2.0</code> to get:</p>
  
  <pre><code>0.5   1.0
  5.0   2.0
  </code></pre>
  
  <p>In case x is out of the table to get the two first/last values to perform an extrapolation.</p>
  
  <p>For example if <code>x=20.0</code> to get:</p>
  
  <pre><code>8.0   4.0
  10.0  5.0
  </code></pre>