﻿{"id":14177,"date":"2025-06-18T10:00:00","date_gmt":"2025-06-18T08:00:00","guid":{"rendered":"https:\/\/www.sigterritoires.fr\/?p=14177"},"modified":"2025-06-11T08:07:59","modified_gmt":"2025-06-11T06:07:59","slug":"qgis-a-new-sql-query-option-in-the-layer-context-menu","status":"publish","type":"post","link":"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/","title":{"rendered":"QGIS: A New \u201cSQL Query\u201d Option in the Layer Context Menu"},"content":{"rendered":"\n<p>Starting with QGIS version 3.42, a subtle but powerful feature has been added to the <strong>context menu of vector layers<\/strong>: the <strong>\u201cSQL Query\u2026\u201d<\/strong> option.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contenu <\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/#A_Simple_but_Valuable_Addition\" >A Simple but Valuable Addition<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/#Simple_Example_Select_Parcels_Over_One_Hectare\" >Simple Example: Select Parcels Over One Hectare<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/#Spatial_Example_Parcels_Within_200_m_of_Rivers\" >Spatial Example: Parcels Within 200 m of Rivers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/#Practical_Tips\" >Practical Tips<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/#A_Great_Time-Saver_for_Advanced_Users\" >A Great Time-Saver for Advanced Users<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/#In_Summary\" >In Summary<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<p>This improvement provides a <strong>direct and user-friendly interface for running SQL queries<\/strong>, without needing to open the Processing Toolbox or the DB Manager.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Simple_but_Valuable_Addition\"><\/span> A Simple but Valuable Addition<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Right-clicking on a vector layer (whether from a database or a local file like a <a href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/geopackage-format-and-qgis-3\/\">GeoPackage<\/a>) now shows a new entry: <strong>SQL Query\u2026<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"307\" data-attachment-id=\"14178\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/requete_sql_menu_couches-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?fit=2560%2C1227&amp;ssl=1\" data-orig-size=\"2560,1227\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"requete_sql_menu_couches\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?fit=640%2C307&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1.jpg?resize=640%2C307&#038;ssl=1\" alt=\"\" class=\"wp-image-14178\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?resize=1024%2C491&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?resize=300%2C144&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?resize=768%2C368&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?resize=1536%2C736&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?resize=2048%2C982&amp;ssl=1 2048w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?w=1280&amp;ssl=1 1280w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/figure>\n\n\n\n<p>This command opens a lightweight SQL editor window where you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Filter features<\/strong> by attributes or geometry,<\/li>\n\n\n\n<li><strong>Generate new geometries<\/strong> (buffer, intersection, etc.),<\/li>\n\n\n\n<li><strong>Perform aggregations<\/strong> (sum, count, average, etc.).<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The functionality is powered by <strong>QGIS SQL<\/strong>, a flexible engine that works with both local datasets and remote spatial databases (e.g., <a href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/starting-with-postgres-postgis\/\">PostGIS<\/a>).<\/p>\n\n\n\n<p>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 <strong>\u201cLoad as new layer\u201d<\/strong> option, the query result can be added as a <strong>virtual layer<\/strong> in the <em>Layers<\/em> panel. This allows you to reuse the filtered dataset instantly, without duplicating data, and with the flexibility of dynamic access to your selection.<\/p>\n\n\n\n<p>This layer is <strong>temporary <\/strong>and not saved by default. If you wish to keep it or use it in another project, you can then <strong>save it manually<\/strong> in a format of your choice (GeoPackage, <a href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/shapefile-vs-geopackage\/\">Shapefile<\/a>, etc.) by right-clicking &gt; \u201cExport\u201d.<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Simple_Example_Select_Parcels_Over_One_Hectare\"><\/span>Simple Example: Select Parcels Over One Hectare<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&rsquo;s a basic query that selects only the parcels larger than 10,000 m\u00b2 (1 hectare):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT *\nFROM cadastre_rodrigues\nWHERE ST_Area(geometry) &gt; 10000\n<\/code><\/pre>\n\n\n\n<p>This returns a subset of the features, directly viewable and usable in your map project.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Spatial_Example_Parcels_Within_200_m_of_Rivers\"><\/span>Spatial Example: Parcels Within 200 m of Rivers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you have a <code>rivieres_rodrigues<\/code> layer loaded, you can find intersections with a 200 m buffer around the rivers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n  p.id,\n  ST_Intersection(p.geometry, ST_Union(ST_Buffer(r.geometry, 200))) AS geometry\nFROM cadastre_rodrigues AS p,\n     rivieres_rodrigues AS r\nWHERE ST_Intersects(p.geometry, ST_Buffer(r.geometry, 200))\n<\/code><\/pre>\n\n\n\n<p>This query returns the <strong>actual intersected geometries<\/strong>, not the entire parcel.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"340\" data-attachment-id=\"14183\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/qgis-a-new-sql-query-option-in-the-layer-context-menu\/requete_sql_intersection\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?fit=2560%2C1360&amp;ssl=1\" data-orig-size=\"2560,1360\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"requete_sql_intersection\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?fit=640%2C340&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection.jpg?resize=640%2C340&#038;ssl=1\" alt=\"\" class=\"wp-image-14183\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?resize=1024%2C544&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?resize=300%2C159&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?resize=768%2C408&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?resize=1536%2C816&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?resize=2048%2C1088&amp;ssl=1 2048w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?w=1280&amp;ssl=1 1280w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_intersection-scaled.jpg?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Practical_Tips\"><\/span>Practical Tips<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You must include a <code>geometry<\/code> column in your result for it to be displayed on the map.<\/li>\n\n\n\n<li>The SQL engine supports a wide range of spatial functions (<code>ST_Buffer<\/code>, <code>ST_Intersection<\/code>, <code>ST_Union<\/code>, <code>ST_Area<\/code>, etc.).<\/li>\n\n\n\n<li>Query results can be <strong>added to the project<\/strong>, <strong>saved as virtual layers<\/strong>, or <strong>exported to a new file<\/strong> (e.g., GeoPackage).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Great_Time-Saver_for_Advanced_Users\"><\/span> A Great Time-Saver for Advanced Users<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Previously, running SQL queries required opening:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>DB Manager<\/strong> (for database layers),<\/li>\n\n\n\n<li>The <strong>Virtual Layer<\/strong> interface (for local file-based layers),<\/li>\n\n\n\n<li>Or the <strong>Processing Toolbox<\/strong> for spatial queries.<\/li>\n<\/ul>\n\n\n\n<p>The new context menu command <strong>\u201cSQL Query\u2026\u201d<\/strong> streamlines this process and encourages experimentation and productivity.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"In_Summary\"><\/span> In Summary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This new option in the context menu is a <strong>welcome addition to QGIS<\/strong>. It bridges the gap between powerful SQL capabilities and everyday GIS workflows.<\/p>\n\n\n\n<p>By enabling direct access to SQL on any layer, <strong>QGIS empowers users to explore, transform, and filter data more efficiently<\/strong>, without needing to write full scripts or run multiple tools.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Starting with QGIS version 3.42, a subtle but powerful feature has been added to the context menu of vector layers: the \u201cSQL Query\u2026\u201d option. This improvement provides a direct and user-friendly interface for running SQL queries,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":14178,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"give_campaign_id":0,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sfsi_plus_gutenberg_text_before_share":"","sfsi_plus_gutenberg_show_text_before_share":"","sfsi_plus_gutenberg_icon_type":"","sfsi_plus_gutenberg_icon_alignemt":"","sfsi_plus_gutenburg_max_per_row":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1377],"tags":[3001,3298,1386,1353],"class_list":["post-14177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-qgis-en","tag-layers","tag-menu-en","tag-qgis-en","tag-sql-en"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2025\/06\/requete_sql_menu_couches-1-scaled.jpg?fit=2560%2C1227&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p6XU0A-3GF","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/14177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/comments?post=14177"}],"version-history":[{"count":0,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/14177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/media\/14178"}],"wp:attachment":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/media?parent=14177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/categories?post=14177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/tags?post=14177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}