Stay organized with collectionsSave and categorize content based on your preferences.
Query a public dataset with the BigQuery Python client library
Query a public dataset with the BigQuery Python client library
Learn how to:
Activate Cloud Shell in a Google Cloud project.
Open the Cloud Shell Editor.
Prepare files for queries.
Query a public dataset in BigQuery.
Clean up.
Estimated time to complete:
ClickStartto begin.
Activate Cloud Shell in a Google Cloud project
If you do not enable billing for a project, you automatically work in the
BigQuery sandbox. The BigQuery sandbox lets you learn
BigQuery with a limited set of BigQuery
features at no charge. If you do not plan to use your project beyond this
document, we recommend that you use the BigQuery sandbox.
ClickActivate
Cloud Shell.Show
me
To learn how to open the Cloud Shell Editor, clickNext.
Open the Cloud Shell Editor
In Cloud Shell, create a new Python project and file:
To learn how to query a public dataset in BigQuery, clickNext.
Query a public dataset in BigQuery
ClickOpen Editor.
In theExplorerpane, locate yourBIGQUERY-PYTHON-QUICKSTARTproject.
Click theapp.pyfile to open it.
To create a query against thebigquery-public-data.stackoverflowdataset that returns the
top 10 most viewed Stack Overflow pages and their view counts, copy the
following code into theapp.pyfile:
fromgoogle.cloudimportbigquerydefquery_stackoverflow()->None:client=bigquery.Client()results=client.query_and_wait("""SELECTCONCAT('https://stackoverflow.com/questions/',CAST(id as STRING)) as url,view_countFROM `bigquery-public-data.stackoverflow.posts_questions`WHERE tags like '%google-bigquery%'ORDER BY view_count DESCLIMIT 10""")# Waits for job to complete.forrowinresults:print("{}:{}views".format(row.url,row.view_count))if__name__=="__main__":query_stackoverflow()
ClickOpen Terminal.
In the terminal, run theapp.pyscript. If you are prompted to authorize
Cloud Shell and agree to the terms, clickAuthorize.
To avoid incurring charges to your Google Cloud account, either delete
your Google Cloud project, or delete the resources that you created in
this walkthrough.
Delete the project
If you created a new project to learn about BigQuery and you no
longer need the project,delete it. Be aware
that deleting a project deletes everything in the project and custom project IDs
are lost.
Delete the resources
If you used an existing project, delete thebigquery-python-quickstartfolder
that you created:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["Query a public dataset with the BigQuery Python client library\n\n\nQuery a public dataset with the BigQuery Python client library\n==============================================================\n\nLearn how to:\n\n\u003cbr /\u003e\n\n1. Activate Cloud Shell in a Google Cloud project.\n2. Open the Cloud Shell Editor.\n3. Prepare files for queries.\n4. Query a public dataset in BigQuery.\n5. Clean up.\n\nEstimated time to complete:\n\nClick **Start** to begin.\n\nActivate Cloud Shell in a Google Cloud project\n----------------------------------------------\n\n1.\n\n If you do not enable billing for a project, you automatically work in the\n BigQuery sandbox. The BigQuery sandbox lets you learn\n BigQuery with a limited set of BigQuery\n features at no charge. If you do not plan to use your project beyond this\n document, we recommend that you use the BigQuery sandbox.\n2. \n3. Click **Activate\n Cloud Shell** . Show me\n\nTo learn how to open the Cloud Shell Editor, click **Next**.\n\nOpen the Cloud Shell Editor\n---------------------------\n\n1. In Cloud Shell, create a new Python project and file:\n\n mkdir bigquery-python-quickstart \\\n && touch \\\n bigquery-python-quickstart/app.py\n\n This command creates a Python project that's named\n `bigquery-python-quickstart` and a file that's named `app.py`.\n2. Open the Cloud Shell Editor:\n\n cloudshell workspace bigquery-python-quickstart\n\nTo learn how to prepare files for queries, click **Next**.\n\nPrepare files for queries\n-------------------------\n\n1. To open a terminal in the Cloud Shell Editor, click\n **Open Terminal**.\n\n2. Open your project directory:\n\n cd bigquery-python-quickstart\n\n3. Install the BigQuery client library for Python:\n\n pip install --upgrade google-cloud-bigquery\n\n The output is similar to the following. Several lines are omitted to\n simplify the output. \n\n Installing collected packages: google-cloud-bigquery\n ...\n Successfully installed google-cloud-bigquery-3.9.0\n ...\n\nTo learn how to query a public dataset in BigQuery, click\n**Next**.\n\nQuery a public dataset in BigQuery\n----------------------------------\n\n1. Click **Open Editor**.\n\n2. In the **Explorer** pane, locate your `BIGQUERY-PYTHON-QUICKSTART`\n project.\n\n3. Click the `app.py` file to open it.\n\n4. To create a query against the\n `bigquery-public-data.stackoverflow` dataset that returns the\n top 10 most viewed Stack Overflow pages and their view counts, copy the\n following code into the `app.py` file:\n\n\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n\n\n\n def query_stackoverflow() -\u003e None:\n client = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html()\n results = client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_query_and_wait(\n \"\"\"\n SELECT\n CONCAT(\n 'https://stackoverflow.com/questions/',\n CAST(id as STRING)) as url,\n view_count\n FROM `bigquery-public-data.stackoverflow.posts_questions`\n WHERE tags like '%google-bigquery%'\n ORDER BY view_count DESC\n LIMIT 10\"\"\"\n ) # Waits for job to complete.\n\n for row in results:\n print(\"{} : {} views\".format(row.url, row.view_count))\n\n\n if __name__ == \"__main__\":\n query_stackoverflow()\n\n \u003cbr /\u003e\n\n5. Click **Open Terminal**.\n\n6. In the terminal, run the `app.py` script. If you are prompted to authorize\n Cloud Shell and agree to the terms, click **Authorize**.\n\n python app.py\n\n The result is similar to the following: \n\n https://stackoverflow.com/questions/35159967 : 170023 views\n https://stackoverflow.com/questions/22879669 : 142581 views\n https://stackoverflow.com/questions/10604135 : 132406 views\n https://stackoverflow.com/questions/44564887 : 128781 views\n https://stackoverflow.com/questions/27060396 : 127008 views\n https://stackoverflow.com/questions/12482637 : 120766 views\n https://stackoverflow.com/questions/20673986 : 115720 views\n https://stackoverflow.com/questions/39109817 : 108368 views\n https://stackoverflow.com/questions/11057219 : 105175 views\n https://stackoverflow.com/questions/43195143 : 101878 views\n\nYou have successfully queried a public dataset with the\nBigQuery Python client library.\n\nTo avoid incurring charges to your account and learn about next steps, click\n**Next**.\n\nNext steps\n----------\n\n\nKeep the resources that you created and do more with BigQuery, or clean up to avoid\nbilling charges.\n\n### Do more with BigQuery\n\n- Learn more about using the [BigQuery Python client library](/python/docs/reference/bigquery/latest).\n- Learn more about [BigQuery public datasets](/bigquery/public-data).\n- Learn how to [load data into BigQuery](/bigquery/docs/loading-data).\n- Learn more about [querying data in BigQuery](/bigquery/docs/query-overview).\n- Get [updates about BigQuery](/bigquery/docs/release-notes).\n- Learn about [BigQuery pricing](/bigquery/pricing).\n- Learn about [BigQuery quotas and limits](/bigquery/quotas).\n\n### Clean up\n\nTo avoid incurring charges to your Google Cloud account, either delete\nyour Google Cloud project, or delete the resources that you created in\nthis walkthrough.\n\n### Delete the project\n\nIf you created a new project to learn about BigQuery and you no\nlonger need the project,\n[delete it](https://console.cloud.google.com/cloud-resource-manager). Be aware\nthat deleting a project deletes everything in the project and custom project IDs\nare lost.\n\n### Delete the resources\n\nIf you used an existing project, delete the `bigquery-python-quickstart` folder\nthat you created:\n\n1. In Cloud Shell, move up a directory:\n\n cd ..\n\n2. Delete the resources that you created:\n\n rm -R bigquery-python-quickstart\n\n The `-R` flag deletes all assets in a folder."]]