If you don't enable billing for the Trusted Cloud project that you use in this
tutorial, then you will work with data in the BigQuery sandbox. The
BigQuery sandbox lets you learn BigQuery with a limited
set of BigQuery features at no charge.
At the bottom of the Trusted Cloud console, a
Cloud Shell
session starts and displays a command-line prompt. Cloud Shell is a shell environment
with the Google Cloud CLI
already installed and with values already set for
your current project. It can take a few seconds for the session to initialize.
View details about a public dataset
BigQuery offers several sample tables in the
bigquery-public-data.samples dataset that you can query. In this tutorial,
you run queries on the shakespeare table, which contains an entry for every
word in every Shakespeare play.
View details about the shakespeare table in the samples dataset:
bqshowbigquery-public-data:samples.shakespeare
The output is similar to the following. Some columns are omitted to simplify the
output.
Last modified Schema Total Rows Total Bytes
----------------- ------------------------------------ ------------ ------------
14 Mar 17:16:45 |- word: string (required) 164656 6432064
|- word_count: integer (required)
|- corpus: string (required)
|- corpus_date: integer (required)
Query a public dataset
Use the bq query command to run SQL queries on data.
If you launched Cloud Shell from the documentation, enter the
following command to set your project ID. This prevents you from having to
specify the project ID in each CLI command.
gcloudconfigsetprojectPROJECT_ID
Replace PROJECT_ID with your project ID.
Determine how many times the substring raisin appears in Shakespeare's
works:
bqquery--use_legacy_sql=false\'SELECT word, SUM(word_count) AS count FROM bigquery-public-data.samples.shakespeare WHERE word LIKE "%raisin%" GROUP BY word'
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-26 UTC."],[[["\u003cp\u003eThis tutorial guides users on how to examine and query a public dataset using the \u003ccode\u003ebq\u003c/code\u003e command-line tool within the Google Cloud environment, specifically focusing on the \u003ccode\u003eshakespeare\u003c/code\u003e table in the \u003ccode\u003ebigquery-public-data.samples\u003c/code\u003e dataset.\u003c/p\u003e\n"],["\u003cp\u003eUsers can test the functionality within a limited environment in the BigQuery sandbox without incurring charges, as long as billing is not enabled for their project, allowing users to work with a limited set of features.\u003c/p\u003e\n"],["\u003cp\u003eThe tutorial demonstrates how to use the \u003ccode\u003ebq show\u003c/code\u003e command to examine the structure of the \u003ccode\u003eshakespeare\u003c/code\u003e table, and the \u003ccode\u003ebq query\u003c/code\u003e command to run SQL queries to search for specific words or substrings, such as "raisin" and "huzzah".\u003c/p\u003e\n"],["\u003cp\u003eThe guide instructs users on how to clean up resources, including deleting the Google Cloud project to avoid ongoing charges, with specific caveats about potential data loss when deleting a project.\u003c/p\u003e\n"],["\u003cp\u003eAdditional resources are provided to extend the learning experience, such as further documentation on using the \u003ccode\u003ebq\u003c/code\u003e tool, the BigQuery sandbox, public datasets, loading data, querying data, pricing and quotas.\u003c/p\u003e\n"]]],[],null,["Query a public dataset with the bq tool Learn how to examine and query a public dataset with the bq command-line tool.\n\n*** ** * ** ***\n\nTo follow step-by-step guidance for this task directly in the\nGoogle Cloud console, click **Guide me**:\n\n[Guide me](https://console.cloud.google.com/freetrial?redirectPath=/?walkthrough_id=bigquery--query-public-dataset-bq)\n\n*** ** * ** ***\n\nBefore you begin\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n- [Create or select a Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects).\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n - Create a Google Cloud project:\n\n ```\n gcloud projects create PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with a name for the Google Cloud project you are creating.\n - Select the Google Cloud project that you created:\n\n ```\n gcloud config set project PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your Google Cloud project name.\n\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n- [Create or select a Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects).\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n - Create a Google Cloud project:\n\n ```\n gcloud projects create PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with a name for the Google Cloud project you are creating.\n - Select the Google Cloud project that you created:\n\n ```\n gcloud config set project PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your Google Cloud project name.\n\n1.\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n2. If you don't enable billing for the Google Cloud project that you use in this tutorial, then you will work with data in the BigQuery sandbox. The BigQuery sandbox lets you learn BigQuery with a limited set of BigQuery features at no charge.\n3. Ensure that the BigQuery API is enabled.\n\n [Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=bigquery)\n\n If you created a new project, the BigQuery API is automatically\n enabled.\n4. In the Google Cloud console, activate Cloud Shell.\n\n [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n\n\n At the bottom of the Google Cloud console, a\n [Cloud Shell](/shell/docs/how-cloud-shell-works)\n session starts and displays a command-line prompt. Cloud Shell is a shell environment\n with the Google Cloud CLI\n already installed and with values already set for\n your current project. It can take a few seconds for the session to initialize.\n\n\u003cbr /\u003e\n\nView details about a public dataset\n\nBigQuery offers several sample tables in the\n`bigquery-public-data.samples` dataset that you can query. In this tutorial,\nyou run queries on the `shakespeare` table, which contains an entry for every\nword in every Shakespeare play.\n\nView details about the `shakespeare` table in the `samples` dataset: \n\n bq show bigquery-public-data:samples.shakespeare\n\nThe output is similar to the following. Some columns are omitted to simplify the\noutput. \n\n Last modified Schema Total Rows Total Bytes\n ----------------- ------------------------------------ ------------ ------------\n 14 Mar 17:16:45 |- word: string (required) 164656 6432064\n |- word_count: integer (required)\n |- corpus: string (required)\n |- corpus_date: integer (required)\n\nQuery a public dataset\n\nUse the `bq query` command to run SQL queries on data.\n\n1. If you launched Cloud Shell from the documentation, enter the\n following command to set your project ID. This prevents you from having to\n specify the project ID in each CLI command.\n\n gcloud config set project \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your project ID.\n2. Determine how many times the substring `raisin` appears in Shakespeare's\n works:\n\n bq query --use_legacy_sql=false \\\n 'SELECT\n word,\n SUM(word_count) AS count\n FROM\n bigquery-public-data.samples.shakespeare\n WHERE\n word LIKE \"%raisin%\"\n GROUP BY\n word'\n\n The output is similar to the following: \n\n +---------------+-------+\n | word | count |\n +---------------+-------+\n | praising | 8 |\n | Praising | 4 |\n | raising | 5 |\n | dispraising | 2 |\n | dispraisingly | 1 |\n | raisins | 1 |\n +---------------+-------+\n\n3. Search for the substring `huzzah` in Shakespeare's works:\n\n bq query --use_legacy_sql=false \\\n 'SELECT\n word\n FROM\n bigquery-public-data.samples.shakespeare\n WHERE\n word = \"huzzah\"'\n\n Because the substring doesn't appear in Shakespeare's works, no results are\n returned.\n\nClean up\n\n\nTo avoid incurring charges to your Google Cloud account for\nthe resources used on this page, delete the Google Cloud project with the\nresources.\n\nDelete the project If you used the [BigQuery sandbox](/bigquery/docs/sandbox) to query the public dataset, then billing is not enabled for your project, and you don't need to delete it.\n\n\u003cbr /\u003e\n\n\nThe easiest way to eliminate billing is to delete the project that you\ncreated for the tutorial.\n\nTo delete the project:\n\n| **Caution** : Deleting a project has the following effects:\n|\n| - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.\n| - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.\n|\n|\n| If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects\n| can help you avoid exceeding project quota limits.\n1. In the Google Cloud console, go to the **Manage resources** page.\n\n [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)\n2. In the project list, select the project that you want to delete, and then click **Delete**.\n3. In the dialog, type the project ID, and then click **Shut down** to delete the project.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nWhat's next\n\n- Learn more about [using the bq command-line tool](/bigquery/bq-command-line-tool).\n- Learn how to [load and query data using the bq command-line tool](/bigquery/docs/quickstarts/load-data-bq).\n- Learn about the [BigQuery sandbox](/bigquery/docs/sandbox).\n- Learn more about [BigQuery public datasets](/bigquery/public-data)."]]