# Calculating nearest point from a road, its name, distance and what road it intersects

I have a road and I need to calculate the nearest restaurant from it, its distance, its name and which road it is on(the restaurant). I have done all the things above except for the last one. Does anyone have any ideas?

`````` SELECT a.name, b.name, ST_Distance(a.geom, b.geom) AS distance
WHERE a.name='Roehampton'
ORDER BY distance ASC
LIMIT 1
``````
• Sorry, is the question how to find which road a restaurant is on? Having the where clause with Roahampton Lane is a bit confusing. Also, please don't write things like any1, this is a professional forum, and text speak is not really appropriate :-) Jul 31, 2019 at 7:15

There is a specific way to make use of index while searching for nearest neighbors, it's `<->`, and as maximilien said you should use the road table again. If the restaurant is not exactly on the road, try a nearest neighbors search again:

``````WITH restaurant AS (
SELECT a.name, b.name, ST_Distance(a.geom, b.geom) AS distance, b.geom
WHERE a.name='Roehampton Lane'
ORDER BY a.geom <-> b.geom ASC
LIMIT 1
)
ORDER BY res.geom <-> roads.geom ASC
LIMIT 1
``````
• Thank you alot. :)
– OSL
Jul 31, 2019 at 11:41

maybe you can try to add another time the road table and look for St_intersects with restaurant geom ...

something like this :

``````SELECT c.name, a.name, b.name, ST_Distance(a.geom, b.geom) AS distance