Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using a query I found on stackoverflow for finding locations near a given latitude/longitude in a database. Below is the query. Entirely out of curiosity, can someone explain what exactly it's doing (the math bits, not the sql)? and is the lat/long to start from.


SELECT id, ( 3959 * 
acos( cos( radians(44.96577) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-93.268433) ) + sin( radians(44.907411) ) * sin( radians( latitude ) ) ) ) 
AS distance 
HAVING distance < 10 
ORDER BY distance ASC, id ASC LIMIT 10
share|cite|improve this question

This formula uses the Spherical law of cosines to calculate the great circle distance between two points, given their latitudes and logitudes.

In particular, it calculates the distance between fixed point (lat $44.\cdots$, long $-93.\cdots$) with the known points in the database.

The 3959 is (an approximation of) the radius of the earth in miles.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.