-
DB files
- restaurant.db
- library.db
-
We will be using
DB Browser for SQLite
-
Email template: To: Steffie Kleis At: [email protected]
"Hello Steffie- We'd love to see you at the five year anniversary for Nadia's Garden..."
-
Challenge
- Generate a list of customer information.
- First Name
- Last Name
- Alphabetical Order on Last Name
- Generate a list of customer information.
-
Solution:
SELECT FirstName, LastName, Email FROM Customers ORDER BY LastName;
-
Email template:
To: Steffie Kleis At: [email protected]
"Hi- I'd love to be there! There will be 4 of us attending."
-
Challenge:
-
Create a table to store response info.
- CustomerID
- PartySize
-
Just focus on creating the table.
-
-
Solution:
CREATE TABLE AnniversaryAttendees ("CustomerID" INT, "PartySize" INT);
-
Challenge
-
Create three menus
- All items sorted by price, low to high
- Appetizers and beverages, by type
- All items except beverage, by type
-
-
Solution:
SELECT * FROM Dishes ORDER BY Price;
SELECT * FROM Dishes WHERE Type="Appetizer" OR Type="Beverage" ORDER BY Type;
SELECT * FROM Dishes WHERE Type != "Beverage" ORDER BY Type;
-
Challenge:
-
Add customer information into our database
-
INSERT
-
Our table already creates new unique Customer IDs
-
Customers
- FirstName
- LastName
- Address
- City
- State
- Phone
- Birthday
-
-
Solution:
INSERT INTO Customers (FirstName, LastName, Email, Address, City, State, Phone, Birthday) VALUES ("Anna", "Smith", "[email protected]", "47u9 Lapis Dr.", "Memphis", "TN", "(555) 555-1212", "1973-07-21");
-
Email tempalte:
Talyor Jenkins Old: 27170 6th Ave., Washington, DC New: 74 Pine St., New York, NY
-
UPDATE
-
Challenge:
- Update the customer's address.
-
Solution:
SELECT CustomerID, FirstName, LastName, Address FROM Custoemrs WHERE FirstName = "Taylor" AND LastName = "Jenkins";
UPDATE Customers SET Address="74 Pine St.", City="New York", State="NY" WHERE CustomerID="26"
-
Email template: To: [email protected] From: [email protected] Subject: I'm moving! :(
Body: Hi! I've loved your restaurant for years, but I'm moving out of the area and would like to be removed from your email list. Thanks for all the great meals and memories! I'll miss the Salmon Caesar Salad the most!
Sincerely, Taylor Jenkins 954-555-7424
-
DELETE
-
Challenge:
- Delete this customer form the Customers table.
-
Solution:
SELECT * FROM Customers WHERE FirstName="Taylor" AND LastName="Jenkins";
DELETE FROM Customers WHERE CustomerID="4";
-
Email template:
To: [email protected] From: [email protected] Subject: I'm moving!:(
Body: Hi! I was so happy to hear it's already been five years! How time flies!
I plan to attend the celebration, and I'll bring three friends, too!
Best, Asher Tapley
-
Approach:
- Manually look up a customer in the Customers table.
- Write a statement that will look them up for us.
-
Challenge:
- Using the customer's email address, find their ID and enter their party size into the AnniversaryAttendees table.
-
Solution:
INSERT INTO AnniversaryAttendees (CustomerID, PartySize) VALUES ((SELECT CustomerID FROM Customers WHERE Email="[email protected]"), "4");
-
SEARCH FOR: STE??????N STEVENSON STEPHENSON STEVENSEN STEPHENSEN
-
Literal match
SELECT ... WHERE FirstName='Stevenson' OR FirstName='Stephenson' OR ...
- There's a better way...
-
Challenge:
- Search for a reservation by name, and look for similarity.
-
Schema Reminder
-
RESERVATIONS
- ReservationID
- CustomerID
- Date
-
CUSTOMERS
- CustomerID
- FirstName
- LastName
-
-
Solution:
SELECT Customers.FirstName, Customers.LastName, Reservations.Date, Reservations.PartySize FROM Reservations JOIN Customers ON Customers.CustomerID = Reservations.CustomerID WHERE Customers.LastName LIKE "Ste%"
-
Challenge:
-
Create a Reservation
- Sam McAdams - 14 July 2020, 6PM (5 people)
- [email protected] / (555) 555-1212
-
Don't worry about available times for capacity.
-
-
Solution:
SELECT * FROM Customers WHRE Email="[email protected]"
INSERT INTO Customers (FirstName, LastName, Email, Phone) VALUES ("Sam", "McAdams", '[email protected]", "(555) 555-1232");
INSERT INTO Reservations (CustomerID, Date, PartySize) VALUES ("102", "2020-06-14 18:00:00", "5");
SELECT Customers.FirstName, Customers.LastName, Customers.Email, Reservations.Date, Reservations.PartySize FROM Customers JOIN Reservations ON Customers.CustomerID=Reservations.CustomerID WHERE Customers.Email="[email protected]";
-
Email template: For: Loretta Hundey, 6939 Elka Place House Salad Mini Cheeseburgers Tropical Blue Smoothie
-
Schema Reminder:
- Customers
- CustomerID|FirstName|LastName|...
- Dishes
- DishID|Name|Price
- Orders
- OrderID|CustomerID|Order Date
- OrdersDishes
- OrderID|DishID
- Customers
-
Challenge:
- Create an order
- Find the customer
- Create the order record
- Add items to the order
- Find the total cost
-
Solution:
SELECT CustomerID, FirstName, LastName, Phone FROM Customers WHERE Address="6939 Elka Place" AND LastName="Hundey";
INSERT INTO Orders (CustomerID, OrderDate) VALUES (70, "2020-03-20 14:00:00");
SELECT * FROM Orders WHERE CustomerID="70" ORDER BY OrderDate DESC;
INSERT INTO OrdersDishes (OrderID, DishID) VALUES ("1001", (SELECT DishID FROM Dishes WHERE Name="House Salad")), ("1001", (SELECT DishID FROM Dishes WHERE Name="Mini Cheeseburgers")), ("1001", (SELECT DishID FROM Dishes WHERE Name="Tropical Blue Smoothie"));
SELECT SUM(Dishes.Price) FROM Dishes JOIN OrdersDishes ON Dishes.DishID = OrdersDishes.DishID WHERE OrdersDishes.OrderID="1001";
-
Email template
Cleo Goldwater
, it's your birthday! To celebrate, use this coupon for 50% off aQuinoa Salmon Salad
-
Approach:
- Separate table
- Unique ID for dish
- Use that Unique ID, that foreign key, on the Customers table
-
Challenge:
- Set
Cleo Goldwater
's favorite dish toQuinoa Salmon Salad
- Set
-
Solution:
SELECT DishID FROM Dishes WHERE Name="Quinoa Salmon Salad";
SELECT * FROM Customers WHERE FirstName="Cleo" AND LastName="Goldwater";
UPDATE Customers SET FavoriteDish=(SELECT DishID FROM Dishes WHERE Name="quinoa Salmon Salad") WHERE CustomerID="42";
SELECT Customers.FirstName, Customers.LastName, Customers.FavoriteDish, Dishes.Name FROM Customers JOIN Dishes ON Customers.FavoriteDish=Dishes.DishID
-
Email template:
[Customer], you're one of our favorite customers! Enjoy this 15% off coupon for your next order!
-
Challenge:
- Generate a list of the five customers who have placed the most to-go orders.
- Number of orders
- First and last name, email
- Sorted by number of orders
-
Use both
- Customers table
- Orders table
-
Solution:
SELECT COUNT(Orders.OrderID) AS OrderCount, Customers.FirstName, Customers.LastName, Customers.Email FROM Orders JOIN Customers ON Orders.CustomerID=Customers.CustomerID GROUP BY Orders.CustomerID ORDER BY OrderCount DESC LIMIT 5;
-
Books out on loan
-
Books still in the library
-
Library Database
-
Books
- Title
- Author
- Publication year
- Barcode
-
Patrons
- FirstName
- LastName
- PatronID
-
Loans
- LoanID
- BookID
- PatronID
- LoanDate
- DueDate
- ReturnedDate
-
-
Loans Table
- LoanID, BookID, PatronID, LoanDate, DueDate, ReturnedDate
-
Challenge:
- Find the number of available copies of Dracula.
-
Solution:
SELECT COUNT(Title) FROM Books WHERE Title="Dracula";
SELECT COUNT(Books.Title) FROM Loans JOIN Books ON Loans.BookID = Books.BookID WHERE Books.Title = "Dracula" AND Loans.ReturnedDate IS NULL
SELECT (SELECT COUNT(Books.Title) FROM Books WHERE Title="Dracula") - (SELECT COUNT(Books.Title) FROM Loans JOIN Books ON Loans.BookID = Books.BookID WHERE Books.Title = "Dracula" AND Loans.ReturnedDate IS NULL) AS AvailableBooks;
-
Challenge:
-
Add to the Books table
Title: Dracula Author: Bram Stoker Year: 1897 New ID: 4819277482
Title: Gulliver's Travles Author: Jonathan Swift Year: 1729 New ID: 4899254401
-
-
Solution:
INSERT INTO Books (Title, Author, Published, Barcode) VALUES ("Dracula", "Bram Stoker", "1897", "4819277482"), ("Gulliver's Travels", "Jonathan Swift", "1729", "4899254401");
-
Loans
- LoanID (Auto-generated)
- BookID (FK from Books table)
- PatronID (FK from Patrons table)
- LoanDate
- DueDate
- ReturnedDate
-
Books
- BookID
- ...
- Barcode
-
Challenge:
-
Check out these books to this customer.
The Picture of Dorian Gray, 2855934983 Great Expectations, 4043822646
Jack Vaan, [email protected]
August 25, 2020
September 8, 2020
-
-
Solution:
INSERT INTO Loans (BookID, PatronID, LoanDate, DueDate) VALUES ( (SELECT BookID FROM Books WHERE Barcode="2855934983"), (SELECT PatronID FROM Patrons WHERE Email="[email protected]"), "2020-08-25", "2020-09-08" ); INSERT INTO Loans (BookID, PatronID, LoanDate, DueDate) VALUES ( (SELECT BookID FROM Books WHERE Barcode="4043822646"), (SELECT PatronID FROM Patrons WHERE Email="[email protected]"), "2020-08-25", "2020-09-08" );
-
Confirm:
SELECT * FROM Loans JOIN Books ON Loans.BookID = Books.BookID WHERE PatronID=(SELECT PatronID FROM Patrons WHERE Email="[email protected]")
-
-
Challenge:
- Generate a report of books due back on July 13, 2020, with patron contact information.
- Tables to use:
- Loans
- Patrons
- Books
-
Solution:
SELECT Loans.DueDate, Books.Title, Patrons.Email, Patrons.FirstName FROM Loans JOIN Books ON Loans.BookID = Books.BookID JOIN Patrons ON Loans.PatronID = Patrons.PatronID WHERE Loans.DueDate = "2020-07-13" AND Loans.ReturnedDate IS NULL;
-
Challenge:
- Return these books to the Library on July 5, 2020.
-
Solution:
UPDATE Loans SET ReturnedDate = "2020-07-05" WHERE BookId = (SELECT BookID FROM Books WHERE Barcode = "6435968624") AND ReturnedDate IS NULL;
-
Challenge:
- Create a report showing the 10 patrons who have checked out the fewest books.
-
Solution:
SELECT COUNT(Loans.LoanID) AS loanCount, Patrons.FirstName, Patrons.Email FROM Loans JOIN Patrons ON Loans.PatronID = Patrons.PatronID GROUP BY Loans.PatronID ORDER BY loancount ASC LIMIT 10;
-
Challenge:
- Create a list of books from the 1890s that are currently available.
-
Solution:
SELECT Books.Title, Books.BookID, Books.Author, Books.Published FROM Books JOIN Loans ON Books.BookID = Loans.BookID WHERE Published > 1889 AND Published < 1900 AND Loans.ReturnedDate IS NOT NULL GROUP BY Books.BookID ORDER BY Books.Title;
-
Challenge
-
- Create a report showing how many books were published each year.
-
- Create a report showing the five most popular books to check out.
-
-
Solution:
SELECT Published, COUNT(DISTINCT(Title)) AS pubcount FROM Books GROUP BY Published ORDER BY pubcount DESC;
SELECT COUNT(Loans.LoanID) AS loancount, Books.Title FROM Loans JOIN Books ON Loans.BookID = Books.BookID GROUP BY Books.Title ORDER BY loancount DESC LIMIT 5;