Studying up for the Tokyo 2021 Olympics with SQL
By: Sejal Dua
Tags: Olympics, Postgresql, Sql, Tokyo Olympics, Deep Dives

Studying up for the Tokyo 2021 Olympics with SQL

Now more than ever, I am wishing I was a professional athlete in the best shape of my life, gearing up for the Tokyo 2021 Olympics. But rather than feeling sorry for myself as I set up my spot on the couch with pillows, blankets, and a non-vegetable snack, I figured this would be an opportune time to apply some of my SQL knowledge (shoutout to Udemy and Coursera) and learn a thing or two from the historical olympic dataset. Four — well, now five — years is a significant amount of time. To put it into perspective, at the time of the last Summer Olympics, I had never coded before and had approximately zero exposure to data science. In this span of time, it can be difficult to situate ourselves into the right spirit whilst watching the olympics. I know I personally just stare at the screen in awe of the unimaginable athletic prowess being put on display. Not this time around, though!

The motivation for this project is ask and answer questions about how the Olympics have evolved over time, including questions about the participation and performance of women, the variation in athletic build (i.e. height, weight, age) by sport and by nation, and the nature of olympic dominance by nation, which is demonstrated by number of medals received.

I will be using a historical dataset on the modern Olympic Games, which includes all the Games (Summer and Winter) from Athens 1896 to Rio 2016. The source has been scraped from www.sports-reference.com and released as a Kaggle dataset (thanks to Kaggle user rgriffin). If you would like to follow along, you will want to download the 2 available files in this dataset: athlete_events.csv and noc_regions.csv. Most of the data is contained within the former, but the latter file will be useful in order to perform a SQL join on our data tables and obtain country / region names instead of National Olympic Committee 3 letter codes.

Screenshot of Kaggle dataset

The data attributes that we will be working with include the following: id, name, sex, age, height, weight, team, noc, games, year, season, city, sport, event, medal. Note that there are a few values of “NA” which represent missing data, thus the only column containing purely integers is the year column. Also note that while the Olympic Games now alternate between Summer and Winter sports every 2 years, that has not always been the case, which is why the season column will be useful.

In this step-by-step tutorial, I will walk you through different types of SQL queries, ranging in difficulty and complexity. As for getting set up in a coding environment, you will want to make sure you have PostgreSQL and PgAdmin installed on your local machine. If you do not have these already installed and need help doing so, please consult the references section at the end of this article. Next you will want to fire up a server through your localhost. You can do this through the psql command line or via PgAdmin. The default port will likely be 5432. I named my server “playground” but the name of the server does not matter so much. Just make sure you remember the name of your database and your username because we will be needing those later.

Screenshot of PostgreSQL server configuration

You will also want to navigate to your preferred code editor for writing Python code. For the most enjoyable experience, I recommend hopping into a blank Jupyter Notebook. Within your Python environment, make sure you install the following packages: psycopg2, pandas, and matplotlib.

In the first cell of your notebook, paste the following imports:

import psycopg2

import pandas.io.sql as sqlio

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns # optional

plt.style.use('ggplot') # optional

The first thing we must do is establish a connection to the PostgreSQL database using the psycopg2 library.

Use psycopg2 methods to create a connection to the database, create tables, and copy data into tables

One fetched result from the executed SQL query

Now that all our data is located within our two relational database tables, we should be able to access it with a simple select statement. However, one less than ideal aspect of the psycopg2 library is that it is a bit difficult to view the result set in a digestible format.

While the cursor class allows us to retrieve data from the database by iteration or using methods such as fetchone() , fetchmany() , and fetchall() , it would be neat to be able to view the data in a tabular format.