Skip to content

Instantly share code, notes, and snippets.

@RaczeQ
Last active January 19, 2026 10:29
Show Gist options
  • Select an option

  • Save RaczeQ/32cfbe8594c185d0a78e2f1a9d73b524 to your computer and use it in GitHub Desktop.

Select an option

Save RaczeQ/32cfbe8594c185d0a78e2f1a9d73b524 to your computer and use it in GitHub Desktop.
Which three places in the UK have the highest density of pubs?
-- Select all pubs in the Great Britain and calculate how many pubs are in a 5 km radius around each pub.
WITH pubs AS (
-- irish_pub....................................eat_and_drink > bar > irish_pub
-- pub..........................................eat_and_drink > bar > pub
SELECT
id,
JSON_EXTRACT_PATH_TEXT(names, 'primary') as name,
JSON_EXTRACT_PATH_TEXT(categories, 'primary') as category,
confidence,
JSON_EXTRACT_PATH_TEXT(addresses, '[0]') as address,
TO_GEOGRAPHY(geometry) geography
FROM
OVERTURE_MAPS__PLACES.CARTO.PLACE places
WHERE
JSON_EXTRACT_PATH_TEXT(categories, 'primary') IN ('irish_pub', 'pub') -- filter category
AND confidence >= 0.75 -- filter confidence
AND JSON_EXTRACT_PATH_TEXT(addresses, '[0].country') = 'GB' -- filter from Great Britain
AND operating_status = 'open' -- filter open locations
),
pubs_in_vicinity AS (
SELECT origin.id, COUNT(neighbour.id) neighbours
FROM pubs origin
LEFT JOIN pubs neighbour
ON ST_DWITHIN(origin.geography, neighbour.geography, 5000) -- Join in 5 KM radius
WHERE origin.id != neighbour.id
GROUP BY 1
)
SELECT
* EXCLUDE (geography)
FROM pubs
JOIN pubs_in_vicinity
USING (id)
ORDER BY pubs_in_vicinity.neighbours DESC
LIMIT 5;
-- NAME NEIGHBOURS ADDRESS,
-- The Wilmington 1600 69 Rosebery Avenue, London, EC1R 4PT
-- Pakenham Arms 1599 1 Pakenham Street, London, WC1X 0LA
-- The Easton 1599 22 Easton St, London, WC1X 0DS
-- Easton Clerkenwell 1598 22 Easton St, London, WC1X 0DS
-- Exmouth Arms 1597 23 Exmouth Market, London, EC1R 4QL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment