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
292 changes: 105 additions & 187 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,266 +1,184 @@
# Java SQL
# 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 database using PostgreSQL
* Create a new datadaase using PostgreSQL

## Introduction
# Introduction

Working with SQL

## Instructions
# Instructions

ReImport the Northwind database into PostgreSQL using pgAdmin. This is the same data we used during the guided project.

Reimport the Northwind database into PostgreSQL using pgAdmin. This is the same data we used during the guided project.
clone https://github.com/pthom/northwind_psql.git

* [ ] ***pgAdmin data refresh***
## pgAdmin

* Select the northwind database created during the guided project.
* Right Click Databases
* Create
* type in northwind2

* Tools -> Query Tool
* Open file northwind.sql (you used this script during the guided project)
* Open file northwind.sql (from cloned repo)
* Execute

* Look under
* northwind -> Schemas -> public -> tables
* 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 in the provided SQL code block. You will be submitting that through the regular fork, change, pull process

* [ ] ***find all customers that live in London. Returns 6 records***

<details><summary>hint</summary>

* This can be done with SELECT and WHERE clauses
</details>

```SQL

```

* [ ] ***find all customers with postal code 1010. Returns 3 customers***

<details><summary>hint</summary>

* This can be done with SELECT and WHERE clauses
</details>

```SQL

```

* [ ] ***find the phone number for the supplier with the id 11. Should be (010) 9984510***

<details><summary>hint</summary>

* This can be done with SELECT and WHERE clauses
</details>
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.

```SQL

```
### 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';

* [ ] ***list orders descending by the order date. The order with date 1998-05-06 should be at the top***
"customer_id","company_name","contact_name","contact_title","address","city","region","postal_code","country","phone","fax"
"AROUT","Around the Horn","Thomas Hardy","Sales Representative","120 Hanover Sq.","London",NULL,"WA1 1DP","UK","(171) 555-7788","(171) 555-6750"
"BSBEV","B's Beverages","Victoria Ashworth","Sales Representative","Fauntleroy Circus","London",NULL,"EC2 5NT","UK","(171) 555-1212",NULL
"CONSH","Consolidated Holdings","Elizabeth Brown","Sales Representative","Berkeley Gardens 12 Brewery","London",NULL,"WX1 6LT","UK","(171) 555-2282","(171) 555-9199"
"EASTC","Eastern Connection","Ann Devon","Sales Agent","35 King George","London",NULL,"WX3 6FW","UK","(171) 555-0297","(171) 555-3373"
"NORTS","North/South","Simon Crowther","Sales Associate","South House 300 Queensbridge","London",NULL,"SW7 1RZ","UK","(171) 555-7733","(171) 555-2530"
"SEVES","Seven Seas Imports","Hari Kumar","Sales Manager","90 Wadhurst Rd.","London",NULL,"OX15 4NB","UK","(171) 555-1717","(171) 555-5646"

<details><summary>hint</summary>

* This can be done with SELECT, WHERE, and ORDER BY clauses
</details>
### find all customers with postal code 1010. Returns 3 customers.
> This can be done with SELECT and WHERE clauses

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

* [ ] ***find all suppliers who have names longer than 20 characters. Returns 11 records***
SELECT Phone FROM Suppliers WHERE Supplier_Id = 11;

<details><summary>hint</summary>
### 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

* This can be done with SELECT and WHERE clauses
* You can use `length(company_name)` to get the length of the name
</details>
SELECT * FROM Orders ORDER BY Order_Date DESC;

```SQL
### 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 supplier_id, company_name, city
from suppliers
where length(company_name) > 20

* [ ] ***find all customers that include the word 'MARKET' in the contact title. Should return 19 records***
### 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

<details><summary>hint</summary>
> 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

* 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 insensitive comparing so upper(contact_title)
</details>
> 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***
### 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'
* the city is 'Bag End'
* ths city is 'Bag End'
* the postal code is '111'
* the country is 'Middle Earth'
<details><summary>hint</summary>
> This can be done with the INSERT INTO clause

* This can be done with the INSERT INTO clause
</details>
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')

```SQL

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

