-
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?
Brock O. Lee is adopting a new low-carb diet.
He requires foods matching the following criteria:
- Must be a vegetable.
- Must include the word green in its description.
- Must have less than 10 grams of carbs for a 100 gram portion. Your client wants a simple table listing all of these foods with the name of the food and the grams of carbs, from least to most.
Amanda A. Appleton has always hated the letter "z". This unusual phobic state has now resulted in a health condition, as she suffers from a zinc deficiency!
A woman her age requires 8 mg of zinc daily.
Her doctor recommends the following treatment:
- Eat foods providing at least half the daily requirement of zinc per 100g portion
- Eat foods containing the letter "z" in their descriptions, to desensitize yourself from the phobia Run a quick query to provide her with a list of suitable foods!
Your old high school math teacher bursts through the door!
He's joined an environmental organization and is convinced there's a conspiracy at play!
He needs a list of foods whose ID is evenly divided by their RefuseAmount.
You try to explain this doesn't make much sense, but he's adamant.
Note: A number is evenly divided by another when there's no remainder after division. The remainder (modulo) operator % can help you here.
Your old teacher's ecstatic!
He offers to pay you $0.50 for each unit of refuse in your list, instead of your flat rate of $200.00.
You're not sure if he's trying to short-change you or if he's truly lost his mind.
Adjust your query to calculate your pay and find out!