Is there any way I can import a shapefile into QGIS, then run spatial SQL queries against it?

I can obviously use ogr2ogr -sql to do this, but I have to import the output into QGIS each time to look at it, which is a pain.

It would be great if I could just import the shapefile into QGIS, then run SQL against it and see the results instantly.

1 Answer 1


You can use a virtual layer. Load your Shapefile in QGIS, then go to Layer / Add Layer / Add-Edit Virtual Layer. Type the SQL you want.

For instance, if your shapefile is named myshp and has attribute test you can query as:

select * from myshp a
where a.test = 1234;

Or you can make a spatial query using the geometry field:

select * from myshp 
where st_intersects(geometry, make_point(1,2));
  • amazing, thanks for this!
    – umbe1987
    Commented May 26, 2020 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.