QGIS: A New “SQL Query” Option in the Layer Context Menu

Starting with QGIS version 3.42, a subtle but powerful feature has been added to the context menu of vector layers: the “SQL Query…” option.

This improvement provides a direct and user-friendly interface for running SQL queries, without needing to open the Processing Toolbox or the DB Manager.


A Simple but Valuable Addition

Right-clicking on a vector layer (whether from a database or a local file like a GeoPackage) now shows a new entry: SQL Query…

This command opens a lightweight SQL editor window where you can:

  • Filter features by attributes or geometry,
  • Generate new geometries (buffer, intersection, etc.),
  • Perform aggregations (sum, count, average, etc.).

The functionality is powered by QGIS SQL, a flexible engine that works with both local datasets and remote spatial databases (e.g., PostGIS).

The result of the query is displayed in a dedicated window as a list of records matching the specified criteria. You can review the results directly, but more importantly, by using the “Load as new layer” option, the query result can be added as a virtual layer in the Layers panel. This allows you to reuse the filtered dataset instantly, without duplicating data, and with the flexibility of dynamic access to your selection.

This layer is temporary and not saved by default. If you wish to keep it or use it in another project, you can then save it manually in a format of your choice (GeoPackage, Shapefile, etc.) by right-clicking > “Export”.


Simple Example: Select Parcels Over One Hectare

Here’s a basic query that selects only the parcels larger than 10,000 m² (1 hectare):

SELECT *
FROM cadastre_rodrigues
WHERE ST_Area(geometry) > 10000

This returns a subset of the features, directly viewable and usable in your map project.


Spatial Example: Parcels Within 200 m of Rivers

If you have a rivieres_rodrigues layer loaded, you can find intersections with a 200 m buffer around the rivers:

SELECT 
  p.id,
  ST_Intersection(p.geometry, ST_Union(ST_Buffer(r.geometry, 200))) AS geometry
FROM cadastre_rodrigues AS p,
     rivieres_rodrigues AS r
WHERE ST_Intersects(p.geometry, ST_Buffer(r.geometry, 200))

This query returns the actual intersected geometries, not the entire parcel.


Practical Tips

  • You must include a geometry column in your result for it to be displayed on the map.
  • The SQL engine supports a wide range of spatial functions (ST_Buffer, ST_Intersection, ST_Union, ST_Area, etc.).
  • Query results can be added to the project, saved as virtual layers, or exported to a new file (e.g., GeoPackage).


A Great Time-Saver for Advanced Users

Previously, running SQL queries required opening:

  • The DB Manager (for database layers),
  • The Virtual Layer interface (for local file-based layers),
  • Or the Processing Toolbox for spatial queries.

The new context menu command “SQL Query…” streamlines this process and encourages experimentation and productivity.


In Summary

This new option in the context menu is a welcome addition to QGIS. It bridges the gap between powerful SQL capabilities and everyday GIS workflows.

By enabling direct access to SQL on any layer, QGIS empowers users to explore, transform, and filter data more efficiently, without needing to write full scripts or run multiple tools.


Si cet article vous a intéressé et que vous pensez qu'il pourrait bénéficier à d'autres personnes, n'hésitez pas à le partager sur vos réseaux sociaux en utilisant les boutons ci-dessous. Votre partage est apprécié !

Leave a Reply

Your email address will not be published. Required fields are marked *