# `gdal vector sql`

Added in version 3.11.

Apply SQL statement(s) to a dataset.

## Synopsis

    Usage: gdal vector sql [OPTIONS] <INPUT> <OUTPUT> <statement>|@<filename>

    Apply SQL statement(s) to a dataset.

    Positional arguments:
      -i, --input <INPUT>                                  Input vector datasets [required]
      -o, --output <OUTPUT>                                Output vector dataset [required]
      --sql <statement>|@<filename>                        SQL statement(s) [may be repeated] [required]

    Common Options:
      -h, --help                                           Display help message and exit
      --json-usage                                         Display usage as JSON document and exit
      --config <KEY>=<VALUE>                               Configuration option [may be repeated]
      --progress                                           Display progress bar

    Options:
      -f, --of, --format, --output-format <OUTPUT-FORMAT>  Output format ("GDALG" allowed)
      --co, --creation-option <KEY>=<VALUE>                Creation option [may be repeated]
      --lco, --layer-creation-option <KEY>=<VALUE>         Layer creation option [may be repeated]
      --overwrite                                          Whether overwriting existing output is allowed
      --update                                             Whether to open existing dataset in update mode
      --overwrite-layer                                    Whether overwriting existing layer is allowed
      --append                                             Whether appending to existing layer is allowed
      --output-layer <OUTPUT-LAYER>                        Output layer name(s) [may be repeated]
      --dialect <DIALECT>                                  SQL dialect (e.g. OGRSQL, SQLITE)

    Advanced Options:
      --if, --input-format <INPUT-FORMAT>                  Input formats [may be repeated]
      --oo, --open-option <KEY>=<VALUE>                    Open options [may be repeated]

## Description

gdal vector sql returns one or several layers evaluated from SQL statements.

### Standard options

-f, --of, --format, --output-format <OUTPUT-FORMAT>

  Which output vector format to use. Allowed values may be given by `gdal --formats | grep vector | grep rw | sort`

--co <NAME>=<VALUE>

  Many formats have one or more optional dataset creation options that can be used to control particulars about the file created. For instance, the GeoPackage driver supports creation options to control the version.

  May be repeated.

  The dataset creation options available vary by format driver, and some simple formats have no creation options at all. A list of options supported for a format can be listed with the --formats command line option but the documentation for the format is the definitive source of information on driver creation options. See Vector drivers format specific documentation for legal creation options for each format.

  Note that dataset creation options are different from layer creation options.

-lco <NAME>=<VALUE>

  Layer creation option (format specific)

--overwrite

  Allow program to overwrite existing target file or dataset. Otherwise, by default, gdal errors out if the target file or dataset already exists.

--sql <sql_statement>|@<filename>

  SQL statement to execute that returns a table/layer (typically a SELECT statement).

  Can be repeated to generated multiple output layers (repeating --sql <value> for each output layer)

--dialect <DIALECT>

  SQL dialect.

  By default the native SQL of an RDBMS is used when using `gdal vector sql`. If using `sql` as a step of `gdal vector pipeline`, this is only true if the step preceding `sql` is `read`, otherwise the OGRSQL dialect is used.

  If a datasource does not support SQL natively, the default is to use the `OGRSQL` dialect, which can also be specified with any data source.

  The SQL SQLite dialect dialect can be chosen with the `SQLITE` and `INDIRECT_SQLITE` dialect values, and this can be used with any data source. Overriding the default dialect may be beneficial because the capabilities of the SQL dialects vary. What SQL dialects a driver supports can be checked with "gdal vector info".

  Supported dialects can be checked with `gdal vector info`. For example:

      $ gdal vector info --format "PostgreSQL"
      Supported SQL dialects: NATIVE OGRSQL SQLITE

      $ gdal vector info --format "ESRI Shapefile"
      Supported SQL dialects: OGRSQL SQLITE

--output-layer <OUTPUT-LAYER>

  Output SQL layer name(s). If not specified, a generic layer name such as "SELECT" may be generated.

  Must be specified as many times as there are SQL statements, either as several --output-layer arguments, or a single one with the layer names combined with comma.

### Advanced options

--oo <NAME>=<VALUE>

  Dataset open option (format specific).

  May be repeated.

--if <format>

  Format/driver name to be attempted to open the input file(s). It is generally not necessary to specify it, but it can be used to skip automatic driver detection, when it fails to select the appropriate driver. This option can be repeated several times to specify several candidate drivers. Note that it does not force those drivers to open the dataset. In particular, some drivers have requirements on file extensions.

  May be repeated.

## GDALG output (on-the-fly / streamed dataset)

This program supports serializing the command line as a JSON file using the `GDALG` output format. The resulting file can then be opened as a vector dataset using the GDALG: GDAL Streamed Algorithm driver, and apply the specified pipeline in a on-the-fly / streamed way.

## Examples

### Example 1: Generate a GeoPackage file with a layer sorted by descending population

    $ gdal vector sql in.gpkg out.gpkg --output-layer country_sorted_by_pop --sql="SELECT * FROM country ORDER BY pop DESC"

### Example 2: Generate a GeoPackage file with 2 SQL result layers

    $ gdal vector sql in.gpkg out.gpkg --output-layer=beginning,end --sql="SELECT * FROM my_layer LIMIT 100" --sql="SELECT * FROM my_layer OFFSET 100000 LIMIT 100"