Skip to content

Latest commit

 

History

History
522 lines (320 loc) · 9.65 KB

README.md

File metadata and controls

522 lines (320 loc) · 9.65 KB

Project Summary

In this project we will be practicing inserting and querying data using SQL. We'll make use of a handy online tool provided by DevMountain that will allow us to write SQL in your browser. 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.

Table - person

Instructions

  1. Create a table called person that records a person's id, name, age, height ( in cm ), city, favorite_color.
    • id should be an auto-incrementing id/primary key - Use type: SERIAL
  2. Add 5 different people into the person database.
    • Remember to not include the person_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 ( person_id SERIAL PRIMARY KEY, name VARCHAR(200), age INTEGER, height INTEGER, city VARCHAR(200), favorite_color VARCHAR(200) );
#2
INSERT INTO person ( name, age, height, city, favorite_color ) 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 favorite_color != 'red';
#11
SELECT * FROM person WHERE favorite_color != 'red' AND favorite_color != 'blue';
#12
SELECT * FROM person WHERE favorite_color = 'orange' OR favorite_color = 'green';
#13
SELECT * FROM person WHERE favorite_color IN ( 'orange', 'green', 'blue' );
#14
SELECT * FROM person WHERE favorite_color IN ( 'yellow', 'purple' )

Table - orders

Instructions

  1. Create a table called orders that records: order_id, person_id, product_name, product_price, quantity.
  2. Add 5 orders to the orders table.
    • Make orders for at least two different people.
    • person_id 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 person_id.

Solution

SQL Solutions
#1
CREATE TABLE orders ( order_id SERIAL PRIMARY KEY, person_id INTEGER, product_name VARCHAR(200), product_price NUMERIC, quantity INTEGER );
#2
INSERT INTO orders ( person_id, product_name, product_price, quantity ) VALUES ( 0, 'Product', 12.50, 2 );
#3
SELECT * FROM orders;
#4
SELECT SUM(quantity) FROM orders;
#5
SELECT SUM(product_price * quantity) FROM orders;
#6
/* The value of person_id depends on what IDs you used. Use a valid ID from your table */
SELECT SUM(product_price * quantity) FROM orders WHERE person_id = 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 birthdate for the youngest employee.
  3. Find the 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 first_name, last_name FROM employee WHERE city = 'Calgary';
#2
SELECT MAX(birth_date) from employee;
#3
SELECT MIN(birth_date) from employee;
#4
SELECT * FROM employee WHERE reports_to = 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 billing_country = '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 billing_state in ('CA', 'TX', 'AZ');
#7
SELECT AVG(total) FROM invoice;
#8
SELECT SUM(total) FROM invoice;

Resources

SQL

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.