-
Notifications
You must be signed in to change notification settings - Fork 9
Managing Data with SQL: Exercises
In SQL Online, type the following query and click "Run":
SELECT *
FROM FOOD_NAMEWhat if we want to get only specific entries from our data? The WHERE clause can be used after the FROM clause to add conditions to what data is retrieved, like so:
SELECT *
FROM FOOD_NAME
WHERE column operator value;-
columncontains the value we want to test -
operatoris the type of comparison (e.g.,=,<,>,<=,>=) -
valueis the value we want to match (e.g., a number)
Only data where the WHERE clause is true will be retrieved.
Refine the query from Exercise 1 using a WHERE clause to SELECT only food with a FoodID of 5, i.e. FoodID = 5
Query from Exercise 1:
SELECT *
FROM FOOD_NAMEDesign a query to SELECT foods with FoodIDs up to and including 7.
Design a query to SELECT foods in the poultry food group (FoodGroupID = 5), with a food source ID of 28.
Comparison operators can also be used with text, for example, using = to search for an exact text match (including capitalization):
Use the condition FoodDescription = 'Corn fritter' to SELECT only the food with the description of 'Corn fritter'
Create a query to retrieve (SELECT) data that contains the word apple anywhere in its FoodDescription column.
Create a query to retrieve data from FoodGroupID = 19 (Sweets) whose FoodDescription starts with dessert.
Use this query as a starting point 👇:
SELECT FoodID, FoodGroupID, FoodSourceID, FoodDescription
FROM FOOD_NAME
WHERE FoodDescription LIKE "%apple%";Reuse the query with LIKE (above) to sort retrieved data by FoodID, from highest to lowest.
Reuse the query with LIKE (above) to Sort retrieved data in ascending order on FoodDescription.
How can we use GROUP BY determine how many foods exist in each food group? What function should we use? What column should we use for grouping?
Modify the query below to use an SQL function and a GROUP BY clause to display the number of foods in each food group (i.e., for each FoodGroupID).
SELECT FoodGroupID
FROM FOOD_NAME- The
COUNT(*)function can be used to count the number of data entries (rows) that have been included in a group.
In the left pane of the SQLite IDE, expand each of the following tables to view their columns:
NUTRIENT_NAME-
NUTRIENT_AMOUNTWhich column do they have in common that would allow us toJOINthem on matching values?
Write a query to join the NUTRIENT_NAME and NUTRIENT_AMOUNT tables to get the nutrient breakdown (nutrient names and nutrient values) for food ID 20 (brie cheese).
Rewrite the query from EXERCISE 6 to use aliases nn and na for the NUTRIENT_NAME and NUTRIENT_AMOUNT tables.
Let's get a bit more practice joining tables:
- Take a look at the
FOOD_NAME,REFUSE_AMOUNTandREFUSE_NAMEtables - How can these tables be joined?
- Select a food you like.
- Write a query to print the amounts of each type of refuse (waste) produced by a portion of that food.
- Each line should list: the food name; the type of refuse; and the amount of refuse.
Ms. Flamm Boyant, France's foremost food critic is visiting your city! She has heard of Canada's intriguing "food groups" and her request is simple. She requires:
- A list of the food groups available (by name).
- The number of foods in each group.
- The groups listed from largest (most foods) to smallest.
Find her this data before she writes a bad review on social media!
- Where can you find food group names?
- What function can you use to get the number of foods per group?
- What 2 tables will you need to join in this query?