GithubHelp home page GithubHelp logo

java-sql's Introduction

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

Introduction

Working with SQL

Instructions

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

  • pgAdmin data refresh

  • Select the northwind database created during the guided project.

  • Tools -> Query Tool

    • Open file northwind.sql (you used this script during the guided project)
    • Execute
  • Look under

    • northwind -> 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

    hint
    • 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

    hint
    • 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

    hint
    • 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

    hint
    • 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. Returns 11 records

    hint
    • This can be done with SELECT and WHERE clauses
    • You can use length(company_name) to get the length of the name
SELECT * FROM suppliers
WHERE length(company_name) > 20;
  • find all customers that include the word 'MARKET' in the contact title. Should return 19 records

    hint
    • 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)
SELECT * FROM customers
WHERE upper(contact_title) like '%MARKET%';
  • 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'

  • the postal code is '111'

  • the country is 'Middle Earth'

    hint
    • This can be done with the INSERT INTO clause
INSERT INTO customers
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"

    hint
    • This can be done with UPDATE and WHERE clauses
UPDATE customers
SET postal_code = '11122'
WHERE customer_id = 'SHIRE';
  • 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

    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
SELECT COUNT(o.customer_id) as numorders, c.customer_id, company_name 
FROM orders o 
JOIN customers c 
ON o.customer_id = c.customer_id 
GROUP BY c.customer_id;
  • 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

    hint
    • This can be done by adding an ORDER BY clause to the previous answer and changing the group by field
SELECT COUNT(o.customer_id) as numorders, c.customer_id, contact_name
FROM orders o 
JOIN customers c 
ON o.customer_id = c.customer_id 
GROUP BY c.customer_id
ORDER BY numorders DESC;
  • 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

    hint
    • This is very similar to the previous two queries, however, it focuses on the City rather than the Customer Names
SELECT COUNT(o.customer_id) as numorders, c.city
FROM orders o 
JOIN customers c 
ON o.customer_id = c.customer_id 
GROUP BY c.city
ORDER BY city;

Data Normalization

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

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: Person Table

Person Id Person Name Fenced Yard City Dweller
1 Jane No Yes
2 Bob No No
3 Sam Yes No

Table Name: PetPerson Table

Pet Id Person Id
1 1
2 1
3 1
4 2
5 3
6 3
7 3

Table Name: Pet Table

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

Table Name: Pet Type Table

Type Id Type
1 Dog
2 Cat
3 Turtle
4 Fish
5 Horse

Stretch Goals

  • delete all customers that have no orders. Should delete 2 (or 3 if you haven't deleted the record added) records
  • 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.
  • constraints

    • the id should be the primary key for the table.
    • account name should be unique.
    • account budget is required.

To see the script

  • 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!

Database Script

java-sql's People

Contributors

jrmmba8314 avatar chancepayne avatar sierraog avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.