Skip to content
 
 

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

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

Introduction

Working with SQL

Instructions

Surf to SQL Try Editor at W3Schools.com
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.

Clicking the Restore Database button in the page will repopulate the database with the original data and discard all changes you have made.

find all customers that live in London. Returns 6 records.

SELECT * FROM customers WHERE city = 'London'

find all customers with postal code 1010. Returns 3 customers.

SELECT * FROM customers WHERE PostalCode = '1010'

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

SELECT phone, contactname FROM suppliers WHERE supplierID = '11'

list orders descending by the order date. The order with date 1997-02-12 should be at the top.

SELECT * FROM orders ORDER BY orderDate desc

find all suppliers who have names longer than 20 characters. You can use length(SupplierName) to get the length of the name. Returns 11 records.

SELECT * FROM suppliers WHERE length(SupplierName) > 20

find all customers that include the word "market" in the name. Should return 4 records.

SELECT * FROM customers WHERE CustomerName LIKE ('%market%')

add a customer record for "The Shire", the contact name is "Bilbo Baggins" the address is "1 Hobbit-Hole" in "Bag End", postal code "111" and the country is "Middle Earth".

INSERT INTO customers(customerName, contactName, address, city, postalCode, Country) VALUES('The Shire', 'Bilbo Baggins', '1 Hobbit Hole', 'Bag End', '111', 'Middle Earth')

update Bilbo Baggins record so that the postal code changes to "11122".

UPDATE customers SET postalcode = '11122' WHERE contactname = 'Bilbo Baggins'

list orders grouped by customer showing the number of orders per customer. Rattlesnake Canyon Grocery should have 7 orders.

SELECT count(o.customerID), c.customerName FROM orders o JOIN customers c WHERE o.customerID = c.customerid GROUP BY o.customerID

list customers names and the number of orders per customer. Sort the list by number of orders in descending order. Ernst Handel should be at the top with 10 orders followed by QUICK-Stop, Rattlesnake Canyon Grocery and Wartian Herkku with 7 orders each.

SELECT count(o.customerID), c.customerName FROM orders o JOIN customers c WHERE o.customerID = c.customerid GROUP BY o.customerID ORDER BY count(o.customerID) DESC

list orders grouped by customer's city showing number of orders per city. Returns 58 Records with Aachen showing 2 orders and Albuquerque showing 7 orders.

SELECT count(o.customerID), c.city FROM orders o JOIN customers c WHERE o.customerID = c.customerid GROUP BY c.city ORDER BY count(o.customerID) DESC

Stretch Goals

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

DELETE FROM customers WHERE customerid IN (SELECT c.customerid FROM customers c LEFT JOIN orders o ON o.CustomerID = c.CustomerID
WHERE o.orderid IS NULL)

Create Database and Table

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

CREATE TABLE public.accounts ( id integer NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ), name text COLLATE pg_catalog."default" NOT NULL, budget integer, CONSTRAINT "unique" UNIQUE (name, id)

) WITH ( OIDS = FALSE ) TABLESPACE pg_default;

ALTER TABLE public.accounts OWNER to postgres;

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors