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
Working with SQL
Surf to SQL Try Editor at W3Schools.com
Clicking the Restore Database button in the page will repopulate the database with the original data and discard all changes you have made.
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.
This can be done with SELECT and WHERE clauses
ANSWER:
SELECT *
FROM customers
where city = "London"
This can be done with SELECT and WHERE clauses
ANSWER:
SELECT *
FROM customers
WHERE postalcode = 1010
This can be done with SELECT and WHERE clauses
ANSWER:
SELECT phone
FROM suppliers
WHERE supplierid = 11
This can be done with SELECT, WHERE, and ORDER BY clauses
ANSWER:
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.
This can be done with SELECT and WHERE clauses
ANSWER:
SELECT SupplierName
FROM suppliers
WHERE length(SupplierName) > 20
This can be done with SELECT and a WHERE clause using the LIKE keyword
ANSWER:
SELECT *
FROM customers
WHERE customerName LIKE '%market%'
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
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".
This can be done with the INSERT INTO clause
ANSWER:
INSERT INTO customers(customerName, contactName, address, city, postalcode, country)
VALUES ('The Shire', 'Bilbo Baggins', '1 Hobbit-Hole', 'Bag End', 111, 'Middle Earth')
This can be done with UPDATE and WHERE clauses
ANSWER:
UPDATE customers
SET postalcode = 11122
WHERE postalcode = 111
list orders grouped by customer showing the number of orders per customer. Rattlesnake Canyon Grocery should have 7 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
ANSWER:
SELECT c.CustomerName, *, COUNT(o.OrderID) AS OrderSum FROM Orders AS o
JOIN Customers c ON o.CustomerID = c.CustomerID
GROUP BY c.CustomerID
ORDER BY OrderSum DESC, c.CustomerName
LIMIT 5;
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.
This can be done by adding an ORDER BY clause to the previous answer
ANSWER:
SELECT c.CustomerName, COUNT(o.OrderID) AS OrderSum FROM Orders AS o
JOIN Customers c ON o.CustomerID = c.CustomerID
GROUP BY c.CustomerID
ORDER BY OrderSum DESC, c.CustomerName
LIMIT 4;
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.
This is very similar to the previous two queries, however, it focuses on the City rather than the CustomerName
ANSWER:
SELECT c.City, COUNT(o.OrderID)
AS OrderSum FROM Orders AS o
JOIN Customers c ON o.CustomerID = c.CustomerID
GROUP BY c.City
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 |
Persons table
| Person ID | Person Name | Fenced Yar | CityDweller |
|---|---|---|---|
| 01 | Ellie | No | yes |
| 02 | Joe | No | no |
| 03 | Ginger | Yes | no |
| Person ID | Pet ID | Pet Name | Pet Type |
|---|---|---|---|
| 01 | 010 | Ellie | Dog |
| 02 | 020 | Joe | Horse |
| 03 | 030 | Ginger | Dog |
| Person ID | Pet ID | Pet Name 2 | Pet Type 2 |
|---|---|---|---|
| 01 | 040 | Tiger | Cat |
| 03 | 050 | Miss Kitty | Cat |
| Person ID | Pet ID | Pet Name 3 | Pet Type 3 |
|---|---|---|---|
| 01 | 060 | Toby | Turtle |
| 03 | 070 | Bubble | Fish |
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
-
use pgAdmin to create a database, naming it
budget. -
add an
accountstable 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.budgetnumeric value.
-
constraints
- the
idshould be the primary key for the table. - account
nameshould be unique. - account
budgetis required.
- the