GRASS logo

Note: This document is for an older version of GRASS GIS that will be discontinued soon. You should upgrade, and read the current manual page.

NAME - Selects data from attribute table.
Performs SQL query statement(s).


database, attribute table, SQL

SYNOPSIS --help [-cdvt] [sql=sql_query] [input=name] [table=name] [driver=name] [database=name] [separator=character] [vertical_separator=character] [null_value=string] [output=name] [--overwrite] [--help] [--verbose] [--quiet] [--ui]


Do not include column names in output
Describe query only (don't run it)
Vertical output (instead of horizontal)
Only test query, do not execute
Allow output files to overwrite existing files
Print usage summary
Verbose module output
Quiet module output
Force launching GUI dialog


SQL SELECT statement
Example: select * from towns where population > 10000
Name of file containing SQL select statement(s)
'-' for standard input
Name of table to query
Name of database driver
Options: dbf, odbc, ogr, pg, sqlite
Default: sqlite
Name of database
Default: $GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db
Field separator
Special characters: pipe, comma, space, tab, newline
Default: pipe
Vertical record separator (requires -v flag)
Special characters: pipe, comma, space, tab, newline
String representing NULL value
Name for output file (if omitted or "-" output to stdout)

Table of contents

DESCRIPTION prints result of selection from database based on SQL statement read from input file or from standard input to standard output. Each individual query has to be written on one single line and different queries have to be written on separate lines.


If parameters for database connection are already set with db.connect, they are taken as default values and do not need to be specified each time. Output will be displayed to standard output or can be directed to a file (option output).


Basic usage sql="select * from roads"
echo "select * from roads" | input=-
or input=file.sql
cat file.sql | input=-

Select all from table roads: -c driver=odbc database=mydb table=hospitals \
          input=file.sql output=result.csv

Select some string attribute, exclude others: sql="SELECT * FROM archsites WHERE str1 <> 'No Name'"

Select some string attribute with ZERO length: sql="SELECT * FROM archsites WHERE str1 IS NULL"

Select coordinates from PostGIS table: sql="SELECT x(geo),y(geo) FROM localizzazione"

Execute multiple SQL statements

cat file.sql
SELECT * FROM busstopsall WHERE cat = 1
SELECT cat FROM busstopsall WHERE cat > 4 AND cat < 8 input=file.sql

Count number of cases falling into same position

When multiple observation have the spatial coordinates, they can still be counted (if needed, coordinates can be uploaded to the attribute table by sql="SELECT long,lat,site_id,department,obs,COUNT(long) as count_cases \
               FROM diseases GROUP BY long,lat"


db.connect, db.describe, db.drivers, db.droptable, db.execute, db.login, db.tables

GRASS SQL interface


Original author unknown (probably CERL)
Modifications by Radim Blazek, ITC-Irst, Trento, Italy
Support for multiple statements by Martin Landa, Czech Technical University in Prague


Available at: source code (history)

Latest change: Monday Nov 18 20:15:32 2019 in commit: 1a1d107e4f6e1b846f9841c2c6fabf015c5f720d

Note: This document is for an older version of GRASS GIS that will be discontinued soon. You should upgrade, and read the current manual page.

Main index | Database index | Topics index | Keywords index | Graphical index | Full index

© 2003-2023 GRASS Development Team, GRASS GIS 7.8.9dev Reference Manual