Prepare and test your skills
Prepare and test your skills
BigQuery ML (opens in a new tab) is a capability in BigQuery that lets you create and train machine learning models with SQL. Models are stored in BigQuery datasets like tables and views, so training data does not have to move to Python or Java code. The CREATE MODEL (opens in a new tab) statement both creates the model object and starts training on the results of a query.
The CREATE MODEL workflow is the ordered lifecycle that moves data from a training query to a stored model and then to predictions. The workflow starts when CREATE MODEL reads training rows and ends when you inspect and explain the trained model. Default settings handle much of the work, while options let you tune the result.
A seven-step flow from running CREATE MODEL with training data through preprocessing, hyperparameter tuning, evaluation, ML.PREDICT inference, explainability, and model weights inspection.
CREATE MODEL statement that names the model, sets OPTIONS such as MODEL_TYPE, and supplies training data with a query.TRANSFORM clause.NUM_TRIALS to better fit the model to the training data.ML.PREDICT to score new data with the trained model.After training, later prediction queries read new input rows and pass them through the stored model object.
A CREATE MODEL statement names a destination model and defines how training should happen. The model name follows the form project.dataset.model, so the trained model lives with the data it learned from. An OPTIONS clause sets MODEL_TYPE and other settings, and a final query after AS SELECT supplies the rows and columns used for training.
The MODEL_TYPE option controls which algorithm BigQuery ML trains, so the choice must match the dataset and the desired outcome. Classification and regression models use INPUT_LABEL_COLS to identify the label column that holds the answer to predict. The TRANSFORM clause sits between the options and the training query and applies manual feature engineering every time the model trains or predicts. Data-split options such as DATA_SPLIT_METHOD, DATA_SPLIT_EVAL_FRACTION, and DATA_SPLIT_COL control which rows are reserved for evaluation during training.
Different model families require different required columns in the statement:
INPUT_LABEL_COLS for the label column, and logistic regression labels can have up to 50 unique values.USER_COL for user data, ITEM_COL for item data, and RATING_COL for rating data.TIME_SERIES_TIMESTAMP_COL for time points and TIME_SERIES_DATA_COL for the values to forecast.Internally trained models are built-in BigQuery ML models that train entirely inside BigQuery. These models support a dry run on the CREATE MODEL statement to estimate how much data the statement will process. Choose an internally trained model when you want fast SQL-based training without an external service.
low-value, medium-value, or high-value.ARIMA_PLUS and ARIMA_PLUS_XREG forecast values and detect anomalies while handling seasonality, holidays, and outliers.Externally trained, remote, and imported models extend BigQuery ML beyond built-in algorithms. The three categories differ by where training happens and how the CREATE MODEL statement connects to the model.
| Category | Where training happens | How CREATE MODEL connects |
|---|---|---|
| Externally trained | Agent Platform, using TensorFlow, XGBoost, or AutoML | Set MODEL_TYPE to options such as DNN_CLASSIFIER, BOOSTED_TREE_CLASSIFIER, RANDOM_FOREST_CLASSIFIER, or AUTOML_CLASSIFIER; dry run is not supported |
| Remote | Agent Platform or partner and Cloud AI services | Reference the deployed model by its HTTPS endpoint; the statement processes no bytes and incurs no BigQuery charges |
| Imported | Outside BigQuery, then loaded from Cloud Storage | Import formats such as Open Neural Network Exchange (ONNX), TensorFlow, and TensorFlow Lite for prediction inside BigQuery |
Exam tip: K-means training uses all input data and needs no label column, while linear regression, logistic regression, boosted tree, and similar supervised models reserve part of the data for evaluation and require a label setting such as INPUT_LABEL_COLS.
Evaluation in BigQuery ML (opens in a new tab) is the step that checks how well a trained model predicts new data. The ML.EVALUATE (opens in a new tab) function performs this check in SQL by comparing predictions against known true values and returning performance metrics. Those metrics help decide if the model is ready to use and which model performs better when models are compared.
ML.EVALUATE is a SQL table function that takes a stored model and an evaluation dataset as inputs and returns a single row of statistics. Data flows from the model object and from the evaluation table or query into the function, and the function outputs metrics by matching each prediction to its actual label value. For example, predictions flow from a model such as census.census_model and true labels flow from rows filtered as dataframe = 'evaluation', so the function measures performance on data the model did not train on. The result is one summary row, not one row per prediction, which makes overall effectiveness easy to read at a glance.
Automatic evaluation means BigQuery ML calculates evaluation metrics during the CREATE MODEL statement and stores them with the model. The dataset used for those stored metrics depends on the model type, the data-split options, and whether hyperparameter tuning is used. To retrieve the stored metrics later, call ML.EVALUATE on the model with no input data specified.
The evaluation source varies by model group:
DATA_SPLIT_METHOD, DATA_SPLIT_EVAL_FRACTION, and DATA_SPLIT_COL, plus DATA_SPLIT_TEST_FRACTION when hyperparameter tuning is used.Because the split controls which rows are reserved for evaluation, the same model code can produce different stored metrics when the split options change.
Evaluation with a new dataset means calling ML.EVALUATE with input data specified after the model already exists. In this pattern, fresh rows flow from a table or query into the function along with the stored model, and the function returns new metrics for that specific dataset. This approach tests how the model might perform outside the training set and supports fair comparison when two models are scored on the same new data. Choose this path when validating before deployment or when checking performance over time as new labeled data arrives.
Exam tip: ML.EVALUATE with no input data returns the metrics stored during CREATE MODEL, while ML.EVALUATE with input data scores the model on the new table or query you supply.
Classification and regression models use different metrics because they predict different kinds of outputs. Classification models predict categories, so their metrics describe how often predicted categories match true categories. Regression models predict numbers, so their metrics describe how far predicted numbers are from true numbers.
A useful model shows strong classification scores or low regression error on evaluation data, not only on training data, since evaluation data better reflects unseen use.
The ML.METRICS function computes evaluation metrics for classification or regression from any table or query that contains actual and predicted values. Unlike ML.EVALUATE, the ML.METRICS function does not depend on a stored model object, so predictions can flow directly from a query result into the function. This is useful when predictions come from another system, from manual logic, or from a model that was not saved in BigQuery. Choose ML.EVALUATE when validating a stored BigQuery ML model, and choose ML.METRICS when only actual and predicted columns are available.
BigQuery ML (opens in a new tab) is a capability in BigQuery that lets you create, train, and use machine learning models with SQL instead of Python or Java. Training data stays inside BigQuery tables or views, and the trained model is stored in a BigQuery dataset like a table or view. This approach brings machine learning to the data, so you do not have to move or reformat large datasets for an outside framework.
The BigQuery ML development workflow is an ordered lifecycle that moves data from training tables to a stored model and then to predictions. The workflow starts with a CREATE MODEL (opens in a new tab) statement that reads training data from BigQuery, and it ends with inference and explanation on new data. Default settings let you complete this workflow without deep machine learning knowledge, while tuning helps improve results.
CREATE MODEL statement that names the model, sets MODEL_TYPE, and selects training data from a table, view, or query.TRANSFORM clause.After creation, the stored model depends on the dataset that contains it, and later prediction queries read input data and pass it through that stored model.
Linear regression in BigQuery ML predicts the value of a numerical metric for new data from similar historical data. A common example is estimating a house sale price from location and house characteristics compared with past sales. You create a linear regression model by setting the MODEL_TYPE option to LINEAR_REG in the CREATE MODEL statement.
Labels for linear regression are real-valued numbers, which means they cannot be positive infinity, negative infinity, or NaN (Not a Number). During training, feature columns flow from the input table or query into the model, while the label column provides the true numeric value the model learns to predict. Choose linear regression when the answer you need is a number rather than a category.
Logistic regression in BigQuery ML classifies new data into two or more possible values using a model trained on similar labeled data. Examples include predicting whether an email is spam or whether a product review is positive, negative, or neutral. You create a logistic regression model by setting the MODEL_TYPE option to LOGISTIC_REG in the CREATE MODEL statement.
Labels for logistic regression are categories, and they can have up to 50 unique values such as low-value, medium-value, or high-value. During training, feature columns flow from the input table or query into the model, while the label column provides the correct category for each training row. Choose logistic regression when the answer you need is a category rather than a continuous number.
K-means clustering in BigQuery ML segments data into groups without using predefined labels. A common example is identifying customer segments from purchase behavior so each customer belongs to the most similar group. K-means is an unsupervised learning technique, so model training does not require labels or split data for training or evaluation.
During training, all of the input data is used as training data, and the model learns groupings based on similarity across the selected feature columns. After training, new rows flow into the prediction function and the model assigns each row to a cluster. Choose k-means clustering when you need to discover natural groups rather than predict a known label.
Exam tip: Linear regression predicts numbers, logistic regression predicts categories with up to 50 unique values, and k-means clustering groups data without labels.
The choice between linear regression, logistic regression, and k-means depends on the kind of question the model must answer and whether a label column exists. All three train on data from a BigQuery table, view, or query, and all three save the resulting model in the dataset for later evaluation and prediction. The key difference is whether the output is a number, a category, or a group assignment.
| Model | Predicts | Label requirement |
|---|---|---|
| Linear regression | A numeric value, such as sale price | Requires a real-valued numeric label that is not infinity or NaN |
| Logistic regression | A category, such as spam or not spam | Requires a categorical label with up to 50 unique values |
| K-means clustering | A group assignment, such as customer segment | No label required; learns from feature similarity |
project.dataset.model, sets MODEL_TYPE in OPTIONS, and supplies training data with an AS SELECT query.The CREATE MODEL statement both creates the model object and starts training on the results of a query. It names the destination model as project.dataset.model, sets MODEL_TYPE and other settings in OPTIONS, and supplies training rows and columns with an AS SELECT query.
Use linear regression when the answer you need is a number, logistic regression when the answer is a category, and k-means clustering when you need to discover natural groups. Linear regression requires a real-valued numeric label that is not infinity or NaN, logistic regression requires a categorical label with up to 50 unique values, and k-means clustering requires no label and learns from feature similarity.
ML.EVALUATE with no input data returns the evaluation metrics stored during CREATE MODEL, while ML.EVALUATE with input data scores the model on the new table or query you supply. Use the version with input data to test performance outside the training set and to fairly compare two models on the same new data.
ML.EVALUATE validates a stored BigQuery ML model by comparing its predictions against known true values, while ML.METRICS computes evaluation metrics directly from actual and predicted columns without a stored model object. Choose ML.EVALUATE when validating a stored model and choose ML.METRICS when only actual and predicted columns are available, such as predictions from another system or manual logic.
A data analyst has trained a linear regression model in BigQuery ML to predict customer transaction amounts. The analyst executes the ML.EVALUATE SQL function on an evaluation dataset to assess the model's predictive accuracy.
Which metric returned by the evaluation function measures the average absolute difference between target values and predicted values, where a lower score indicates better model performance?
Mean Absolute Error (MAE)
Precision
Confusion Matrix
R-squared (r²)
Last updated