Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,44 @@ Answer the following data queries. Keep track of the SQL you write by pasting it
### find all customers that live in London. Returns 6 records.
> This can be done with SELECT and WHERE clauses

SELECT *
FROM customers
WHERE city = 'London'


### find all customers with postal code 1010. Returns 3 customers.
> This can be done with SELECT and WHERE clauses

SELECT *
FROM customers
WHERE postal_code = '1010'


### find the phone number for the supplier with the id 11. Should be (010) 9984510.
> This can be done with SELECT and WHERE clauses

SELECT phone
FROM suppliers
WHERE supplier_id = '11'


### list orders descending by the order date. The order with date 1998-05-06 should be at the top.
> This can be done with SELECT, WHERE, and ORDER BY clauses

SELECT *
FROM orders
ORDER BY order_date DESC

# did not need to use WHERE


### find all suppliers who have names longer than 20 characters. You can use `length(company_name)` to get the length of the name. Returns 11 records.
> This can be done with SELECT and WHERE clauses

SELECT *
FROM suppliers
WHERE length(company_name) > 20


### find all customers that include the word 'MARKET' in the contact title. Should return 19 records.
> This can be done with SELECT and a WHERE clause using the LIKE keyword
Expand All @@ -60,6 +82,10 @@ Answer the following data queries. Keep track of the SQL you write by pasting it

> Remember to convert your contact title to all upper case for case insenstive comparing so upper(contact_title)

SELECT *
FROM customers
WHERE UPPER(contact_title) LIKE '%MARKET%'


### add a customer record for
* customer id is 'SHIRE'
Expand All @@ -71,24 +97,48 @@ Answer the following data queries. Keep track of the SQL you write by pasting it
* the country is 'Middle Earth'
> This can be done with the INSERT INTO clause

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')


### update _Bilbo Baggins_ record so that the postal code changes to _"11122"_.
> This can be done with UPDATE and WHERE clauses

UPDATE customers
SET postal_code = '11122'
WHERE contact_name = 'Bilbo Baggins'


### list orders grouped by customer showing the number of orders per customer. _Rattlesnake Canyon Grocery_ should have 18 orders.
> 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 count(o.customer_id), c.company_name
FROM orders o JOIN customers c
ON o.customer_id = c.customer_id
GROUP BY c.company_name


### list customers names and the number of orders per customer. Sort the list by number of orders in descending order. _Save-a-lot Markets should be at the top with 31 orders followed by _Ernst Handle_ with 30 orders. Last should be _Centro comercial Moctezuma_ with 1 order.
> This can be done by adding an ORDER BY clause to the previous answer

SELECT count(o.customer_id), c.company_name
FROM orders o JOIN customers c
ON o.customer_id = c.customer_id
GROUP BY c.company_name
ORDER BY count(o.customer_id) DESC


### list orders grouped by customer's city showing number of orders per city. Returns 69 Records with _Aachen_ showing 6 orders and _Albuquerque_ showing 18 orders.
> This is very similar to the previous two queries, however, it focuses on the City rather than the CustomerName

SELECT count(o.ship_city), c.city
FROM orders o JOIN customers c
ON o.customer_id = c.customer_id
GROUP BY c.city



## Data Normalization

Expand All @@ -102,6 +152,66 @@ Take the following data and normalize it into a 3NF database.
| Bob | Joe | Horse | | | | | No | No |
| Sam | Ginger | Dog | Miss Kitty | Cat | Bubble | Fish | Yes | No |


1st Nf

Person Table

| Person id | Person Name | Fenced Yard | City Dweller |
|---------- |-------------|-------------|--------------|
| 1 | Jane | No | Yes |
| 2 | Bob | No | No |
| 3 | Sam | Yes | No |

Pets Table

| Pet id | Person id | Pet Name | Pet Type |
| ------ | --------- | ---------- | -------- |
| 1 | 1 | Ellie | Dog |
| 2 | 1 | Tiger | Cat |
| 3 | 1 | Toby | Turtle |
| 4 | 2 | Joe | Horse |
| 5 | 3 | Ginger | Dog |
| 6 | 3 | Miss Kitty | Cat |
| 7 | 3 | Bubble | Fish |


2nd NF

Person Table

| Person id | Person Name | Living id |
|---------- |-------------|-----------|
| 1 | Jane | 1 |
| 2 | Bob | 2 |
| 3 | Sam | 3 |

Living Table

| Living id | Fenced Yard | City Dweller |
|---------- |-------------|--------------|
| 1 | No | Yes |
| 2 | No | No |
| 3 | Yes | No |

Pets Table

