This topic describes how to enable standard SQL for use with your BigQuery statements.
To learn how to get started with the BigQuery web UI, see the Quickstart Using the Web UI.
To learn how to get started with the bq command-line tool, see the Quickstart Using the bq Command-Line Tool.
Setting the useLegacySql option
To enable standard SQL for a query:
Web UI
- Open the BigQuery web UI.
- Click Compose Query.
- Click Show Options.
- Uncheck the Use Legacy SQL checkbox.
Command-line
Add the--use_legacy_sql=false flag to your command line
statement.
bq query --use_legacy_sql=false "SELECT word FROM publicdata.samples.shakespeare"
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
When running a synchronous query, standard SQL is used by default.
When running an asynchronous query, standard SQL is used by default.
To use legacy SQL when running queries, set the UseLegacySql parameter to true.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
When running a synchronous query, set the UseStandard parameter to true to use standard SQL syntax.
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
When running a synchronous query, set the useLegacySql parameter to false to use standard SQL syntax.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
When running a synchronous query, set the useLegacySql parameter to false to use standard SQL syntax.
When running an asynchronous query, set the useLegacySql parameter to false to use standard SQL syntax.
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
When running a synchronous query, set the useLegacySql parameter to False to use standard SQL syntax.
When running an asynchronous query, set the useLegacySql parameter to False to use standard SQL syntax.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
When running a synchronous query, set the use_legacy_sql parameter to False to use standard SQL syntax.
When running an asynchronous query, set the use_legacy_sql parameter to False to use standard SQL syntax.
Setting a query prefix
You can set the SQL variant, either legacy SQL or standard SQL, by including a prefix to your query in the web UI, REST API, or when using a client library.
| Prefix | Description |
|---|---|
#legacySQL |
Runs the query using legacy SQL |
#standardSQL |
Runs the query using standard SQL |
For example, if you copy and paste the following query into the web UI, BigQuery runs the query using standard SQL and ignores the default Use Legacy SQL option setting.
#standardSQL
SELECT
weight_pounds, state, year, gestation_weeks
FROM
`bigquery-public-data.samples.natality`
ORDER BY weight_pounds DESC
LIMIT 10;
If you explicitly set the useLegacySql option, it must be consistent with the
prefix. For example, in the web UI, if you uncheck the Use Legacy SQL
option, you cannot use the #legacySQL prefix.
The query prefixes #legacySQL and #standardSQL:
- Are NOT case-sensitive
- Must precede the query
- Must be separated from the query by a newline character
Some third-party tools might not support this prefix if, for example, they modify the query text before sending it to BigQuery.
What's next
- To learn more about standard SQL, see SQL Reference.