diff --git a/README.md b/README.md index a24715b..9d11850 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,11 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same
hint * This can be done with SELECT and WHERE clauses + + SELECT * + FROM customers + Where city = 'London' +
```SQL @@ -45,6 +50,11 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same
hint * This can be done with SELECT and WHERE clauses + + SELECT * + FROM customers + Where postal_code = '1010' +
```SQL @@ -55,6 +65,10 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same
hint +SELECT * +FROM suppliers +Where supplier_id = '11' + * This can be done with SELECT and WHERE clauses
@@ -64,6 +78,11 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same * [ ] ***list orders descending by the order date. The order with date 1998-05-06 should be at the top*** + +SELECT * +FROM orders +ORDER BY order_date DESC +
hint * This can be done with SELECT, WHERE, and ORDER BY clauses @@ -75,6 +94,10 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same * [ ] ***find all suppliers who have names longer than 20 characters. Returns 11 records*** +SELECT * + FROM suppliers + WHERE length(company_name) > 20 +
hint * This can be done with SELECT and WHERE clauses @@ -87,6 +110,10 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same * [ ] ***find all customers that include the word 'MARKET' in the contact title. Should return 19 records*** +SELECT * +FROM customers +WHERE upper(contact_title) LIKE '%MARKET%' +
hint * This can be done with SELECT and a WHERE clause using the LIKE keyword @@ -108,6 +135,19 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same * the country is 'Middle Earth'
hint + + INSERT INTO customers(customer_id, company_name, contact_name, address, city, postal_code, country) +VALUES('SHIRE', 'The Shire', 'Bilbo Baggins', '1 Hobbit-Hole', 'Bag End', '111', 'Middle Earth') + + +-- INSERT INTO customers(customer_id, company_name, contact_name) +-- VALUES(9191, 'Lambda School', 'John Mitchell') + +-- INSERT INTO customers(customer_id, company_name, contact_name) +-- SELECT 9192, company_name, contact_name +-- FROM suppliers +-- WHERE company_name LIKE 'Big%' + * This can be done with the INSERT INTO clause
@@ -117,6 +157,10 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same * [ ] ***update _Bilbo Baggins_ record so that the postal code changes to _"11122"_*** +UPDATE customers +SET postal_code = '11122' +Where customer_id = 'SHIRE' +
hint * This can be done with UPDATE and WHERE clauses @@ -128,18 +172,51 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same * [ ] ***list orders grouped and ordered by customer company name showing the number of orders per customer company name. _Rattlesnake Canyon Grocery_ should have 18 orders*** +SELECT company_name, + (SELECT COUNT(o.order_id) + FROM orders o + WHERE o.customer_id = c.customer_id) as ordercount +FROM customers c +ORDER BY ordercount +
hint * This can be done with SELECT, COUNT, JOIN and GROUP BY clauses. Your count should focus on a field in the Orders table, not the Customer table * There is more information about the COUNT clause on [W3 Schools](https://www.w3schools.com/sql/sql_count_avg_sum.asp)
+SELECT c.customer_id as idfromcustomer, o.customer_id as idfromorder, o.order_date, c.company_name, c.contact_name +FROM orders o RIGHT JOIN customers c +ON o.customer_id = c.customer_id +WHERE o.order_date is NULL + + +SELECT c.customer_id as idfromcustomer, o.customer_id as idfromorder, o.order_date, c.company_name, c.contact_name +FROM orders o RIGHT JOIN customers c +ON o.customer_id = c.customer_id +WHERE o.order_date is null + +SELECT c.customer_id as idfromcustomer, o.customer_id as idfromorder, o.order_date, c.company_name, c.contact_name +FROM orders o RIGHT JOIN customers c +on o.customer_id = c.customer_id + +SELECT order_data, company_name, contact_name +FROM orders JOIN customers + ```SQL ``` * [ ] ***list customers by contact name and the number of orders per contact name. Sort the list by the number of orders in descending order. _Jose Pavarotti_ should be at the top with 31 orders followed by _Roland Mendal_ with 30 orders. Last should be _Francisco Chang_ with 1 order*** +SELECT contact_name, + (SELECT COUNT(o.order_id) + FROM orders o + WHERE o.customer_id = c.customer_id) as ordercount +FROM customers c +ORDER BY ordercount DESC + +
hint * This can be done by adding an ORDER BY clause to the previous answer and changing the group by field @@ -151,6 +228,14 @@ Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same * [ ] ***list orders grouped by customer's city showing the number of orders per city. Returns 69 Records with _Aachen_ showing 6 orders and _Albuquerque_ showing 18 orders*** +SELECT city, + (SELECT COUNT(o.order_id) //makes another table and grabs from that or filters cities + FROM orders o + WHERE o.ship_city = c.city) as ordercount +FROM customers c +GROUP BY c.city +ORDER BY ordercount DESC +
hint * This is very similar to the previous two queries, however, it focuses on the City rather than the Customer Names @@ -177,55 +262,64 @@ Below are some empty tables to be used to normalize the database * Not all of the cells will contain data in the final solution * Feel free to edit these tables as necessary -Table Name: -| | | | | | | | | | +Table Name: People + +| Person Id | Name | | | | | | | | |------------|------------|------------|------------|------------|------------|------------|------------|------------| -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | +| 1 | Jane | | | | | | | | | +| 2 | Bob | | | | | | | | +| 3 | Sam | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | -Table Name: +Table Name: Pet Types -| | | | | | | | | | +|Pet Type Id | Pet Type | | | | | | | | |------------|------------|------------|------------|------------|------------|------------|------------|------------| -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | +| 1 | Cat | | | | | | | | +| 2 | Fish | | | | | | | | +| 3 | Turtle | | | | | | | | +| 4 | Dog | | | | | | | | +| 5 | Horse | | | | | | | | | | | | | | | | | | | | | | | | | | | | -Table Name: +Table Name: Pet Names -| | | | | | | | | | +| Pet Name | Pet Id |Pet Type Id | Location Id| Person Id | | | | | |------------|------------|------------|------------|------------|------------|------------|------------|------------| -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | +| Ellie | 1 | 4 | 1 | 1 | | | | | +| Joe | 2 | 5 | 3 | 2 | | | | | +| Ginger | 3 | 4 | 1 | 3 | | | | | +| Tiger | 4 | 1 | 2 | 1 | | | | | +| Miss Kitty| 5 | 1 | 2 | 3 | | | | | +| Toby | 6 | 3 | 3 | 1 | | | | | +| Bubble | 7 | 2 | 3 | 3 | | | | | -Table Name: -| | | | | | | | | | +Table Name: Locations + +| Locations |Location Id | | | | | | | | |------------|------------|------------|------------|------------|------------|------------|------------|------------| -| | | | | | | | | | -| | | | | | | | | | -| | | | | | | | | | +|Fenced Yard | 1 | | | | | | | | +|City Dweller| 2 | | | | | | | | +|Other | 3 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --- +3NF (third normal form) + +No derived fields +Eliminate fields that do not depend on the key +In an employees tables, do not store the name of departments. If a department has no employees, there is no where to store the department name. Store the department name in a separate table and use foreign keys to reference it. + + ### Stretch Goals @@ -264,3 +358,232 @@ To see the script * Copy the script from the text file into the SQL code block above! ![Database Script](assets/jx-12-m3-script.gif) + + +//GUIDED PROJECT + +//QUERIES + +//------------------------------------------------ +SELECT * +FROM customers +//------------------------------------------------ + +//------------------------------------------------ +SELECT company_name, contact_name, contact_title +FROM customers +//------------------------------------------------ + +//------------------------------------------------ +SELECT company_name, contact_name, country +FROM customers +WHERE country = 'Sweden' +//------------------------------------------------ + +//------------------------------------------------ + +SELECT product_name, units_in_stock +FROM products +WHERE units_in_stock < 10 + +//------------------------------------------------ + +//------------------------------------------------ +SELECT company_name, contact_name, city, country +FROM customers +WHERE UPPER(country) in ('USA', 'JAPAN', 'GERMANY') +ORDER BY country DESC, city +//------------------------------------------------ + +//------------------------------------------------ +SELECT product_id, product_name, unit_price +FROM products +ORDER BY unit_price +LIMIT 5 +//------------------------------------------------ + +//------------------------------------------------ +SELECT count(*) +FROM products + +-- this gives us how many products we have -- +//------------------------------------------------ + +//------------------------------------------------ +SELECT COUNT(DISTINCT unit_price) +FROM products +//------------------------------------------------ + +//------------------------------------------------ + +SELECT MIN(unit_price) +FROM products + +--find minimum price-- + +//------------------------------------------------ + +//------------------------------------------------ + +SELECT MAX(unit_price) +FROM products +//------------------------------------------------ + +//------------------------------------------------ + +//SUB QUERY +SELECT * +FROM products +WHERE unit_price = + (SELECT MAX(unit_price) + FROM products) + +//------------------------------------------------ + +//-------------------------------------------- +SELECT DISTINCT quantity_per_unit +FROM products + +SELECT DISTINCT quantity_per_unit, COUNT(*) +FROM products +GROUP BY quantity_per_unit + +//--------------------------------------------- + +//--------------------------------------------- +SELECT DISTINCT quantity_per_unit, COUNT(*) +FROM products +GROUP BY quantity_per_unit +ORDER BY 2 DESC +//--------------------------------------------- + +//--------------------------------------------- +SELECT DISTINCT quantity_per_unit, COUNT(*) +FROM products +GROUP BY quantity_per_unit +HAVING COUNT(*) > 1 +ORDER BY 2 DESC +//--------------------------------------------- + + + +//--------------------------------------------- +SELECT o.order_date, c.company_name, c.contact_name +FROM orders o JOIN customers c +on o.customer_id = c.customer_id + +-- HOW DO I WANT TO MATCH up the rows +//--------------------------------------------- + + + +//--------------------------------------------- +SELECT o.customer_id, c.customer_id +FROM orders o JOIN customers c +on o.customer_id = c.customer_id +//--------------------------------------------- + +//--------------------------------------------- +SELECT o.order_date, c.company_name, c.contact_name +FROM orders o RIGHT JOIN customers c +on o.customer_id = c.customer_id + +--RIGHT + +//--------------------------------------------- + +//--------------------------------------------- +SELECT c.customer_id as idfromcustomer, o.customer_id as idfromorder, o.order_date, c.company_name, c.contact_name +FROM orders o RIGHT JOIN customers c +ON o.customer_id = c.customer_id +//--------------------------------------------- + + + + + +//--------------------------------------------- +SELECT c.customer_id as idfromcustomer, o.customer_id as idfromorder, o.order_date, c.company_name, c.contact_name +FROM orders o RIGHT JOIN customers c +ON o.customer_id = c.customer_id +WHERE o.order_date is NULL +//--------------------------------------------- + + + + +//--------------------------------------------- +INSERT INTO customers(customer_id, company_name, contact_name) +VALUES(9191, 'Lambda School', 'John Mitchell') +//--------------------------------------------- + + + +//--------------------------------------------- +SELECT * FROM CUSTOMERS +//--------------------------------------------- + + + +//--------------------------------------------- +SELECT 9192, company_name, contact_name +FROM suppliers +WHERE company_name LIKE 'Big%' +//--------------------------------------------- + + + +//--------------------------------------------- +INSERT INTO customers(customer_id, company_name, contact_name) +SELECT 9192, company_name, contact_name +FROM suppliers +WHERE company_name LIKE 'Big%' +//--------------------------------------------- + + + +//UPDATE + +//--------------------------------------------- +UPDATE products +SET discontinued = 1 +WHERE unit_price < 10.00 +//--------------------------------------------- + +//--------------------------------------------- +UPDATE suppliers +SET city = 'Oslo', phone = '5555555555', fax = '1234567890' +WHERE supplier_id = 15 + + +---MUST HAVE WHERE CLAUSE !!!! +--MUST HAVE WHERE CLAUSE !!!! so ONLY the one you +want is updated and not the entire table + +//--------------------------------------------- + +//DELETE + +//--------------------------------------------- +DELETE +FROM products +WHERE unit_price < 10.00 + + + +GOT THIS: +ERROR: update or delete on table "products" violates foreign key constraint "fk_order_details_products" on table "order_details" +DETAIL: Key (product_id)=(13) is still referenced from table "order_details". +SQL state: 23503 + +THIS MEANS ANOTHER TABLE RELIES ON THIS DATA AND +MUST DELETE FROM OTHER TABLE FIRST + +//--------------------------------------------- + + +//--------------------------------------------- +DELETE +FROM customers +WHERE customer_id = '9191 +//--------------------------------------------- \ No newline at end of file