MySQL Great Circle Distance Haversine formula
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The Great Circle Distance, often associated with the Haversine formula, is a critical concept in geospatial data analysis. This mathematical formula calculates the shortest distance between two points on the surface of a sphere, which is essential in fields like geography, aviation, and telecommunications. In this article, we'll explore how the MySQL database engine can be used to calculate the Great Circle Distance using the Haversine formula, including examples, subtopics, and a summary table.
Theoretical Background
Great Circle and Haversine Formula
A Great Circle is the largest circle that can be drawn on a sphere, resulting from the intersection of the sphere with a plane passing through its center. On Earth, the shortest path between two points on its surface lies along the Great Circle connecting them.
The Haversine formula is an equation that provides the great-circle distance between two points on a sphere given their longitudes and latitudes. The formula assumes a perfectly spherical shape for Earth, which is approximately valid for many practical applications.
The Haversine formula is expressed as:
Where: • is the distance between the two points (along the surface of the sphere), • is the radius of the sphere (mean radius of Earth = 6,371 km), • , are the latitudes of point 1 and point 2 (in radians), • , are the longitudes of point 1 and point 2 (in radians), • is the haversine function.
Implementing the Haversine Formula in MySQL
To calculate the distance, MySQL can execute SQL queries that implement this formula. Below is an example of how this can be done:
• places
is a table with columns lat
and lon
representing the latitude and longitude of points,
• :source_id
and :destination_id
should be replaced with the specific IDs for the source and destination points.
• Aviation: Pilots and airlines use Great Circle routes to plan flight paths that minimize fuel consumption and time.
• Telecommunications: Providers calculate the distance between towers to optimize network coverage and signal strength.
• Logistics and Shipping: Companies optimize delivery routes using these calculations to ensure cost efficiency.
• Spherical Assumption: Though the formula assumes Earth is a sphere, the actual shape can introduce slight errors in extensive navigation tasks.
• Crossing the 180th Meridian: Handling calculations where longitude measurements cross the International Date Line can require special consideration.
Related reading
- MySQL high CPU usage
- MySQL How to copy rows, but change a few fields?
- MySQL how to join tables on two fields
- MySQL How to modify stored procedures atomically?
- MySQL IF NOT NULL, then display 1, else display 0
- MySQL ignore errors when importing?
- MySQL IN condition limit
- MySQL Incorrect datetime value '0000-00-00 000000

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.