Compute the great-circle distance between two points on a spherical Earth using the spherical law of cosines.
ST_DISTANCE_SPHERE(lat1, lng1, lat2, lng2)
## Overview Returns the great-circle distance in meters between two points on a spherical Earth model, calculated using the spherical law of cosines. The formula is numerically simpler than Haversine and produces nearly identical results for most real-world distances. It is intended as an interchangeable alternative where a specific formula is required by tooling or for cross-engine compatibility. Use ST_DISTANCE_SPHERE whenever you want a fast, dependency-free distance between latitude/longitude pairs and you are not worried about the very small precision loss that the law of cosines exhibits at extremely short distances. ## When to use ST_DISTANCE_SPHERE vs. alternatives - **ST_DISTANCE_SPHERE** (this function): spherical Earth, law-of-cosines formulation. Accurate to roughly 0.3 percent across most of the globe. Drop-in alternative to Haversine. - **ST_DISTANCE_HAVERSINE**: spherical Earth, Haversine formulation. Preferred default because it is numerically stable for very short distances (under about 1 meter). - **ST_DISTANCE_VINCENTY**: ellipsoidal (WGS 84) distance. Survey-grade accuracy; slower and can fail to converge at near-antipodal points. - **ST_DISTANCE**: planar distance in CRS units; use only on projected data. ## Behavior - Returns a DOUBLE in meters. - Uses a spherical Earth with mean radius 6,371,008.8 meters, matching the other spherical distance functions. - Arguments are ordered (lat, lng), not (lng, lat), mirroring ST_DISTANCE_HAVERSINE and the other great-circle functions. - Symmetric: swapping the two points returns the same value. - Returns 0 for identical points. - Returns NULL if any argument is NULL. - Handles the antimeridian correctly; longitudes do not need to be pre-normalized. - Inputs outside valid latitude/longitude ranges are not rejected; they produce numerically defined but geographically meaningless results. ## Accuracy and performance - Accuracy: approximately 0.3 percent for distances up to a few thousand kilometers, increasing slightly on trans-global paths. - The law of cosines can lose precision on sub-meter distances due to floating-point cancellation in arccos; prefer ST_DISTANCE_HAVERSINE when working at very short ranges. - Performance is comparable to Haversine; both are constant-cost per call. - Combine with H3 cell prefiltering (see H3_LATLNG_TO_CELL) to avoid cross-product scans on large tables. ## Compatibility - Matches the spherical-law-of-cosines distance convention used by most SQL geospatial engines that expose a dedicated sphere function. - Inputs are always WGS 84 decimal degrees. No SRID argument is required or accepted.
| Name | Type | Description |
|---|---|---|
lat1 | Specifies the WGS 84 latitude of the first point in decimal degrees, in the range -90 to 90. | |
lng1 | Specifies the WGS 84 longitude of the first point in decimal degrees, in the range -180 to 180. | |
lat2 | Specifies the WGS 84 latitude of the second point in decimal degrees, in the range -90 to 90. | |
lng2 | Specifies the WGS 84 longitude of the second point in decimal degrees, in the range -180 to 180. |
-- Great-circle distance in meters.
-- Expected result: approximately 1,181,000 meters.
SELECT ST_DISTANCE_SPHERE(52.5200, 13.4050, 41.9028, 12.4964) AS distance_m;
-- Divide by 1000 for kilometers.
SELECT ST_DISTANCE_SPHERE(51.5074, -0.1278, 48.8566, 2.3522) / 1000.0 AS distance_km;
-- Keep stores within 10 km of a reference location.
SELECT store_id, store_name
FROM retail.catalog.stores
WHERE ST_DISTANCE_SPHERE(latitude, longitude, 51.5074, -0.1278) <= 10000;
-- Haversine and spherical-law-of-cosines agree to within floating-point noise for most inputs.
SELECT
ST_DISTANCE_HAVERSINE(51.5074, -0.1278, 40.7128, -74.0060) AS haversine_m,
ST_DISTANCE_SPHERE(51.5074, -0.1278, 40.7128, -74.0060) AS sphere_m,
ST_DISTANCE_SPHERE(51.5074, -0.1278, 40.7128, -74.0060) -
ST_DISTANCE_HAVERSINE(51.5074, -0.1278, 40.7128, -74.0060) AS delta_m;
-- Spot the sub-percent differences between spherical and ellipsoidal models.
SELECT
ST_DISTANCE_SPHERE(51.5074, -0.1278, 48.8566, 2.3522) AS sphere_m,
ST_DISTANCE_HAVERSINE(51.5074, -0.1278, 48.8566, 2.3522) AS haversine_m,
ST_DISTANCE_VINCENTY(51.5074, -0.1278, 48.8566, 2.3522) AS vincenty_m;
-- Return the 5 closest stations to a given point.
SELECT station_id,
ST_DISTANCE_SPHERE(lat, lng, 35.6762, 139.6503) AS distance_m
FROM transit.catalog.stations
ORDER BY distance_m
LIMIT 5;