---
title: "SQL Snippets: Where to Save Them and Libraries to Use"
description: "SQL snippets are saved pieces of SQL you insert with a short trigger word instead of retyping them."
canonical: "https://www.aifordatabase.com/blog/sql-snippets/"
author: "Dhrumil Barot"
author_bio: "Dhrumil is an enterprise analytics expert. Dhrumil built AI for Database so anyone on a team can get answers from live company data in plain English, without waiting on a data team."
published: 2026-11-19
updated: 2026-11-19
---

# SQL Snippets: Where to Save Them and Libraries to Use

**TL;DR:** SQL snippets are saved pieces of SQL you insert with a short trigger word instead of retyping them. SSMS stores them as `.snippet` XML files, VS Code as JSON `.code-snippets` files, DBeaver as Templates and DataGrip as Live Templates. If the snippet you keep reusing is really a business question ("revenue by month", "active users this week"), AI for Database lets you ask it in plain English, shows the SQL, and pins the answer to a dashboard that refreshes itself.

SQL snippets (also called SQL templates or live templates) are reusable fragments of SQL, such as a standard join, a date filter or a `TRY...CATCH` block, that your editor inserts when you type a prefix and press Tab. Every major SQL editor supports them, but each stores them in a different place and format: SSMS uses XML `.snippet` files registered in Tools > Code Snippets Manager, VS Code uses JSON with `${1:placeholder}` tab stops, DBeaver and DataGrip use template settings pages. Below are the exact paths, a sample snippet for each, and the snippet libraries worth importing.

