Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Google Cloud · Associate Data Practitioner

SQL in BigQuery

Associate Data Practitioner · Lesson 5 of 16 · 34:43

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Google Cloud Associate Data Practitioner exam, on the official exam guide. In this lesson: from a business question to a query, aggregating data with GROUP BY and HAVING, window functions and joins, and keeping queries fast and cheap (selected columns, partitions, clustering, the dry run estimate). Includes three original practice questions, with Cymbal Energy, a fictional electricity and gas supplier, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Questions from managers

Cymbal Energy's managers have questions. Which towns use the most electricity at peak hours? Which customers' usage jumped this month? The answers sit in BigQuery, and the data practitioner turns each business question into SQL.

From a business question to a query

Start from the business question, not from the data. Define the metric, like total kilowatt hours, the level of detail, like each town and each day, and the time period. Then find the tables that hold it, and check what one row means, so that nothing is counted twice. Only then write the query in BigQuery Studio, and save it, so that others can reuse it.

Aggregating data

Most reports aggregate. Select the columns you need, filter rows with where, group them with group by, and compute totals and averages with functions like sum, count, and average. Filter the groups themselves with having, for example the towns above a threshold, and sort the result with order by. Common table expressions, written with the keyword with, split a long query into named steps that are easy to read. And Gemini in BigQuery can write or explain SQL from a plain language prompt, but you still check the result.

Window functions and joins

Window functions compute across related rows, without collapsing them. Rank orders customers within each town, and a running sum adds up consumption day after day, with an over clause that sets the partition and the order. Lag reads the value of the previous row, so you can compare each month with the month before, and spot jumps in usage. Joins combine tables: an inner join keeps matching rows only, and a left join keeps every row of the first table, even without a match. Join on the right keys, or rows multiply, and totals go wrong.

Fast and cheap queries

On demand, BigQuery charges queries by the bytes they read, so a few habits matter. Select only the columns you need, and avoid select star on wide tables: on a table that is not clustered, adding limit does not reduce the bytes read. Filter on the partition column, like the reading date, so BigQuery skips whole partitions, and clustering narrows the scan further. Check the estimate shown before you run a query, or test it with a dry run, which reads nothing. And save results that many reports reuse, instead of running the same heavy query again and again.

Recap

Let's recap. Start from the business question: the metric, the level of detail, and the period. Group by computes totals, and having filters the groups. Window functions like rank, running sums, and lag compare rows without collapsing them. Inner joins keep matches, left joins keep every row of the first table, and the wrong keys multiply rows. And read less to pay less: needed columns only, partition filters, clustering, and an estimate before you run.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A manager wants the towns whose total consumption last month was above one million kWh. After grouping the readings by town, which clause keeps only those towns?

    • WHERE, on each reading
    • ORDER BY, on the total
    • LIMIT, on the result
    • HAVING, on the total per town
    Show the answer

    HAVING, on the total per town

    Having filters the groups after aggregation, so it keeps only the towns above the threshold. “WHERE, on each reading” only filters single readings, before any total exists. “ORDER BY, on the total” only sorts the towns, and keeps all of them. And “LIMIT, on the result” only cuts the result to a number of rows, whatever their totals.

  2. For each customer, the analyst wants this month’s consumption next to last month’s, on the same row, to spot sudden jumps. Which SQL feature fits?

    • A GROUP BY on the month column
    • The LAG window function
    • A cross join of all the months
    • A HAVING clause
    Show the answer

    The LAG window function

    The lag window function reads last month's value onto the same row as this month's. “A GROUP BY on the month column” would give one row per month, with no comparison on the same row. “A cross join of all the months” would pair every month with every other month, and multiply the rows. And “A HAVING clause” only filters groups, without reading another row.

  3. A dashboard query reads every column of a 10 TB readings table, partitioned by day, with no clustering. The dashboard shows only yesterday’s totals for three columns. How can the scan be reduced the most?

    • Add a LIMIT clause to the query
    • Sort the result with ORDER BY
    • Select the three columns, filter on yesterday’s partition
    • Run the query with batch priority
    Show the answer

    Select the three columns, filter on yesterday’s partition

    Selecting only the three columns, and filtering on yesterday's partition, reads a tiny part of the table. “Add a LIMIT clause to the query” would not reduce the bytes scanned on a table that is not clustered. “Sort the result with ORDER BY” only sorts the result, and still reads every column. And “Run the query with batch priority” only changes the priority of the query, not the bytes it reads.