Skip to content
Open
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
113 changes: 105 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,55 @@ 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

```sql
SELECT company_name, contact_name, city
FROM customers
WHERE city = 'London'
```

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

```sql
SELECT company_name, contact_name, city, postal_code
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

```sql
SELECT supplier_id, company_name, contact_name, 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

```sql
SELECT *
FROM orders
ORDER BY order_date DESC
```

### 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

```sql
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

> Don't forget the wildcard '%' symbols at the beginning and end of your substring to denote it can appear anywhere in the string in question

> Remember to convert your contact title to all upper case for case insenstive comparing so upper(contact_title)
```sql
SELECT *
FROM customers
WHERE UPPER(contact_title) LIKE '%MARKET%'
```


### add a customer record for
Expand All @@ -70,25 +95,58 @@ Answer the following data queries. Keep track of the SQL you write by pasting it
* the postal code is '111'
* the country is 'Middle Earth'
> This can be done with the INSERT INTO clause
```sql
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')

/* Check if the new record is in */
SELECT *
FROM customers
```

### update _Bilbo Baggins_ record so that the postal code changes to _"11122"_.
> This can be done with UPDATE and WHERE clauses
```sql
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)

```sql
SELECT contact_name, 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
```

### 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

```sql
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 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

```sql
SELECT city,
(SELECT COUNT(o.order_id)
FROM orders o
WHERE o.ship_city = c.city) as ordercount
FROM customers c
ORDER BY ordercount DESC
```

## Data Normalization

Expand All @@ -102,6 +160,45 @@ 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 |


### 1NF - pets broken into separate rows
* Person Table
| Person Id | Person Name | Fenced Yard | City Deweller |
|-----------|-------------|-------------|---------------|
| 1 | Jane | No | Yes |
| 2 | Bob | No | No |
| 3 | Sam | Yes | No |

* Pet 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 |

### 2NF - Fenced Yard is moved to pet table
* Person Table
| Person Id | Person Name | City Deweller |
|-----------|-------------|---------------|
| 1 | Jane | Yes |
| 2 | Bob | No |
| 3 | Sam | No |

* Pet Table
| Pet Id | Person Id | Pet Name | Pet Type | Fenced Yard |
|--------|-----------|------------|----------|-------------|
| 1 | 1 | Ellie | Dog | No |
| 2 | 1 | Tiger | Cat | No |
| 3 | 1 | Toby | Turtle | No |
| 4 | 2 | Joe | Horse | No |
| 5 | 3 | Ginger | Dog | No |
| 6 | 3 | Miss Kitty | Cat | No |
| 7 | 3 | Bubble | Fish | Yes |

---
## Stretch Goals

Expand Down