| Pet id | Person id | Pet Name | Pet Type |
| ------ | --------- | ---------- | -------- |
| 1 | 1 | Ellie | Dog |
| 2 | 1 | Tiger | Cat |
| 3 | 1 | Toby | Turtle |
| 4 | 2 | Joe | Horse |
| 5 | 3 | Ginger | Dog |
| 6 | 3 | Miss Kitty | Cat |
| 7 | 3 | Bubble | Fish |


3rd NF

There is no derived column, because the living table states fenched yard and city dweller info, which does not give cconclusive data to derive one from the other.


---
## Stretch Goals

Expand Down
2 changes: 2 additions & 0 deletions northwind_psql/Docker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
create_db.sh
northwind.sql
4 changes: 4 additions & 0 deletions northwind_psql/Docker/1_docker_run_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cp ../northwind.sql .
cp ../create_db.sh .
docker build -t northwind_image .
docker run --name northwind_container -d northwind_image
1 change: 1 addition & 0 deletions northwind_psql/Docker/2_docker_psql_client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker run -it --rm --link northwind_container:postgres postgres psql -h postgres -U postgres
3 changes: 3 additions & 0 deletions northwind_psql/Docker/3_docker_delete_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker stop northwind_container
docker rm northwind_container
docker image rm northwind_image
5 changes: 5 additions & 0 deletions northwind_psql/Docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM postgres
USER postgres
COPY northwind.sql /docker-entrypoint-initdb.d/northwind.sql
COPY create_db.sh /docker-entrypoint-initdb.d/create_db.sh
CMD ["postgres"]
45 changes: 45 additions & 0 deletions northwind_psql/Docker/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# northwind_psql/Docker

Simple scripts in order to help running the northwind database in a docker container.

The usage of these scripts is optional: they are provided in order to be able to test this database
without having to install postgres on your machine. They work under osx or linux.

## Pre-requirement : install docker

https://www.docker.com/get-started


## Step 1 : run a docker container with the server

Simply run `./1_docker_server.sh`. This will build a docker image named 'northwind_image' and start a *detached* docker container named 'northwind_container' which will be the server.

````bash
> cd Docker
> ./1_docker_run_server.sh
````


## Step 2: run a docker container with a psql client that will connect to the server

Simply run `./2_docker_psql_client.sh`. This will run a postgres docker container that will connect to the server, and run psql against it.


````bash
> ./2_docker_psql_client.sh
psql (10.5 (Debian 10.5-1.pgdg90+1))
Type "help" for help.

postgres=# select * from us_states;
state_id | state_name | state_abbr | state_region
----------+----------------------+------------+--------------
1 | Alabama | AL | south
2 | Alaska | AK | north
...
````

## Step 3 : clean up

Run `./3_docker_delete_server.sh`. This will **destroy** the server (as well as any data that you might have changed)!


Binary file added northwind_psql/ER.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added northwind_psql/EntityRelation model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions northwind_psql/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(The northwind database originates from microsoft : http://northwinddatabase.codeplex.com/license)

# Microsoft Public License (Ms-PL)
Microsoft Public License (Ms-PL)

This license governs use of the accompanying software. If you use the software, you accept this license.
If you do not accept the license, do not use the software.

1. Definitions

The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning
here as under U.S. copyright law.

A "contribution" is the original software, or any additions or changes to the software.

A "contributor" is any person that distributes its contribution under this license.

"Licensed patents" are a contributor's patent claims that read directly on its contribution.

2. Grant of Rights

(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution,
prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.


(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each
contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use,
sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the
contribution in the software.

3. Conditions and Limitations

(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.

(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your
patent license from such contributor to the software ends automatically.

(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices
that are present in the software.

(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a
complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
code form, you may only do so under a license that complies with this license.

(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a
particular purpose and non-infringement.
6 changes: 6 additions & 0 deletions northwind_psql/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Northwind database for Postgres

A simple sql script that will populate a database with the famous northwind example,
adapted for postgres.

<img src=ER.png />
12 changes: 12 additions & 0 deletions northwind_psql/create_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

dropdb --if-exists northwind
dropuser --if-exists northwind_user

createdb northwind
psql northwind < northwind.sql

psql --no-psqlrc template1 -c "create user northwind_user;"
psql --no-psqlrc template1 -c "alter user northwind_user password 'thewindisblowing';"
psql --no-psqlrc template1 -c "grant all on DATABASE northwind to northwind_user;"
psql --no-psqlrc northwind -c "GRANT ALL on ALL tables IN SCHEMA public to northwind_user"
Loading