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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,61 @@ Answer the following data queries. Keep track of the SQL you write by pasting it
### **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 * FROM Suppliers
WHERE phone = "(010) 9984510";

### 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 LEN(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 CustomerName, Count(Orders.OrderID) as TotalOrders
FROM Customers
JOIN Orders ON Orders.CustomerID = Customers.CustomerId
GROUP BY Customers.CustomerID
ORDER BY CustomerName;

### 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 CustomerName, COUNT(O.OrderID) as TotalOrders
FROM Orders as O
JOIN Customers C ON O.CustomerID = C.CustomerID
GROUP BY C.CustomerID
ORDER BY TotalOrders 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 City, COUNT(City) as Total_Orders
FROM Customers
JOIN Orders ON Orders.CustomerID = Customers.CustomerId
GROUP BY City ORDER BY Total_Orders desc;


### 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 not in (select CustomerID from Orders);

## Create Database and Table

Expand All @@ -49,3 +82,7 @@ Answer the following data queries. Keep track of the SQL you write by pasting it
- account `name` should be unique.
- account `budget` is required.


CREATE TABLE accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name STRING NOT NULL UNIQUE, budget NUMERIC NOT NULL
);