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
57 changes: 57 additions & 0 deletions KB-NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#### Setting up for this project
- install Postgres
- install pgAdmin4, open in browser
- clone/copy sql to local


__inside pgAdmin4:__
- pgAdmin is good for viewing and making slight changes
- creating db is not done pgAdmin
- writing DML is done in Spring

__Create new Postgres DB:__
- on server, right click -> 'Create' -> 'Server'
- you can have mulitple servers
- General Settings tab: name server
- ex: 'My local PG server'
- this will show as a new DB in Postgres
- Connection tab: set Host name/address to 'localhost'
- leave port at 5432 (default)
- Username is Postgres user/pass
- try 'password'
- environment variables are set with user/pass?
- Save
- Schemas:
- usually have multiple schemas
- multiple copies of the db to test and experiement
- when testing/developing, don't affect production

__Create a DB:__
- on 'Database', right click -> 'create' -> 'create db'
- General settings: set Database name (ex: 'Northwind2')

__Add to DB/create table:__
- 'Tools' -> 'Query Tool'
- most of the queries are done here
- click 'open folder icon'
- select sql document
- this brings in DML (data manipulation lang)
- execute by clicking top center lighting bolt icon
- refresh table list
- right click -> 'refresh'
- tables should appear

__view a single table:__
- tables are also called entities
- right click on any table -> 'view and edit data' -> 'all rows'
- in our Java world:
- table is our class
- columns are our class fields
- rows are individual objects for the class
- ability to change data in db at this point

__SQL__
- open new query editor, clear previous query
- 'Tools' -> 'Query tool'
- make query
- to run, click upper right/center lightning bolt icon
127 changes: 102 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# java-sql

A student that completes this project shows that they can:
* Query data from a single table
* Query data from multiple tables
* Create a new datadaase using PostgreSQL

- Query data from a single table
- Query data from multiple tables
- Create a new datadaase using PostgreSQL

# Introduction

Expand All @@ -17,78 +18,128 @@ clone https://github.com/pthom/northwind_psql.git

## pgAdmin

* Right Click Databases
* Create
* type in northwind2
- Right Click Databases

* Tools -> Query Tool
* Open file northwind.sql (from cloned repo)
* Execute
- Create
- type in northwind2

* Look under
* northwind2 -> Schemas -> public -> tables
- Tools -> Query Tool

* Clear query windows
- Open file northwind.sql (from cloned repo)
- Execute

Answer the following data queries. Keep track of the SQL you write by pasting it into this document under its appropriate header below. You will be submitting that through the regular fork, change, pull process.
- Look under

- northwind2 -> Schemas -> public -> tables

- Clear query windows

Answer the following data queries. Keep track of the SQL you write by pasting it into this document under its appropriate header below. You will be submitting that through the regular fork, change, pull process.

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

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

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

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

### add a customer record for

### add a customer record for
* customer id is 'SHIRE'
* company name is 'The Shire'
* contact name is 'Bilbo Baggins'
* the address is '1 Hobbit-Hole'
* ths city is 'Bag End'
* the postal code is '111'
* the country is 'Middle Earth'
> This can be done with the INSERT INTO clause
- customer id is 'SHIRE'
- company name is 'The Shire'
- contact name is 'Bilbo Baggins'
- the address is '1 Hobbit-Hole'
- ths city is 'Bag End'
- the postal code is '111'
- 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 customer_id = 'SHIRE'

### 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 company_name, count(order_id)
FROM orders o JOIN customers c
ON o.customer_id=c.customer_id
GROUP BY 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.

### 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 company_name, count(o.order_id)
FROM orders o JOIN customers c
ON o.customer_id = c.customer_id
GROUP BY company_name
ORDER BY count(o.order_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 c.city, count(o.ship_city)
FROM orders o JOIN customers c
ON o.customer_id = c.customer_id
GROUP BY c.city
ORDER BY c.city DESC

## Data Normalization

Expand All @@ -97,19 +148,45 @@ Note: This step does not use PostgreSQL!
Take the following data and normalize it into a 3NF database.

| Person Name | Pet Name | Pet Type | Pet Name 2 | Pet Type 2 | Pet Name 3 | Pet Type 3 | Fenced Yard | City Dweller |
|-------------|----------|----------|------------|------------|------------|------------|-------------|--------------|
| ----------- | -------- | -------- | ---------- | ---------- | ---------- | ---------- | ----------- | ------------ |
| Jane | Ellie | Dog | Tiger | Cat | Toby | Turtle | No | Yes |
| Bob | Joe | Horse | | | | | No | No |
| Sam | Ginger | Dog | Miss Kitty | Cat | Bubble | Fish | Yes | No |

---

| Owner Id | Owner Name |
| :------: | :--------: |
| 1 | Jane |
| 2 | Bob |
| 3 | Sam |

| Pet Id | Pet Name | Pet Type | Owner Id |
| :----: | :--------: | :------: | :------: |
| 1 | Ellie | 1 | 1 |
| 2 | Tiger | 2 | 1 |
| 3 | Toby | 3 | 1 |
| 4 | Joe | 4 | 2 |
| 5 | Ginger | 1 | 3 |
| 6 | Miss Kitty | 2 | 3 |
| 7 | Bubble | 5 | 3 |

| Type Id | Pet Type |
| :-----: | :------: |
| 1 | Dog |
| 2 | Cat |
| 3 | Turtle |
| 4 | Horse |
| 5 | Fish |

## Stretch Goals

### delete all customers that have no orders. Should delete 17 (or 18 if you haven't deleted the record added) records.

> This is done with a DELETE query

> In the WHERE clause, you can provide another list with an IN keyword this list can be the result of another SELECT query. Write a query to return a list of CustomerIDs that meet the criteria above. Pass that to the IN keyword of the WHERE clause as the list of IDs to be deleted

> Use a LEFT JOIN to join the Orders table onto the Customers table and check for a NULL value in the OrderID column

## Create Database and Table
Expand Down