# Calculating distances between student homes and schools using QGIS?

I am using QGIS 2.16.

I have two different point layers:

• points that represent students' homes
• points that represent schools.

In both I have the school ID. I need to calculate the distance between students' homes and their schools, using school ID.

Any idea about a plugin or function that would enable to do this?

• Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq. Nov 3, 2016 at 20:24

Use the QGIS DB Manager and use SQL Spatial to join the data on the SCHOOL_ID column from both tables:

First load your shapefiles into the map:

Then open the Database > DBManager tool

Access your layers from the "Virtual Layers" node:

Click the Query (QGIS Layers) tab.

Here you can write some SQL and use the ST_Distance function as if you were running a PostGIS or Spatialite database.

Here is the query I can use to join the students to the schools on SCHOOLNUM / SCHNUM - and I can also filter by grade level, etc. etc.

I'm also storing my geometry in WGS84/4326, so I'm using the ST_Transform tool to project the geometry to STATE PLANE COLORADO / 2877 which is in Feet, which I then divide by 5280 to get my distances in MILES:

``````select
sch.Schnum
, stu.lname
, ST_Distance(ST_Transform(sch.geometry, 2877), ST_Transform(stu.geometry, 2877)) / 5280 as DistToSchool_Mi

from "Schools_Current" as sch
join "Geo_Active_Student" as stu on sch.schnum = stu.schoolnum
where stu.grade =  '05'
and sch.elem = '1'
``````

And here is the result:

If you like how this works, you should get into PostGIS!!!

• Thank you for your answer. I am trying to apply SQL to my case but I have some problems to do it. I will get into PostGIS to understand it. Nov 3, 2016 at 20:34
• @Forsey feel free to contact me through the email in my profile - happy to help more with School GIS questions! Nov 3, 2016 at 20:38