AliA1997 / sql-1-afternoon

An afternoon project to help solidify basic SQL queries.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Project Summary

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.

Table - People

Instructions

  1. 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
  2. Add 5 different people into the Person database.
    • Remember to not include the ID because it should auto-increment.
  3. List all the people in the Person table by Height from tallest to shortest.
  4. List all the people in the Person table by Height from shortest to tallest.
  5. List all the people in the Person table by Age from oldest to youngest.
  6. List all the people in the Person table older than age 20.
  7. List all the people in the Person table that are exactly 18.
  8. List all the people in the Person table that are less than 20 and older than 30.
  9. List all the people in the Person table that are not 27 (Use not equals).
  10. List all the people in the Person table where their favorite color is not red.
  11. List all the people in the Person table where their favorite color is not red and is not blue.
  12. List all the people in the Person table where their favorite color is orange or green.
  13. List all the people in the Person table where their favorite color is orange, green or blue (use IN).
  14. List all the people in the Person table where their favorite color is yellow or purple (use IN).

Solution

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" AND 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" )

Table - Orders

Instructions

  1. Create a table called Orders that records: PersonID, ProductName, ProductPrice, Quantity.
  2. Add 5 Orders to the Orders table.
    • Make orders for at least two different people.
    • PersonID should be different for different people.
  3. Select all the records from the Orders table.
  4. Calculate the total number of products ordered.
  5. Calculate the total order price.
  6. Calculate the total order price by a single PersonID.

Solution

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 * Quantity) FROM Orders;
#6
/* The value of PersonID depends on what IDs you used. Use a valid ID from your table */
SELECT SUM(ProductPrice * Quantity) FROM Orders WHERE PersonID = 0;

Table - Artist

Instructions

  1. Add 3 new Artists to the Artist table. ( It's already created )
  2. Select 10 artists in reverse alphabetical order.
  3. Select 5 artists in alphabetical order.
  4. Select all artists that start with the word "Black".
  5. Select all artists that contain the word "Black".

Solution

SQL Solutions
#1
INSERT INTO Artist ( Name ) VALUES ( 'artist name' );
#2
SELECT * FROM Artist ORDER BY Name Desc LIMIT 10;
#3
SELECT * FROM Artist ORDER BY Name ASC LIMIT 5;
#4
SELECT * FROM Artist WHERE Name LIKE 'Black%';
#5
SELECT * FROM Artist WHERE Name LIKE '%Black%';

Table - Employee

Instructions

  1. List all Employee first and last names only that live in Calgary.
  2. Find the first and last name and birthdate for the youngest employee.
  3. Find the first and last name and birthdate for the oldest employee.
  4. Find everyone that reports to Nancy Edwards (Use the ReportsTo column).
    • You will need to query the employee table to find the Id for Nancy Edwards
  5. Count how many people live in Lethbridge.

Solution

SQL Solutions
#1
SELECT FirstName, LastName FROM Employee WHERE City = "Calgary";
#2
SELECT FirstName, LastName, Max(BirthDate) FROM Employee;
#3
SELECT FirstName, LastName, Min(BirthDate) FROM Employee;
#4
SELECT * FROM Employee WHERE ReportsTo = 2;
#5
SELECT COUNT(*) FROM Employee WHERE City = "Lethbridge";

Table - Invoice

Instructions

  1. Count how many orders were made from the USA.
  2. Find the largest order total amount.
  3. Find the smallest order total amount.
  4. Find all orders bigger than $5.
  5. Count how many orders were smaller than $5.
  6. Count how many orders were in CA, TX, or AZ (use IN).
  7. Get the average total of the orders.
  8. Get the total sum of the orders.

Solution

SQL Solutions
#1
SELECT Count(*) FROM Invoice WHERE BillingCountry = 'USA';
#2
SELECT Max(total) FROM Invoice;
#3
SELECT Min(total) FROM Invoice;
#4
SELECT * FROM Invoice WHERE Total > 5;
#5
SELECT COUNT(*) FROM Invoice WHERE Total < 5;
#6
SELECT Count(*) FROM Invoice WHERE BillingState in ('CA', 'TX', 'AZ');
#7
SELECT AVG(Total) FROM Invoice;
#8
SELECT SUM(Total) FROM Invoice;

Contributions

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.

Copyright

© 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.

About

An afternoon project to help solidify basic SQL queries.