Overview
In this lab, you will learn more fundamentals of sports data science by writing and executing queries to query data stored in BigQuery tables. The emphasis of the lab is to illustrate how the database works and answer some interesting questions related to the following topics in soccer.
Most total goals scored.
Most attempted passes.
Best penalty success rate.
The data used in this lab comes from the following sources:
Pappalardo et al., (2019) A public data set of spatio-temporal match events in soccer competitions, Nature Scientific Data 6:236, https://www.nature.com/articles/s41597-019-0247-7
Pappalardo et al. (2019) PlayerRank: Data-driven Performance Evaluation and Player Ranking in Soccer via a Machine Learning Approach. ACM Transactions on Intelligent Systems and Technologies (TIST) 10, 5, Article 59 (September 2019), 27 pages. DOI: https://doi.org/10.1145/3343172
Objectives
In this lab, you will learn how to:
Query soccer match event data in BigQuery.
Write and execute queries that join information from multiple tables.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
- Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.
How to start your lab and sign in to the Google Cloud console
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
The Open Google Cloud console button
Time remaining
The temporary credentials that you must use for this lab
Other information, if needed, to step through this lab
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account.
If necessary, copy the Username below and paste it into the Sign in dialog.
student-00-a39e984f9cb5@qwiklabs.net
You can also find the Username in the Lab Details panel.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
ylupPmJv2Byc
You can also find the Password in the Lab Details panel.
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.
Note: Using your own Google Cloud account for this lab may incur extra charges.
Click through the subsequent pages:
Accept the terms and conditions.
Do not add recovery options or two-factor authentication (because this is a temporary account).
Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left.
Task 1. Open BigQuery
The BigQuery console provides an interface to query tables, including public datasets offered by BigQuery.
- In the Cloud Console, from the Navigation menu select BigQuery:
The Welcome to BigQuery in the Cloud Console message box opens. This message box provides a link to the quickstart guide and the release notes.
- Click Done.
The BigQuery console opens.
Note: The process for creating the dataset and tables is taught in the BigQuery Soccer Data Ingestion lab. In this lab the focus is on learning how to query the information.
Once the tables are created the display will be similar to this:
In the next section, begin to learn the fundamentals of creating queries in BigQuery.
Task 2. Matches with the most goals
In this section, create a query that joins together multiple tables featuring soccer data. Based on the information available, you can perform some basic analytics such as the most total goals scored in a match by both teams (in a specific league).
In the Query editor, click "+" (Create SQL query).
Add the following query to the query Editor:
SELECT
date,
label,
(team1.score + team2.score) AS totalGoals
FROM
`soccer.matches` Matches
LEFT JOIN
`soccer.competitions` Competitions ON
Matches.competitionId = Competitions.wyId
WHERE
status = 'Played' AND
Competitions.name = 'Spanish first division'
ORDER BY
totalGoals DESC, date DESC
Here is what the query will do:
joins the matches table (which has final scores) with the competitions table.
filter down to "Spanish first division" matches only.
order by a calculated field that represents total goals in a match.
- Click Run.
The results are displayed below the query window.
Click Check my progress to verify the objective
Check the query has been run
Check my progress
In this section BigQuery was used to illustrate how to define a query that shows soccer information. The query creates a filter that displays specific information about matches from a specific league and enables the information to be categorized by a defined field.
Task 3. Players with the most passes
In this section, create a query that joins together multiple tables featuring soccer data. Based on the information available, you can perform some basic analytics such as total passes by players.
In the Query editor, click "+" (Create SQL query).
Add the following query into the query Editor:
SELECT
playerId,
(Players.firstName || ' ' || Players.lastName) AS playerName,
COUNT(id) AS numPasses
FROM
`soccer.events` Events
LEFT JOIN
`soccer.players` Players ON
Events.playerId = Players.wyId
WHERE
eventName = 'Pass'
GROUP BY
playerId, playerName
ORDER BY
numPasses DESC
LIMIT 10
This query:
joins the events table (which has a record of every pass) with the players table to get player names from their IDs
groups by player
counts the number of passes for each one
orders by the players with the most passes
- Click Run. The results are displayed below the query window.
Click Check my progress to verify the objective
Check the query has been run
Check my progress
In this section BigQuery was used to illustrate how to define a query that shows player information. The query creates a join that displays specific information about a playerId and enables the information to be categorized by a defined field.
In the next section learn more about the existing dataset and explore how it can be used to determine the penalty kick success rate of players.
Task 4. Determine penalty kick success rate
In this section, create a query that joins together multiple tables featuring soccer data. Based on the information available, you can perform some analytics such as the success rate on penalty kicks by each player.
In the Query editor, click "+" (Create SQL query).
Copy and paste the following query into the query Editor:
SELECT
playerId,
(Players.firstName || ' ' || Players.lastName) AS playerName,
COUNT(id) AS numPKAtt,
SUM(IF(101 IN UNNEST(tags.id), 1, 0)) AS numPKGoals,
SAFE_DIVIDE(
SUM(IF(101 IN UNNEST(tags.id), 1, 0)),
COUNT(id)
) AS PKSuccessRate
FROM
`soccer.events` Events
LEFT JOIN
`soccer.players` Players ON
Events.playerId = Players.wyId
WHERE
eventName = 'Free Kick' AND
subEventName = 'Penalty'
GROUP BY
playerId, playerName
HAVING
numPkAtt >= 5
ORDER BY
PKSuccessRate DESC, numPKAtt DESC
The query aggregates the number of penalty kick attempts and successful ones by player and filters to those with at least 5 penalty kick attempts before ordering by success rate.
Note: The above query joins the events table, in this case filtered to only penalty kicks, with the players table to get player names from their IDs.
The tags field in the events table uses BigQuery's array functionality (allowing more than 1 tag to be stored per event), so it must be unnested to determine if the kick was good or not (one can confirm that tag 101 represents a goal using the tags2name table).
- Click Run. The results are displayed below the query window.
Click Check my progress to verify the objective
Check the query has been run
Check my progress
In this section BigQuery was used to illustrate how to define a query that shows player information relating to penalty kicks. The query creates a join that displays specific information about a playerId and enables more detailed information to be displayed.
Task 5. Pop quiz
Test your understanding of BigQuery by completing the short quiz on the topics covered in this lab.
How many different Spanish first division matches achieved the highest number of total goals in our data?
1
3
6
Which player attempted the most passes in the data?
Granit Hkaka
Kyle Walker
Tony Kroos
How many players attempted at least 5 penalty kicks in this data?
20
3
16
Solution of Lab
curl -LO raw.githubusercontent.com/quiccklabs/Labs_solutions/master/BigQuery%20Soccer%20Data%20Analysis/quicklabgsp849.sh
sudo chmod +x quicklabgsp849.sh
./quicklabgsp849.sh