Fetch the results of a SQL statement - Amazon Redshift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Fetch the results of a SQL statement

To fetch the result from a SQL statement that ran, use the redshift-data get-statement-result or redshift-data get-statement-result-v2 Amazon CLI command. The results from get-statement-result are in JSON format. The results from get-statement-result-v2 are in CSV format. You can provide an Id that you receive in response to execute-statement or batch-execute-statement. The Id value for a SQL statement run by batch-execute-statement can be retrieved in the result of describe-statement and is suffixed by a colon and sequence number such as b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652:2. If you run multiple SQL statements with batch-execute-statement, each SQL statement has an Id value as shown in describe-statement. Authorization to run this command is based on the caller's IAM permissions.

The following statement returns the result of a SQL statement run by execute-statement that let the ResultFormat default to JSON. To retrieve the results, call the get-statement-result operation.

aws redshift-data get-statement-result --id d9b6c0c9-0747-4bf4-b142-e8883122f766

The following statement returns the result of the second SQL statement run by batch-execute-statement.

aws redshift-data get-statement-result --id b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652:2

The following is an example of the response to a call to get-statement-result where the SQL result is returned in JSON format in the Records key of the response.

{ "ColumnMetadata": [ { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "userid", "length": 0, "name": "userid", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "query", "length": 0, "name": "query", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": true, "isCurrency": false, "isSigned": false, "label": "label", "length": 0, "name": "label", "nullable": 0, "precision": 320, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "bpchar" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "xid", "length": 0, "name": "xid", "nullable": 0, "precision": 19, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int8" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "pid", "length": 0, "name": "pid", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": true, "isCurrency": false, "isSigned": false, "label": "database", "length": 0, "name": "database", "nullable": 0, "precision": 32, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "bpchar" }, { "isCaseSensitive": true, "isCurrency": false, "isSigned": false, "label": "querytxt", "length": 0, "name": "querytxt", "nullable": 0, "precision": 4000, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "bpchar" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": false, "label": "starttime", "length": 0, "name": "starttime", "nullable": 0, "precision": 29, "scale": 6, "schemaName": "", "tableName": "stll_query", "typeName": "timestamp" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": false, "label": "endtime", "length": 0, "name": "endtime", "nullable": 0, "precision": 29, "scale": 6, "schemaName": "", "tableName": "stll_query", "type": 93, "typeName": "timestamp" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "aborted", "length": 0, "name": "aborted", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "insert_pristine", "length": 0, "name": "insert_pristine", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "concurrency_scaling_status", "length": 0, "name": "concurrency_scaling_status", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" } ], "Records": [ [ { "longValue": 1 }, { "longValue": 3 }, { "stringValue": "health" }, { "longValue": 1023 }, { "longValue": 15279 }, { "stringValue": "dev" }, { "stringValue": "select system_status from stv_gui_status;" }, { "stringValue": "2020-08-21 17:33:51.88712" }, { "stringValue": "2020-08-21 17:33:52.974306" }, { "longValue": 0 }, { "longValue": 0 }, { "longValue": 6 } ] ], "TotalNumRows": 1 }

The following example shows a SQL statement run by execute-statement to return results as JSON. The table testingtable has three integer columns (col1, col2, col3) and there are three rows with values (1, 2, 3), (4, 5, 6), and (7, 8, 9).

aws redshift-data execute-statement --database dev --sql "SELECT col1, col2, col3 FROM testingtable" --cluster-id mycluster-test --result-format JSON
{ "ClusterIdentifier": "mycluster-test", "CreatedAt": "2024-04-02T16:45:25.144000+00:00", "Database": "dev", "DbUser": "IAMR:Administrator", "Id": "d468d942-6df9-4f85-8ae3-bac01a61aec3" }

The following is an example of the response to a call to get-statement-result where the SQL result is returned in JSON format in the Records key of the response.

aws redshift-data get-statement-result --id d468d942-6df9-4f85-8ae3-bac01a61aec3
{ "Records": [ [ { "longValue": 1 }, { "longValue": 2 }, { "longValue": 3 } ], [ { "longValue": 4 }, { "longValue": 5 }, { "longValue": 6 } ], [ { "longValue": 7 }, { "longValue": 8 }, { "longValue": 9 } ] ], "ColumnMetadata": [ { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col1", "name": "col1", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col2", "name": "col2", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col3", "name": "col3", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 } ], "TotalNumRows": 3 }

The following example shows a SQL statement run by execute-statement to return results as a CSV. The table testingtable has three integer columns (col1, col2, col3) and there are three rows with values (1, 2, 3), (4, 5, 6), and (7, 8, 9).

aws redshift-data execute-statement --database dev --sql "SELECT col1, col2, col3 FROM testingtable" --cluster-id mycluster-test --result-format CSV
{ "ClusterIdentifier": "mycluster-test", "CreatedAt": "2024-04-02T16:45:25.144000+00:00", "Database": "dev", "DbUser": "IAMR:Administrator", "Id": "d468d942-6df9-4f85-8ae3-bac01a61aec3" }

The following is an example of the response to a call to get-statement-result-v2 where the SQL result is returned in CSV format in the Records key of the response. Rows are separated by carriage return and newline (\r\n). The first row returned in Records are the column headers. Results returned in CSV format are returned in 1 MB where each chunk can store any number of rows up to 1MB.

aws redshift-data get-statement-result-v2 --id d468d942-6df9-4f85-8ae3-bac01a61aec3
{ "Records": [ { "CSVRecords": "col1,col2,col3\r\n1,2,3\r\n4,5,6\r\n7,8,9\r\n" } ], "ColumnMetadata": [ { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col1", "name": "col1", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col2", "name": "col2", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col3", "name": "col3", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 } ], "TotalNumRows": 3, "ResultFormat": "csv" }