SQL Coordinates Distance
<p>Table: <code>UserCoordinate</code>, PK: <code>UserName</code>, Values: <code>X</code>, <code>Y</code></p>

<p>Assuming <code>UserName</code> John is at the Coordinates (50,50)...</p>

<p>How to select the <code>UserCoordinate</code> rows that are within the distance of 20 that are closest to (50,50) with a limit of 30 results?</p>

<pre><code>SELECT * FROM UserCoordinate
WHERE(
        SQRT(SQUARE(ABS(50 - X)) + SQUARE(ABS(50 - Y))) &lt; 20
)
ORDER BY (SQRT(SQUARE(ABS(50 - X)) + SQUARE(ABS(50 - Y)))) ASC
LIMIT 30;
</code></pre>

<p>How to write the above correctly?</p>

<p>I'm using H2 database, and have full control over the program code (Java).</p>