In this project we will be practicing inserting and querying data using SQL. We'll make use of a handy online tool called Chinook that we'll use to write SQL online. Click me
On the left are the Tables with their fields, the right is where we will be writing our queries, and the bottom is where we will see our results.
Any new tables or records that we add into the database will be removed after you refresh the page.
Use www.sqlteaching.com or sqlbolt.com as resources for the missing keywords you'll need.
- Create a table called Person that records a person's ID, Name, Age, Height ( in cm ), City, FavoriteColor.
- ID should be an auto-incrementing id/primary key - Use type: INTEGER PRIMARY KEY AUTOINCREMENT
- Add 5 different people into the Person database.
- Remember to not include the ID because it should auto-increment.
- List all the people in the Person table by Height from tallest to shortest.
- List all the people in the Person table by Height from shortest to tallest.
- List all the people in the Person table by Age from oldest to youngest.
- List all the people in the Person table older than age 20.
- List all the people in the Person table that are exactly 18.
- List all the people in the Person table that are less than 20 and older than 30.
- List all the people in the Person table that are not 27 (Use not equals).
- List all the people in the Person table where their favorite color is not red.
- List all the people in the Person table where their favorite color is not red or blue.
- List all the people in the Person table where their favorite color is orange or green.
- List all the people in the Person table where their favorite color is orange, green or blue (use IN).
- List all the people in the Person table where their favorite color is yellow or purple (use IN).
SQL Solutions
#1
CREATE TABLE Person ( ID INTEGER PRIMARY KEY AUTOINCREMENT, Name string, Age integer, Height integer, City string, FavoriteColor string );
#2
INSERT INTO Person ( Name, Age, Height, City, FavoriteColor ) VALUES ( "First Last", 21, 182, "City", "Color" );
#3
SELECT * FROM Person ORDER BY Height DESC;
#4
SELECT * FROM Person ORDER BY Height ASC;
#5
SELECT * FROM Person ORDER BY Age DESC;
#6
SELECT * FROM Person WHERE Age > 20;
#7
SELECT * FROM Person WHERE Age = 18;
#8
SELECT * FROM Person WHERE Age < 20 OR Age > 30;
#9
SELECT * FROM Person WHERE Age != 27;
#10
SELECT * FROM Person WHERE FavoriteColor != "red";
#11
SELECT * FROM Person WHERE FavoriteColor != "red" OR FavoriteColor != "blue";
#12
SELECT * FROM Person WHERE FavoriteColor = "orange" OR FavoriteColor = "green";
#13
SELECT * FROM Person WHERE FavoriteColor IN ( "orange", "green", "blue" );
#14
SELECT * FROM Person WHERE FavoriteColor IN ( "yellow", "purple" )
- Create a table called Orders that records: PersonID, ProductName, ProductPrice, Quantity.
- PersonID should be different for different people.
- Make orders for at least two different people.
- Add 5 Orders to Order table.
- Select all the records from the Order table.
- Calculate the total number of products ordered.
- Calculate the total order price.
- Calculate the total order price by a single PersonID.
SQL Solutions
#1
CREATE TABLE Orders ( PersonID integer, ProductName string, ProductPrice float, Quantity integer );
#2
INSERT INTO Orders ( PersonID, ProductName, ProductPrice, Quantity ) VALUES ( 0, "Product", 12.50, 2 );
#3
SELECT * FROM Orders;
#4
SELECT SUM(Quantity) FROM Orders;
#5
SELECT SUM(ProductPrice) FROM Orders;
#6
/* The value of PersonID depends on what IDs you used. Use a valid ID from your table */
SELECT SUM(ProductPrice) FROM Orders WHERE PersonID = 0;
- Add 3 new Artists to the Artist table. ( It's already created )
- Select the top 10 artists in reverse alphabetical order
- Select the top 5 artists in alphabetical order
- Select all artists that start with the word Black
- Select all artists that contain the word Black
SQL Solutions
#1
INSERT INTO Artists ( Name ) VALUES ( 'artist name' );
#2
SELECT * FROM Artists ORDER BY Name Desc LIMIT 10;
- Add 2 new Employees to the Employee table
- List all Employee first and last names only that live in Calgary
- Find the first and last name for the youngest employee
- Find the first and last name for the oldest employee
- Find everyone that reports to Nancy Edwards (Use the ReportsTo column)
- Count how many people live in Lethbridge
- Use the Invoice table for the following
- Count how many orders were made from the USA
- Find the largest order total amount
- Find the smallest order total amount
- Find all orders bigger than $5
- Count how many orders were smaller than $5
- Count how many orders were in CA, TX, or AZ (use IN)
- Get the average total of the orders
- Get the total sum of the orders
If you see a problem or a typo, please fork, make the necessary changes, and create a pull request so we can review your changes and merge them into the master repo and branch.
© DevMountain LLC, 2017. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.