# Write a SOQL query using DISTANCE and GEOLOCATION to find all Warehouse Accounts within 25 miles of a given latitude and longitude.

**Topic:** Development · **Difficulty:** Hard · **Asked at:** Salesforce, Uber, Deloitte · **Tags:** soql, scenario, geolocation, distance

Read it on the site: https://www.forcechallenges.com/interview-questions/write-a-soql-query-using-distance-and-geolocation-to-find-all-warehouse-accounts-within-25-miles-of-a-given

## Answer

Use `DISTANCE()` with `GEOLOCATION()` against a geolocation field (e.g. a custom `Location__c` on Account) in the WHERE clause and optionally ORDER BY.

```soql
SELECT Id, Name, DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi') dist
FROM Account
WHERE DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi') < 25
AND Type = 'Warehouse'
ORDER BY DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi')
```
Units are `'mi'` or `'km'`. The location must be a compound geolocation field; `DISTANCE` can't be used in `GROUP BY`.

---
Source: ForceChallenges · last updated 2026-10-19
