SQL Latitude/Longitude Distance Calculator | Calculate Haversine Distance


SQL Latitude and Longitude Distance Calculator

Calculate the distance between two geographical points using their decimal degrees and see the Haversine formula in action.

Haversine Distance Calculator



Enter latitude in decimal degrees (e.g., 40.7128 for NYC)


Enter longitude in decimal degrees (e.g., -74.0060 for NYC)



Enter latitude in decimal degrees (e.g., 34.0522 for LA)


Enter longitude in decimal degrees (e.g., -118.2437 for LA)


What is a Distance Calculation Using Latitude and Longitude in SQL?

A distance calculation using latitude and longitude in SQL is a method to determine the geographical distance between two points on the Earth’s surface directly within a database query. This is a common requirement in applications like logistics, location-based services, and data analysis. Instead of pulling coordinates into an application to perform the calculation, it can be done efficiently at the database level.

This is typically achieved using the Haversine formula, which accounts for the planet’s spherical shape. Many modern SQL databases, such as SQL Server, PostgreSQL (with PostGIS), and MySQL have built-in functions that simplify this process, like ST_Distance or ST_Distance_Sphere. Our calculator demonstrates the core logic behind these powerful functions.

The Haversine Formula for Distance Calculation

The most common formula for this task is the Haversine formula. It’s a reliable way to calculate the great-circle distance between two points on a sphere given their longitudes and latitudes. The formula is as follows:

a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2(√a, √(1−a))
d = R ⋅ c

This approach is critical for accurate distance calculation using latitude and longitude in sql when dealing with anything more than very short distances. For more detailed guides, see our article on geospatial indexing strategies.

Haversine Formula Variables
Variable Meaning Unit Typical Range
φ1, φ2 Latitude of point 1 and point 2 Radians -π/2 to +π/2
λ1, λ2 Longitude of point 1 and point 2 Radians -π to +π
Δφ, Δλ Difference in latitude and longitude Radians
R Radius of Earth Kilometers or Miles ~6,371 km or ~3,959 miles
d The final calculated distance Kilometers or Miles 0 to ~20,000 km

Visualizing Coordinate System

Diagram of Latitude and Longitude Lat Lon P1 P2 Equator

A simplified diagram illustrating the latitude and longitude lines on a sphere.

Practical Examples

Example 1: New York City to Los Angeles

  • Input Point 1 (NYC): Latitude = 40.7128, Longitude = -74.0060
  • Input Point 2 (LA): Latitude = 34.0522, Longitude = -118.2437
  • Unit: Miles
  • Result: Approximately 2,445 miles

Example 2: London to Paris

  • Input Point 1 (London): Latitude = 51.5074, Longitude = -0.1278
  • Input Point 2 (Paris): Latitude = 48.8566, Longitude = 2.3522
  • Unit: Kilometers
  • Result: Approximately 344 kilometers

How to Use This SQL Distance Calculator

Using this calculator is straightforward:

  1. Enter Point 1 Coordinates: Input the latitude and longitude for your starting point in the first two fields. Use negative values for South latitudes and West longitudes.
  2. Enter Point 2 Coordinates: Do the same for your destination point.
  3. Select Unit: Choose whether you want the result in kilometers or miles from the dropdown menu.
  4. Calculate: Click the “Calculate Distance” button. The result will appear below, along with the intermediate values used in the Haversine formula, which is essential for understanding the distance calculation using latitude and longitude in sql.

Key Factors That Affect Distance Calculation

  • Earth’s Shape: The Earth is not a perfect sphere (it’s an oblate spheroid). The Haversine formula assumes a perfect sphere, which introduces a small error (around 0.3-0.5%). For a more accurate Vincenty vs. Haversine comparison, check our guide.
  • Earth Radius (R): The value used for the Earth’s radius affects the final distance. Our calculator uses an average radius.
  • Data Precision: In SQL, using floating-point data types (like `FLOAT` or `DOUBLE PRECISION`) is crucial for maintaining accuracy in latitude and longitude values.
  • Coordinate Format: Ensure your coordinates are in decimal degrees. If you have them in Degrees/Minutes/Seconds (DMS), they must be converted first.
  • SQL Function Choice: Native SQL functions like `ST_Distance` in PostGIS are often highly optimized and may use more advanced models than the basic Haversine formula, offering better performance and accuracy.
  • Database Indexing: For large datasets, querying distances can be slow. Using a spatial index is critical for performance.

Frequently Asked Questions (FAQ)

1. Why is the Haversine formula used for distance calculation?

The Haversine formula is popular because it’s a good compromise between simplicity and accuracy for calculating distances on a sphere. It avoids issues with calculations near the poles and is less computationally intensive than more complex methods like Vincenty’s formulae. It is a foundational concept for distance calculation using latitude and longitude in sql.

2. How can I get latitude and longitude data for addresses?

You typically use a geocoding service (like Google Maps API, Here, or Mapbox) to convert street addresses into latitude and longitude coordinates, which you can then store in your database.

3. Does MySQL have a built-in function for this?

Yes, since MySQL 5.7, you can use the ST_Distance_Sphere() function. It takes two POINT objects and returns the distance in meters. For example: `SELECT ST_Distance_Sphere(POINT(lon1, lat1), POINT(lon2, lat2));`

4. What about SQL Server?

SQL Server uses the `geography` data type. You can create two `geography` points and use the `STDistance()` method to find the distance in meters. For example: `DECLARE @p1 geography = geography::Point(lat1, lon1, 4326); DECLARE @p2 geography = geography::Point(lat2, lon2, 4326); SELECT @p1.STDistance(@p2);`

5. Is this calculation 100% accurate?

No. It’s an approximation. Factors like the Earth’s irregular shape (it bulges at the equator) and elevation differences are not accounted for in the Haversine formula. However, for most web applications, the accuracy is more than sufficient.

6. Why are my results in Radians in some formulas?

Trigonometric functions in most programming languages and SQL dialects (like `SIN`, `COS`) expect angles to be in radians, not degrees. Therefore, the first step is always to convert your latitude and longitude from degrees to radians.

7. How do I handle the unit conversion?

The calculation is based on the Earth’s radius (R). If you use R in kilometers, your result is in kilometers. If you use R in miles, the result is in miles. This calculator handles the conversion automatically based on your selection.

8. What does the `4326` mean in the SQL Server example?

This is the Spatial Reference System Identifier (SRID). `4326` is the standard SRID for WGS 84, the most common coordinate system used for GPS and web mapping.

© 2026 Your Website. All Rights Reserved. A tool for understanding distance calculation using latitude and longitude in sql.


Leave a Reply

Your email address will not be published. Required fields are marked *