> Try AI for Database free at aifordatabase.com (https://app.aifordatabase.com/signup, sample data included).

![How a snippet goes from a saved file to finished SQL in the editor](https://cdn.sanity.io/images/upsr56wu/production/30783180c5090c1c9302c6b23c4c5c7f285fe252-1344x326.png?w=1280)

FIG 01 — Same flow in SSMS, VS Code, DBeaver and DataGrip; only the storage format differs.

## What AI for Database does for the queries you keep saving

Most snippet collections end up as a pile of report queries: monthly revenue, signups by source, churned accounts. AI for Database handles that job differently for teams without a data team:

*   **Ask instead of paste.** Connect PostgreSQL, MySQL, SQL Server, BigQuery, Supabase and more, type "revenue by country last 30 days", and get a table or chart. Every answer shows its SQL, so it doubles as a snippet you can copy into your editor.
*   **Read-only.** Answers run read-only, so a reused query cannot change data.
*   **Dashboards that refresh themselves.** Pin the question once and it re-runs on a schedule, so nobody pastes the same snippet every Monday.
*   **Workflows.** "When daily signups drop below 50, send a Slack alert" replaces the snippet someone runs by hand to check.

It does not manage editor snippets for you. Keep using the editor tools below for DDL, admin scripts and code you write yourself.

## How do I add custom snippets in SSMS?

SSMS snippets are XML files with a `.snippet` extension that follow the Visual Studio Code Snippets Schema with the language set to SQL ([Microsoft Learn](https://learn.microsoft.com/en-us/ssms/scripting/add-transact-sql-snippets)).

1.  Save the file, for example `TopN.snippet`.
2.  Open SSMS, go to **Tools > Code Snippets Manager**.
3.  Choose **Add** to register a whole folder, or **Import** to add one file to My Code Snippets.
4.  In a query window press Ctrl+K, Ctrl+X to insert it.

```
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Top N rows</Title>
      <SnippetTypes><SnippetType>Expansion</SnippetType></SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal><ID>n</ID><Default>100</Default></Literal>
        <Literal><ID>table</ID><Default>dbo.Orders</Default></Literal>
      </Declarations>
      <Code Language="SQL"><![CDATA[SELECT TOP ($n$) * FROM $table$ ORDER BY 1 DESC;]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
```

To see where the built-in T-SQL snippets live on your machine, pick SQL in the Language box of Code Snippets Manager; the path appears under Location.

## Do VS Code and DBeaver support SQL snippets?

Yes, both do, in different formats.

**VS Code** (and the mssql extension) uses JSON snippets. Open **File > Preferences > Configure Snippets**, pick `sql` or create a global `.code-snippets` file. A project file in `.vscode/` is shared with everyone who opens the repo ([VS Code docs](https://code.visualstudio.com/docs/editing/userdefinedsnippets)). `${1:default}` is a tab stop and `$0` is where the cursor ends. Tab expansion needs `"editor.tabCompletion": "on"`.

```
{
  "Rows in the last N days": {
    "prefix": "lastn",
    "scope": "sql",
    "body": [
      "SELECT *",
      "FROM ${1:orders}",
      "WHERE ${2:created_at} >= CURRENT_DATE - INTERVAL '${3:7} days';$0"
    ],
    "description": "Filter a table to the last N days (PostgreSQL)"
  }
}
```

Azure Data Studio used the same format, but Microsoft retired it on February 28, 2026 and points users to VS Code ([Microsoft Learn](https://learn.microsoft.com/en-us/sql/azure-data-studio/code-snippets)). Copy your old `sql.json` snippets into VS Code as they are.

**DBeaver** calls them SQL Templates. Press Ctrl+Alt+Space to list them, or type the template name and press Tab. Add or edit them under **Preferences > SQL Editor > Templates** ([DBeaver docs](https://dbeaver.com/docs/dbeaver/SQL-Templates/)).

```
-- DBeaver template "dup": find duplicate values
SELECT ${column}, COUNT(*) AS n
FROM ${table}
GROUP BY ${column}
HAVING COUNT(*) > 1;
```

**DataGrip** uses Live Templates: Ctrl+J inserts, Tab expands, and **Settings > Editor > Live Templates** manages them. Built-ins include `sel`, `selw`, `selc`, `ins` and `upd` ([JetBrains docs](https://www.jetbrains.com/help/datagrip/using-live-templates.html)).

| Editor | Name for snippets | Format | Where to manage | Insert |
| --- | --- | --- | --- | --- |
| SSMS | Code snippets | `.snippet` XML | Tools > Code Snippets Manager | Ctrl+K, Ctrl+X |
| VS Code | Snippets | `.code-snippets` / `sql.json` | File > Preferences > Configure Snippets | prefix + Tab |
| DBeaver | SQL Templates | settings page | Preferences > SQL Editor > Templates | name + Tab, Ctrl+Alt+Space |
| DataGrip | Live Templates | settings page | Settings > Editor > Live Templates | abbreviation + Tab, Ctrl+J |
| Databricks SQL | Query snippets | `${1:value}` in UI | View > Query snippets | autocomplete, Ctrl+Space |
| Metabase | Snippets | `{{snippet: Name}}` | SQL editor sidebar | `{{snippet:` autocomplete |

![The same date filter stored in three editors' formats](https://cdn.sanity.io/images/upsr56wu/production/5663c9a9e7d4a4d11b45539bf574a9503b343644-1344x502.png?w=1280)

FIG 02 — Placeholder syntax is the main thing that changes when you move snippets between tools.

## Where can I find ready-made SQL snippet libraries?

Start with these, grouped by kind. All are free unless noted.

**Snippet collections**

*   [count/sql-snippets](https://github.com/count/sql-snippets): curated analytics snippets (cohorts, medians, outliers, time series) with folders for BigQuery, MSSQL, MySQL, Postgres and Snowflake. Best single library for analysts.
*   [Databricks query snippets](https://docs.databricks.com/gcp/sql/user/queries/query-snippets): docs with ready examples of snippets with insertion points, useful as a pattern even outside Databricks.
*   [Metabase snippets](https://www.metabase.com/docs/latest/questions/native-editor/snippets): shows how a team stores shared KPI definitions ("what counts as an active user") as snippets.

**Editor documentation**

*   [SSMS: Add Transact-SQL snippets](https://learn.microsoft.com/en-us/ssms/scripting/add-transact-sql-snippets): the official schema and a full TRY...CATCH example file.
*   [VS Code user-defined snippets](https://code.visualstudio.com/docs/editing/userdefinedsnippets): every placeholder, choice and variable the JSON format supports.
*   [DBeaver SQL templates](https://dbeaver.com/docs/dbeaver/SQL-Templates/): template variables and DBeaver-specific ones such as table and column pickers.
*   [DataGrip live templates](https://www.jetbrains.com/help/datagrip/using-live-templates.html): simple, parameterized and surround templates, plus postfix completion.

**Reference queries to turn into snippets**

*   [PostgreSQL date/time functions](https://www.postgresql.org/docs/current/functions-datetime.html): the source for every date filter snippet on Postgres.
*   [MySQL date and time functions](https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html): same for MySQL.
*   [SQL Server system catalog views](https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/catalog-views-transact-sql): the base for "find a column", "table sizes" and other admin snippets.

**Our free pages**

*   [SQL glossary entry](https://www.aifordatabase.com/glossary/sql/) and [scheduled query](https://www.aifordatabase.com/glossary/scheduled-query/): for when a snippet should run on its own.
*   [Dashboard and query templates](https://www.aifordatabase.com/templates/): report questions ready to run on sample data.

## What makes a SQL snippet worth saving?

A snippet is worth saving when you type the same structure at least weekly and only the names change. Good candidates:

1.  **Boilerplate with fixed syntax:** `TRY...CATCH`, `MERGE`, `CREATE TABLE` with your audit columns.
2.  **Easy-to-get-wrong filters:** half-open date ranges (`>= start AND < end`), timezone conversion.
3.  **Admin lookups:** find a column across tables, table sizes, blocking sessions.
4.  **Team definitions:** "active user" or "net revenue", shared in a project `.code-snippets` file or a Metabase snippet so everyone uses one definition.

Skip it when the query is a one-off report someone else will ask for again next month: that is a dashboard, not a snippet. For PostgreSQL work, see our [PostgreSQL page](https://www.aifordatabase.com/databases/postgresql/).

## Start here

1.  Pick your editor's row in the table above and create one snippet for the query you typed most this week.
2.  Import three analytics snippets from count/sql-snippets for your database.
3.  Put team definitions in a shared project snippet file.
4.  Move recurring report queries out of snippets and into a scheduled dashboard.

D

Written by [Dhrumil Barot](https://www.aifordatabase.com/about/#founder)

Dhrumil is an enterprise analytics expert. Dhrumil built AI for Database so anyone on a team can get answers from live company data in plain English, without waiting on a data team.

[LinkedIn](https://www.linkedin.com/in/bdhrumil)[X](https://x.com/fyidhrumil)

## Frequently Asked Questions

### What is a SQL snippet?

A SQL snippet is a saved, reusable fragment of SQL that an editor inserts when you type a short prefix, often with placeholders you fill in with Tab.

### How do I add custom snippets in SSMS?

Write an XML .snippet file with language SQL, then register it in Tools > Code Snippets Manager with Add (folder) or Import (file).

### Do VS Code and DBeaver support SQL snippets?

Yes. VS Code uses JSON snippet files (sql.json or .code-snippets); DBeaver uses SQL Templates under Preferences > SQL Editor > Templates.

### Can I move Azure Data Studio snippets to VS Code?

Yes. Azure Data Studio used the VS Code snippet format, so the same JSON works in VS Code, which Microsoft recommends after the February 28, 2026 retirement.

### Where can I find ready-made SQL snippet libraries?

count/sql-snippets on GitHub is the broadest free collection, with versions for BigQuery, MSSQL, MySQL, Postgres and Snowflake. > Stop pasting the same report query every week. Try AI for Database free at aifordatabase.com (https://app.aifordatabase.com/signup, sample data included).

Read next

*   [HOW TO FIND DUPLICATES IN SQL (AND DELETE THEM SAFELY) →](https://www.aifordatabase.com/blog/find-duplicates-sql-query/)
*   [HOW TO QUERY YOUR MYSQL DATABASE WITHOUT WRITING SQL IN 2026 →](https://www.aifordatabase.com/blog/mysql-ai-analytics-2026/)
*   [9 BEST FREE REPORTING TOOLS FOR SQL DATA (COMPARED) →](https://www.aifordatabase.com/blog/free-reporting-tools/)

Source: https://www.aifordatabase.com/blog/sql-snippets/
