# sql-query-identifier

> A SQL query identifier

Latest version **3.2.0** (published 2026-07-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install sql-query-identifier
pnpm add sql-query-identifier
yarn add sql-query-identifier
bun add sql-query-identifier
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2026-07-09 |
| First published | 2016-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 10.13 |
| Dependencies | 0 |
| Unpacked size | 101.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 38 |
| Maintainers | maxcnunes, masterodin, masterodinbot, rathboma |

## Links

- npm: https://www.npmjs.com/package/sql-query-identifier
- Repository: https://github.com/coresql/sql-query-identifier
- Homepage: https://github.com/coresql/sql-query-identifier#readme
- Issues: https://github.com/coresql/sql-query-identifier/issues
- npm.io page: https://npm.io/package/sql-query-identifier

## Recent versions

- 3.2.0 (latest) — 2026-07-09
- 3.1.1 — 2026-06-10
- 3.1.0 — 2026-06-04
- 3.0.1 — 2026-06-03
- 3.0.0 — 2026-05-22
- 2.11.0 — 2026-05-05
- 2.10.0 — 2026-03-25
- 2.9.0 — 2025-11-26
- 2.8.0 — 2024-11-15
- 2.7.0 — 2024-02-21
- 2.6.0 — 2023-12-05
- 2.5.0 — 2023-02-06
- 2.4.4 — 2022-08-29
- 2.4.3 — 2022-08-26
- 2.4.2 — 2022-08-10
- … 20 more at https://npm.io/package/sql-query-identifier/versions

## README

sql-query-identifier
===================

[![Test](https://github.com/coresql/sql-query-identifier/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/coresql/sql-query-identifier/actions/workflows/test.yml)
[![npm version](https://badge.fury.io/js/sql-query-identifier.svg)](https://npmjs.com/package/sql-query-identifier)
[![view demo](https://img.shields.io/badge/view-demo-blue.svg)](https://sqlectron.github.io/sql-query-identifier/)

Identifies the types of each statement in a SQL query (also provide the start, end and the query text).

This uses AST and parser techniques to identify the SQL query type.
Although it will not validate the whole query as a fully implemented AST+parser would do.
Instead, it validates only the required tokens to identify the SQL query type. In summary it identifies the type by:

1. Scanning the tokens:
    * Comments
    * The initial keywords that identify the query type (e.g. INSERT, DELETE)
    * White spaces
    * String
    * Semicolon
1. Parsing the tokens to identify the query:
    * Comments are ignored since they could include words that will give false positive to identify the type
    * Keywords are expected to be at the beginning of the query statement
    * White spaces are only required to identify keywords with multiple words (e.g. CREATE TABLE)
    * String values are ignored since they also could include false positive words
    * Semicolon identifies the end of the statement

So the best approach using this module is only applying it after executing the query over the SQL client.
This way you have sure is a valid query before trying to identify the types.

## Current Available Types

For the show statements, please refer to the [MySQL Docs about SHOW Statements](https://dev.mysql.com/doc/refman/8.0/en/show.html).

* INSERT
* UPDATE
* DELETE
* SELECT
* TRUNCATE
* CREATE_DATABASE
* CREATE_SCHEMA
* CREATE_TABLE
* CREATE_VIEW
* CREATE_TRIGGER
* CREATE_FUNCTION
* CREATE_INDEX
* CREATE_PROCEDURE
* DROP_DATABASE
* DROP_SCHEMA
* DROP_TABLE
* DROP_VIEW
* DROP_TRIGGER
* DROP_FUNCTION
* DROP_INDEX
* DROP_PROCEDURE
* ALTER_DATABASE
* ALTER_SCHEMA
* ALTER_TABLE
* ALTER_VIEW
* ALTER_TRIGGER
* ALTER_FUNCTION
* ALTER_INDEX
* ALTER_PROCEDURE
* ANON_BLOCK (BigQuery and Oracle dialects only)
* SHOW_BINARY (MySQL and generic dialects only)
* SHOW_BINLOG (MySQL and generic dialects only)
* SHOW_CHARACTER (MySQL and generic dialects only)
* SHOW_COLLATION (MySQL and generic dialects only)
* SHOW_COLUMNS (MySQL and generic dialects only)
* SHOW_CREATE (MySQL and generic dialects only)
* SHOW_DATABASES (MySQL and generic dialects only)
* SHOW_ENGINE (MySQL and generic dialects only)
* SHOW_ENGINES (MySQL and generic dialects only)
* SHOW_ERRORS (MySQL and generic dialects only)
* SHOW_EVENTS (MySQL and generic dialects only)
* SHOW_FUNCTION (MySQL and generic dialects only)
* SHOW_GRANTS (MySQL and generic dialects only)
* SHOW_INDEX (MySQL and generic dialects only)
* SHOW_MASTER (MySQL and generic dialects only)
* SHOW_OPEN (MySQL and generic dialects only)
* SHOW_PLUGINS (MySQL and generic dialects only)
* SHOW_PRIVILEGES (MySQL and generic dialects only)
* SHOW_PROCEDURE (MySQL and generic dialects only)
* SHOW_PROCESSLIST (MySQL and generic dialects only)
* SHOW_PROFILE (MySQL and generic dialects only)
* SHOW_PROFILES (MySQL and generic dialects only)
* SHOW_RELAYLOG (MySQL and generic dialects only)
* SHOW_REPLICAS (MySQL and generic dialects only)
* SHOW_SLAVE (MySQL and generic dialects only)
* SHOW_REPLICA (MySQL and generic dialects only)
* SHOW_STATUS (MySQL and generic dialects only)
* SHOW_TABLE (MySQL and generic dialects only)
* SHOW_TABLES (MySQL and generic dialects only)
* SHOW_TRIGGERS (MySQL and generic dialects only)
* SHOW_VARIABLES (MySQL and generic dialects only)
* SHOW_WARNINGS (MySQL and generic dialects only)
* UNKNOWN (only available if strict mode is disabled)

## Execution types

Execution types allow to know what is the query behavior

* `LISTING:` is when the query list the data
* `MODIFICATION:` is when the query modificate the database somehow (structure or data)
* `INFORMATION:` is show some data information such as a profile data
* `ANON_BLOCK: ` is for an anonymous block query which may contain multiple statements of unknown type (BigQuery and Oracle dialects only)
* `UNKNOWN`: (only available if strict mode is disabled)

## Installation

Install via npm:

```bash
$ npm install sql-query-identifier
```

## Usage

```js
import { identify } from 'sql-query-identifier';

const statements = identify(`
  INSERT INTO Persons (PersonID, Name) VALUES (1, 'Jack');
  SELECT * FROM Persons;
`);

console.log(statements);
[
  {
    start: 9,
    end: 64,
    text: 'INSERT INTO Persons (PersonID, Name) VALUES (1, \'Jack\');',
    type: 'INSERT',
    executionType: 'MODIFICATION',
    parameters: []
  },
  {
    start: 74,
    end: 95,
    text: 'SELECT * FROM Persons;',
    type: 'SELECT',
    executionType: 'LISTING',
    parameters: []
  }
]
```

## API

`identify` arguments:

1. `input (string)`: the whole SQL script text to be processed
1. `options (object)`: allow to set different configurations
    1. `strict (bool)`: allow disable strict mode which will ignore unknown types *(default=true)*
    2. `dialect (string)`: Specify your database dialect, values: `generic`, `mysql`, `oracle`, `psql`, `sqlite`, `mssql`, `bigquery` and `dynamodb`. *(default=generic)*

### DynamoDB (PartiQL)

DynamoDB exposes a [PartiQL](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.html)-compatible
query language that is a strict subset of SQL. When `dialect: 'dynamodb'` is used, only DML statements
are accepted (`SELECT`, `INSERT`, `UPDATE`, `DELETE`); DDL (`CREATE` / `DROP` / `ALTER` / `TRUNCATE`),
`SHOW`, and transaction-control statements (`BEGIN` / `COMMIT` / `ROLLBACK`) are not part of the
DynamoDB PartiQL grammar and will throw in strict mode (or fall back to `UNKNOWN` in non-strict mode).
Positional `?` placeholders are the supported parameter style. Table and index identifiers are
double-quoted; string and attribute literals are single-quoted, per the DynamoDB PartiQL spec.

```js
import { identify } from 'sql-query-identifier';

identify(`SELECT * FROM "Orders" WHERE OrderID = ?`, { dialect: 'dynamodb' });
// [{ type: 'SELECT', executionType: 'LISTING', parameters: ['?'], ... }]
```

## Contributing

It is required to use [editorconfig](https://editorconfig.org/) and please write and run specs before pushing any changes:

```js
npm test
```

## License

Copyright (c) 2016-2021 The SQLECTRON Team.
This software is licensed under the [MIT License](https://github.com/sqlectron/sql-query-identifier/blob/master/LICENSE).

---
_Source: https://npm.io/package/sql-query-identifier · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