* [ ] ***update _Bilbo Baggins_ record so that the postal code changes to _"11122"_***
update customers
set postal_code = '11122'
where customer_id = 'SHIRE'

<details><summary>hint</summary>

* This can be done with UPDATE and WHERE clauses
</details>
### 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

```SQL
> There is more information about the COUNT clause on [W3 Schools](https://www.w3schools.com/sql/sql_count_avg_sum.asp)

```
SELECT count(o.order_id), c.contact_name, c.company_name
from orders o join customers c
on o.customer_id = c.customer_id
group by c.contact_name, c.company_name
order by count(o.order_id)

* [ ] ***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***

<details><summary>hint</summary>
### 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

* 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)
</details>
SELECT count(o.order_id), c.contact_name, c.company_name
from orders o join customers c
on o.customer_id = c.customer_id
group by c.contact_name, c.company_name
order by count(o.order_id) desc

```SQL

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

* [ ] ***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 count(o.order_id), c.city
from customers c join orders o
on o.customer_id = c.customer_id
group by c.city
order by c.city

<details><summary>hint</summary>

* This can be done by adding an ORDER BY clause to the previous answer and changing the group by field
</details>

```SQL

```

* [ ] ***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***

<details><summary>hint</summary>

* This is very similar to the previous two queries, however, it focuses on the City rather than the Customer Names
</details>

```SQL

```

## Data Normalization

Note: This step does not use PostgreSQL!

* [ ] ***Take the following data and normalize it into a 3NF database***
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 |

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:

| | | | | | | | | |
|------------|------------|------------|------------|------------|------------|------------|------------|------------|
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |

Table Name:

| | | | | | | | | |
|------------|------------|------------|------------|------------|------------|------------|------------|------------|
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |

Table Name:

| | | | | | | | | |
|------------|------------|------------|------------|------------|------------|------------|------------|------------|
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |

Person Name| Fenced Yard| City Dweller| Person Id
Jane No Yes 1
Bob No No 2
Sam Yes No 3
Person Id| Pet Id| Pet Name| Pet Type|
1 1 Ellie Dog
1 2 Tiger Cat
1 3 Toby Turtle
2 4 Joe Horse
3 5 Ginger Dog
3 6 Miss Kitty Cat
3 7 Bubble Fish
---
## Stretch Goals

### Stretch Goals

* [ ] ***delete all customers that have no orders. Should delete 2 (or 3 if you haven't deleted the record added) records***

```SQL

```

* [ ] ***Create Database and Table: After creating the database, tables, columns, and constraint, generate the script necessary to recreate the database. This script is what you will submit for review***

* use pgAdmin to create a database, naming it `budget`.
* add an `accounts` table with the following _schema_:

* `id`, numeric value with no decimal places that should autoincrement.
* `name`, string, add whatever is necessary to make searching by name faster.
* `budget` numeric value.
### 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

* constraints
* the `id` should be the primary key for the table.
* account `name` should be unique.
* account `budget` is required.
> 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

```SQL
## Create Database and Table

```
### Keep track of the code you write and paste at the end of this document

To see the script
- use pgAdmin to create a database, naming it `budget`.
- add an `accounts` table with the following _schema_:

* Right Click on the database name
* Select Backup...
* Set a filename
* To put the file backup.sql in your home directory, you could use `backup.sql`
* Set format to `Plain`
* The script you want is now in the text file named above.
* Copy the script from the text file into the SQL code block above!
- `id`, numeric value with no decimal places that should autoincrement.
- `name`, string, add whatever is necessary to make searching by name faster.
- `budget` numeric value.

![Database Script](assets/jx-12-m3-script.gif)
- constraints
- the `id` should be the primary key for the table.
- account `name` should be unique.
- account `budget` is required.