By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.

How to use Google Gemini with BigQuery

A Guide for SQL Code AI Assistance. By Fassil S. Yehuala

When you're diving into data analysis, figuring out what to ask and how to ask it can feel tricky. But fear not! 

Google Cloud's Gemini is here to lend a hand. Gemini, powered by AI, is an invaluable tool integrated into BigQuery, Google's data warehousing and analytics platform. Whether you're a seasoned data analyst or just dipping your toes into the world of SQL queries, Gemini is designed to assist you every step of the way.

Getting Started with Gemini

First things first, make sure Gemini is set up in your Google Cloud project. Once enabled, Gemini offers a plethora of features, including SQL query generation, completion, explanation, and even Python code generation.

Generating SQL Queries with Ease

One of Gemini's standout features is its ability to generate SQL queries effortlessly. By providing a natural language prompt, users can prompt Gemini to craft SQL statements tailored to their needs. Whether you are starting from scratch or have a vague idea of what you are looking for, Gemini can generate SQL queries to help you explore your data more effectively.

Example:

Natural Language Prompt: "Using bigquery-public-data.chicago_taxi_trips.taxi_trips, show me the total number of trips and the average fare amount for each payment type."

Generated SQL Query:

Feel free to tweak the prompt if Gemini's code isn't hitting the mark. Once you've got what you need, just hit "Insert" to drop the code in.

Completing SQL Queries Seamlessly

Gemini doesn't just stop at generating queries - it also assists in completing them. As you type away in the BigQuery query editor, Gemini analyzes your query's context and suggests logical next steps. Whether it's refining aggregations, adding window functions, or optimizing joins, Gemini's suggestions can streamline your query-writing process, making it more efficient and error-free.

Example:
As you start typing a query:

SELECT
 pickup_community_area,
 COUNT(*) AS total_trips
FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`

Gemini suggests completing the query:

If you're not happy with Gemini's code suggestion, you can ignore the suggestion and write your own. But if you like the suggestion, just hit the tab key to insert it into your code.

Explaining SQL Queries

Ever found yourself staring blankly at a convoluted SQL query, struggling to decipher its meaning? Fear not, Gemini can come to your rescue once again. Just select the code and click on the “Explain this query” button on the left, Gemini will provide a clear and concise explanation of even the most intricate SQL queries. This feature proves invaluable, especially when grappling with lengthy or complex queries where understanding the underlying logic is crucial.

Example:
Given the SQL Query:

SELECT 
  pickup_community_area,
  SUM(fare) + SUM(tips) + SUM(tolls) + SUM(extras) AS total_cost
FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
GROUP BY pickup_community_area;

Gemini Explanation:

  • The SELECT clause specifies the columns to be included in the result set:some text
    • pickup_community_area : The community area where the taxi trip started.
    • total_cost : The total cost of the taxi trip, calculated as the sum of the fare, tips, tolls, and extras.
  • The FROM clause specifies the input table:some text
    • bigquery-public-data.chicago_taxi_trips.taxi_trips : A publicly available BigQuery dataset containing historical Chicago taxi trip data.
  • The GROUP BY clause groups the results by the pickup_community_area column. This means that the query will calculate the total cost for each unique pickup community area.

The result of the query will be a table with two columns: pickup_community_area and total_cost . This table can be used to analyze the total cost of taxi trips in different areas of Chicago. For example, you could use this data to identify areas with high or low taxi fares or to compare the cost of taxi trips over time.

Disabling Gemini Features

While Gemini offers a wide range of features to enhance your BigQuery experience, there may be instances where you prefer to work without AI assistance. In such cases, disabling Gemini features is a straightforward process. Simply navigate to the BigQuery toolbar, click on the Gemini button, and clear the features you wish to disable—whether it's SQL query generation, completion, or explanation.

Conclusion

In the ever-expanding landscape of data analytics, Gemini emerges as a powerful ally, empowering users to unlock the full potential of BigQuery. From generating SQL queries to explaining complex code and everything in between, Gemini streamlines the data exploration and analysis process, enabling users to derive actionable insights with ease. 

So, whether you're a seasoned data scientist or a novice analyst, harness the power of Gemini to elevate your BigQuery experience to new heights.

But remember, with great power comes great responsibility—Gemini's suggestions are subject to validation, especially as it's an early-stage technology.

More to read

If you enjoyed this blog, you'll love these too! Dive into more captivating content:

Enhancing Data Representation in Looker Studio

Riding the Wave in Looker Studio

Latest Updates on Google Data Analytics (March 2024)

From Guesswork to Precision: Estimation Intervals in Looker Studio

Further Links

Follow us on LinkedIn for insights into our daily work and important updates on BigQuery, Data Studio, and marketing analytics.

Subscribe to our YouTube channel for discussions on Data Warehouse, BigQuery, Looker Studio, and Google Tag Manager.

If you are interested in learning BigQuery from scratch, get access to our free BigQuery Course

Elevate your skills with Google Data Studio and BigQuery by enrolling in our Udemy course.

Need help setting up a modern, cost-efficient data warehouse or analytical dashboard? Email us at hello@datadice.io to schedule a call.