Skip to content

Managing Data with SQL: Exercises

Jonathan edited this page Apr 28, 2023 · 10 revisions

Exercise 1: SELECT

In SQL Online, type the following query and click "Run":

SELECT *
FROM FOOD_NAME

Exercise 2: WHERE

What 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;
  • column contains the value we want to test
  • operator is the type of comparison (e.g., =, <, >, <=, >=)
  • value is the value we want to match (e.g., a number)

Only data where the WHERE clause is true will be retrieved.

Exercise 2A

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_NAME

Exercise 2B

Design a query to SELECT foods with FoodIDs up to _and including 7.

Exercise 2C

Design a query to SELECT foods in the poultry food group (FoodGroupID = 5), with a food source ID of 28.

Exercise 2D

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'

Exercise 3: Matching text with LIKE

Exercise 3A

Exercise 3B

Exercise 4: ORDER BY

Exercise 4A

Exercise 4B

Exercise 5: GROUP BY

Exercise 6: JOIN

Exercise 7: Table aliases

Stretch Exercise(s)

More than one JOIN

Challenge Problems

Challenge 1: Critical groups

Challenge 2: I want my greens!

Challenge 3: Zigzag meal plan

Challenge 4: No leftovers!

Challenge 4B: Leftovers = PROFIT

Clone this wiki locally