This lab will follows up on this week's introduction to SQL, a language that allows you to quickly retrieve information about large datasets. In today's exercises, you will get practice writing SQL queries using a few example databases. Before getting started writing SQL, you will upload data from our provided smaller datasets, including nutritional stats from the USDA food database, as well as data for different video games and their lifetime sales.
Cloud 9 conviently has MySQL pre-installed for you to use. All you have to do is start it on the c9
environment and then run our provided setup.sql
file. This file will load the lab's data, populating three new tables: FoodData, Games, and Sales (Games and Sales both represent video game data and are related, as opposed to FoodData). The slides below provide a walkthrough to help you get cloud 9 set up with SQL.
The following steps will help you get started with using MySQL on Cloud 9:
setup.sql
file and then save it in a directory called sqllab
in Cloud 9.
mysql-ctl start
on the terminal and press enter, and then type mysql-ctl cli
and press enter. You should see something like the following:
use c9
on the terminal to use the provided cloud 9 database, and then type source sqllab/setup.sql
(or source path/to/setup.sql
if you saved the file elsewhere) to load the lab data. Loading the data may take a few seconds.
Query OK, 1 row affected...
messages.
Now you're ready to do the really cool stuff! First, we'll use the smaller FoodData table to fetch some data about food nutrition info found from the USDA nutrition database. Each food listed has data for a 100g serving, which makes it useful to compare nutrition information between different foods.
The following table (called a schema) shows the different types of data (as columns) in the FoodData table for each food item (row), where protein and carbs are given in grams:
FoodData
id | description | calories | protein | carb |
---|---|---|---|---|
09427 | Abiyuch, raw | 69.0 | 1.5 | 17.6 |
... | ... | ... | ... | ... |
For your first query, let's first see what foods we have available. To refresh your memory from yesterday's lecture, we'll give this one to you. Type the following SQL code in your terminal (you can open a new terminal and run mysql-ctl cli
if you closed it earlier):
SELECT description FROM FoodData;
This command will list all of the food descriptions in the FoodData table. You should see a number of rows populate your terminal after executing this command, followed by a result row count of 2417 rows. If you run into an error at this point, it may be a result of unsuccessfully setting up the databases in Exercise 1 in which case, you may want to go back and read through your command syntax.
Recall that the WHERE clause lets us specify criteria in our query to filter out results.
Show all food descriptions and their calorie counts from FoodData that have fewer than 20 calories per 100g (remember that all data is given for 100g in FoodData).
Write a query to return all food descriptions and calorie counts from FoodData that have more than 500 calories. Order the results in ascending order of calorie counts.
Write a query to return the descriptions , calorie count, grams of protein, and grams of carbohydrates for foods that have more grams of protein than carbohydrates. Order the results by alphabetical order.
Write a query to return the descriptions and calorie counts of foods that have the word 'apple' in them. Order the results in ascending order by calorie count (the calorie counts are stored as strings, so you will have to use the CAST function to turn them into DECIMALs).
In this part of the lab, you will use the video game datasets provided, represented in the Games and Sales tables, each which have 3000 rows of video game data.
Games
id | name | platform | release_year | genre | publisher | developer | rating |
---|---|---|---|---|---|---|---|
1 | Wii Sports | Wii | 2006 | Sports | Nintendo | Nintendo | E |
... | ... | ... | ... | ... | ... | ... | ... |
Sales
id | na_sales | eu_sales | japan_sales | other_sales | global_sales |
---|---|---|---|---|---|
1 | 41360000 | 28960000 | 3770000 | 8450000 | 82530000 |
... | ... | ... | ... | ... | ... |
Write a SQL query that returns the names of all games that were developed by Nintendo.
Write a SQL query that returns a list of all platform types in the Games table (e.g., Wii).
Write a SQL query that returns the names of the video games that were released in the earliest year recoded in the Games table.
Write a SQL query that returns the names and release year of the video games that have the word "Pokemon" in their name.
Write a SQL query that returns the largest amount of lifetime sales (in US dollars) from one video game in North America. How does this number copare to the largest sales from one game in Japan?
Write a SQL query that returns the sum of all global sales from in the Sales database.
Write a SQL query that returns the average of all global sales from in the Sales database.
Write a SQL query that returns the average of all global sales from in the Sales database.
If you've finished all of the SQL queries in this lab, great work! We recommend that you continue to practice different queries using this tutorial. Tomorrow, we will continue to go over a few more different SQL statements and introduce simple database manipulation!