diff --git a/.vscode/settings.json b/.vscode/settings.json index 23698105..e44b35fc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,7 @@ "editor.cursorBlinking": "solid", "editor.fontFamily": "ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono', 'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro', 'Fira Mono', 'Droid Sans Mono', 'Courier New', monospace", "editor.fontLigatures": false, - "editor.fontSize": 22, + "editor.fontSize": 16, "editor.formatOnPaste": true, "editor.formatOnSave": true, "editor.lineNumbers": "on", @@ -17,8 +17,7 @@ "files.autoSave": "afterDelay", "screencastMode.onlyKeyboardShortcuts": true, "terminal.integrated.fontSize": 18, - "workbench.activityBar.visible": true, "workbench.colorTheme": "Visual Studio Dark", "workbench.fontAliasing": "antialiased", "workbench.statusBar.visible": true -} +} \ No newline at end of file diff --git a/01_restaurant/challenges/01_01_invitations_ch.sql b/01_restaurant/challenges/01_01_invitations_ch.sql index c83188eb..13e82c22 100644 --- a/01_restaurant/challenges/01_01_invitations_ch.sql +++ b/01_restaurant/challenges/01_01_invitations_ch.sql @@ -1,4 +1,19 @@ --- Generate a list of customer information. +-- Generate a list -- yes a list!--of customer information. -- Show their first name, last name, and email address. --- Sort the list of results by last name. \ No newline at end of file +-- Sort the list of results by last name. +SELECT + FirstName, + LastName, + Email + +FROM + Customers + + ORDER BY + LastName + +CREATE TABLE AnniversaryPartyAttendess ) + CustomerID INTEGER, + PartySize INTEGER + ); \ No newline at end of file diff --git a/01_restaurant/challenges/01_02_anniversary_ch.sql b/01_restaurant/challenges/01_02_anniversary_ch.sql index d68138bb..80ff6213 100644 --- a/01_restaurant/challenges/01_02_anniversary_ch.sql +++ b/01_restaurant/challenges/01_02_anniversary_ch.sql @@ -4,4 +4,7 @@ -- Associate a customer's ID number with the number of people -- they plan to bring in their party. --- Hint: SQLite offers the INTEGER and REAL datatypes \ No newline at end of file +-- Hint: SQLite offers the INTEGER and REAL datatypes +CREATE TABLE AnniversaryParty ( + CustomerID INTEGER, + Party INTEGER); diff --git a/01_restaurant/challenges/01_03_printmenus_ch.sql b/01_restaurant/challenges/01_03_printmenus_ch.sql index cd202d34..0be0017a 100644 --- a/01_restaurant/challenges/01_03_printmenus_ch.sql +++ b/01_restaurant/challenges/01_03_printmenus_ch.sql @@ -2,4 +2,18 @@ -- Create a report with all the items sorted by price (lowest to highest). -- Create a report showing appetizers and beverages. --- Create a report with all items except beverages. \ No newline at end of file +-- Create a report with all items except beverages. + +/*Types: Appetizer, Main, Dessert, Beverage +*/ + +SELECT + Name, + Type, + Price +FROM + Dishes +WHERE + Type IN ('Appetizer', 'Beverage'); +ORDER BY + Type; \ No newline at end of file diff --git a/01_restaurant/challenges/01_04_newcustomer_ch.sql b/01_restaurant/challenges/01_04_newcustomer_ch.sql index f47fb847..ac5f557c 100644 --- a/01_restaurant/challenges/01_04_newcustomer_ch.sql +++ b/01_restaurant/challenges/01_04_newcustomer_ch.sql @@ -3,4 +3,18 @@ -- Use the following information to create a record: -- Anna Smith (asmith@samoca.org) -- 479 Lapis Dr., Memphis, TN --- Phone: (555) 555-1212; Birthday: July 21, 1973 \ No newline at end of file +-- Phone: (555) 555-1212; Birthday: July 21, 1973 + +INSERT INTO +Customers (FirstName, LastName, Email, Address, City, State, Phone, Birthday) +VALUES + ('Anna', 'Smith', 'asmith@samoca.org', '479 Lapis Dr.', 'Memphis', 'TN', '(555) 555-1212', + '1973-07-21'); + + SELECT * FROM Customers ORDER BY CustomerID DESC; + +DELETE +FROM + Customers +WHERE + CustomerID = 101; \ No newline at end of file diff --git a/01_restaurant/challenges/01_05_updateinfo_ch.sql b/01_restaurant/challenges/01_05_updateinfo_ch.sql index ad034f88..2f7ddbf0 100644 --- a/01_restaurant/challenges/01_05_updateinfo_ch.sql +++ b/01_restaurant/challenges/01_05_updateinfo_ch.sql @@ -1,4 +1,25 @@ -- Update a customer's contact information. -- Taylor Jenkins, of 27170 6th Ave., Washington, DC, --- has moved to 74 Pine St., New York, NY. \ No newline at end of file +-- has moved to 74 Pine St., New York, NY. + +SELECT + CustomerID, + FirstName, + LastName, + Address +FROM + Customers +WHERE + FirstName = 'Taylor' +AND + LastName = 'Jenkins'; + +UPDATE + Customers +SET + Address = '74 Pine St.', City = 'New York', State = 'NY' +WHERE + CustomerID = 26; + +Select * FROM Customers WHERE CustomerID = 26; \ No newline at end of file diff --git a/01_restaurant/challenges/01_06_removeinfo_ch.sql b/01_restaurant/challenges/01_06_removeinfo_ch.sql index 56d912f9..3b4c32a9 100644 --- a/01_restaurant/challenges/01_06_removeinfo_ch.sql +++ b/01_restaurant/challenges/01_06_removeinfo_ch.sql @@ -2,4 +2,51 @@ -- A customer named Norby has notified us he won't -- be able to keep his Friday reservation. --- Today is July 24, 2022. \ No newline at end of file +-- Today is July 24, 2022. + +-- Find his customer ID, +-- locate the date, get Reservation ID +-- remove the for where Reserveration ID = blahblahblah + +SELECT + LastName, + FirstName, + CustomerID +FROM + Customers +WHERE + LastName = 'Norby' + OR + FirstName = 'Norby'; + +SELECT + ReservationID, + Date(Date) as Day + +FROM + Reservations +WHERE + CustomerID = 64 + AND + Day > '2022-07-24'; + +SELECT + * +FROM + Reservations r +INNER JOIN + Customers c +ON + c.CustomerID = r.CustomerID +WHERE + (c.LastName = 'Norby' + OR + c.FirstName = 'Norby') +AND + r.Date > '2022-07-22'; + +DELETE +FROM + Reservations +WHERE + ReservationID = 2000; \ No newline at end of file diff --git a/01_restaurant/challenges/01_07_attendevent_ch.sql b/01_restaurant/challenges/01_07_attendevent_ch.sql index 11a66a24..4fb87c3d 100644 --- a/01_restaurant/challenges/01_07_attendevent_ch.sql +++ b/01_restaurant/challenges/01_07_attendevent_ch.sql @@ -1,4 +1,18 @@ -- Register a customer for our Anniversary event. -- The customer 'atapley2j@kinetecoinc.com' will be in --- attendance, and will bring 3 friends. \ No newline at end of file +-- attendance, and will bring 3 friends. + +INSERT INTO + AnniversaryParty (CustomerID, Party) +VALUES ( + (SELECT + c.CustomerID + FROM + Customers c + WHERE + c.Email = 'atapley2j@kinetecoinc.com'), + 8); +Select * FROM AnniversaryParty; + + \ No newline at end of file diff --git a/01_restaurant/challenges/01_08_findreservation_ch.sql b/01_restaurant/challenges/01_08_findreservation_ch.sql index 13f598ab..8b6f767f 100644 --- a/01_restaurant/challenges/01_08_findreservation_ch.sql +++ b/01_restaurant/challenges/01_08_findreservation_ch.sql @@ -4,4 +4,23 @@ -- Variations of the name include: -- Stevensen, Stephensen, Stevenson, Stephenson, Stuyvesant --- There are four people in the party. Today is June 14th. \ No newline at end of file +-- There are four people in the party. Today is June 14th. + +SELECT + r.ReservationID, + c.FirstName||' '||c.LastName as [Reservation Name], + r.PartySize, + r.Date + +FROM + Reservations r +INNER JOIN + Customers c +ON + r.CustomerID = c.CustomerID + +WHERE + c.LastName LIKE 'St%' +AND + r.Date LIKE '%06-14%' +; \ No newline at end of file diff --git a/01_restaurant/challenges/01_09_createreservation_ch.sql b/01_restaurant/challenges/01_09_createreservation_ch.sql index 49ee4666..c94b061c 100644 --- a/01_restaurant/challenges/01_09_createreservation_ch.sql +++ b/01_restaurant/challenges/01_09_createreservation_ch.sql @@ -3,4 +3,159 @@ -- Use the following information: -- Sam McAdams (smac@kinetecoinc.com), for 5 people --- on August 12, 2022 at 6PM (18:00) \ No newline at end of file +-- on August 12, 2022 at 6PM (18:00) + + +/* +Holy fuck stick. I tried over and over to get this going in one statement, but could never get it to work. +The instructor did it in 3 separate statements, which was SO MUCH EASIER!! I could have done that. + +Here: +*/ + +SELECT + CustomerID +FROM + Customers +WHERE + Email = 'smac@kinetecoinc.com'; +/* no answer, because it doesn't exist yet. +So add him to Customers */ + +INSERT INTO + Customers (FirstName, LastName, Email) + VALUES ('Sam', 'McAdams', 'smac@kinetecoinc.com'); + +SELECT * +FROM Customers +ORDER BY CustomerID Desc +LIMIT 5; +/* Now customer exists. ID 102... +So add his reservation */ + +INSERT INTO + Reservations (CustomerID, Date, PartySize) +VALUES (102, '2022-08-12 18:00:00',5); + +SELECT * +FROM + Reservations +ORDER BY + ReservationID DESC LIMIT 5; + +-- ----------------------------- + +INSERT INTO + Customers (FirstName, LastName, Email) + Values ('Sam', 'McAdams', 'smac@kinetecoinc.com') +-- test for not existing: +WHERE NOT EXISTS ( + SELECT 1 + FROM + Customers + WHERE + Email = 'smac@kinetecoinc.com' +) +INSERT INTO + Reservations (CustomerID, Date, PartySize) + VALUES( + (SELECT + c.CustomerID + FROM + Customers c + WHERE + c.Email = 'smac@kinetecoinc.com'), '2022-08-12 18:00:00',5); + +SELECT * FROM Customers ORDER BY CustomerID DESC LIMIT 10; + +SELECT * From Reservations ORDER BY Date Desc Limit 10; + + + +-- ------------------------------- +IF EXISTS + (SELECT 1 + FROM + Customers c + INNER JOIN + Reservations r + ON + c.CustomerID = r.CustomerID + WHERE + c.Email = 'smac@kinetecoinc.com'); + + +-- --------------------------------------------------- + + +IF EXISTS + (SELECT + c.CustomerID + FROM + Customers c + INNER JOIN + Reservations r + ON + c.CustomerID = r.CustomerID + WHERE + c.Email = 'smac@kinetecoinc.com') +BEGIN +-- if customer already exists, add them to the reservations + INSERT INTO + Reservations (CustomerID, Date, PartySize) + VALUES (c.CustomerID, '2022-08-12 18:00:00',5) +END +ELSE +-- If customer does not exist, create customer, then add reservation + INSERT INTO + Customers (FirstName, LastName, Email) + VALUES ('Sam','McAdams','smac@kinetecoinc.com') +-- now add reservation for new customer + BEGIN + INSERT INTO + Reservations (CustomerID, Date, PartySize) + + VALUES( + (SELECT + c.CustomerID + FROM + Customers c + INNER JOIN + Reservations r + ON + c.CustomerID = r.CustomerID + WHERE + c.Email = 'smac@kinetecoinc.com'), + '2022-08-12 18:00:00',5) + END; + +SELECT * FROM Customers ORDER BY CustomerID DESC LIMIT 10; + +SELECT * From Reservations ORDER BY Date Desc Limit 10; + + + (SELECT + c.CustomerID + FROM + Customers c + WHERE + c.Email = 'smac@kinetecoinc.com') + + +INSERT INTO + Reservations (CustomerID, Date, PartySize) + +VALUES( +(SELECT + c.CustomerID +FROM + Customers c +INNER JOIN + Reservations r +ON + c.CustomerID = r.CustomerID +WHERE + c.Email = 'smac@kinetecoinc.com'), + '2022-08-12 18:00:00',5); + +Select * FROM Reservations ORDER BY Date DESC; \ No newline at end of file diff --git a/01_restaurant/challenges/01_10_takeorder_ch.sql b/01_restaurant/challenges/01_10_takeorder_ch.sql index 54ed21e3..89c901e0 100644 --- a/01_restaurant/challenges/01_10_takeorder_ch.sql +++ b/01_restaurant/challenges/01_10_takeorder_ch.sql @@ -6,4 +6,76 @@ -- Items: 1 House Salad, 1 Mini Cheeseburgers, and -- 1 Tropical Blue Smoothie -- Delivery date and time: September 20, 2022 @ 2PM (14:00) --- There are no taxes or other fees. \ No newline at end of file +-- There are no taxes or other fees. + +/* +Find the customer - search in Customers, get ID +Create order record - add CustomerID and Order Date ('2022-09-22 14:00:00') to Orders table + - Get Order ID +From the Dishes table - Get Dish IDs +Add items to order - Add OrderID (multiple time) and DishIDs to OrdersDishes +Find total cost - sum the Prices from the Dishes table +*/ + +SELECT + CustomerID +FROM Customers +WHERE + FirstName = 'Loretta' + AND LastName = 'Hundey'; + +INSERT INTO + Orders (CustomerID, OrderDate) + VALUES ( + (SELECT + CustomerID +FROM Customers +WHERE + FirstName = 'Loretta' + AND LastName = 'Hundey'), '2022-09-22 14:00:00'); + +SELECT * FROM Orders ORDER BY OrderID DESC; +-- Success! OrderID is 1001 + +-- Items: 1 House Salad, 1 Mini Cheeseburgers, and +-- 1 Tropical Blue Smoothie +SELECT + DishID +FROM + Dishes +WHERE + Name = 'House Salad' OR -- 4 + Name = 'Mini Cheeseburgers' OR -- 7 + Name = 'Tropical Blue Smoothie'; -- 20 + +INSERT INTO + OrdersDishes (OrderID, DishID) +VALUES(1001,4),(1001,7),(1001,20); + +-- ------- ** integrated way to combine the above 2 statements -------------- +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')); + +-- -------- ** end integration ---------------------------------------------- + + +SELECT * FROM OrdersDishes ORDER BY OrdersDishesID Desc LIMIT 5; +-- Cool! orders added successfully to order +-- now get the cost + +SELECT + SUM (d.Price) +FROM + Dishes d +INNER JOIN + OrdersDishes od +ON + d.DishID = od.DishID +WHERE + od.OrderID = 1001; + +-- total cost is $21.00 \ No newline at end of file diff --git a/01_restaurant/challenges/01_11_trackfavorites_ch.sql b/01_restaurant/challenges/01_11_trackfavorites_ch.sql index 149d18a7..244f036f 100644 --- a/01_restaurant/challenges/01_11_trackfavorites_ch.sql +++ b/01_restaurant/challenges/01_11_trackfavorites_ch.sql @@ -1,4 +1,26 @@ -- Update information in the database. -- Set Cleo Goldwater's favorite dish to --- the Quinoa Salmon Salad. \ No newline at end of file +-- the Quinoa Salmon Salad. + +-- Experiment: +SELECT + CustomerID +FROM + Customers c +WHERE + c.FistName = Cleo AND + c.LastName = Goldwater; +-- she is customerID 42 + + +-- My trial: +UPDATE + Customers +SET + FavoriteDish = (SELECT DishID FROM Dishes WHERE Name = 'Quinoa Salmon Salad') +WHERE + CustomerID = (SELECT CustomerID FROM Customers c WHERE c.FirstName = 'Cleo' AND c.LastName = 'Goldwater'); + +-- confirm: +SELECT * FROM Customers WHERE CustomerID = 42; \ No newline at end of file diff --git a/01_restaurant/challenges/01_12_bestcustomers_ch.sql b/01_restaurant/challenges/01_12_bestcustomers_ch.sql index 7325900b..3cf4feda 100644 --- a/01_restaurant/challenges/01_12_bestcustomers_ch.sql +++ b/01_restaurant/challenges/01_12_bestcustomers_ch.sql @@ -1,3 +1,20 @@ -- Identify a few customers who have ordered delivery -- from the restaurant the most often, so we can send --- them a promotional coupon. \ No newline at end of file +-- them a promotional coupon. + +SELECT + o.CustomerID, + c.FirstName||' '||c.LastName as [Customer Name], + c.Email, + Count(o.OrderID) AS [Number of Orders] +FROM + Orders o +INNER JOIN + Customers c +ON + o.CustomerID = c.CustomerID +GROUP BY + o.CustomerID +ORDER BY + [Number of Orders] DESC +LIMIT 10; \ No newline at end of file diff --git a/01_restaurant/restaurant.db b/01_restaurant/restaurant.db index 07e2cd1a..c3b18ad3 100644 Binary files a/01_restaurant/restaurant.db and b/01_restaurant/restaurant.db differ diff --git a/02_library/challenges/02_01_checkavailability_ch.sql b/02_library/challenges/02_01_checkavailability_ch.sql index 8af48cca..a9554ec1 100644 --- a/02_library/challenges/02_01_checkavailability_ch.sql +++ b/02_library/challenges/02_01_checkavailability_ch.sql @@ -1,2 +1,43 @@ -- Determine how many copies of the book 'Dracula' --- are available for library patrons to borrow. \ No newline at end of file +-- are available for library patrons to borrow. + +SELECT + BookID +FROM + Books +WHERE + Title = 'Dracula'; +-- IDs are 12, 60, 73 + +-- ---------- ** First trial ----------- +SELECT * +FROM + Loans +WHERE + BookID IN (12, 60, 73) +ORDER BY + BookID + ; +-- ------- ** My completed steps ---------- +SELECT + LoanID, + b.BookID, + b.Title, + b.Author, + PatronID, + MAX(LoanDate) [Loan Date], + DueDate, + ReturnedDate +FROM + Loans l +INNER JOIN + Books b +ON + b.BookID = l.BookID +WHERE + l.BookID IN (SELECT BookID FROM Books WHERE Title = 'The Scarlet Letter') +GROUP BY + l.BookID; + + + diff --git a/02_library/challenges/02_02_addbook_ch.sql b/02_library/challenges/02_02_addbook_ch.sql index 9c0334d8..3868fbfe 100644 --- a/02_library/challenges/02_02_addbook_ch.sql +++ b/02_library/challenges/02_02_addbook_ch.sql @@ -8,4 +8,25 @@ -- Title: Gulliver’s Travels into Several Remote Nations of the World -- Author: Jonathan Swift -- Published: 1729 --- ID Number: 4899254401 \ No newline at end of file +-- ID Number: 4899254401 + +SELECT + BookID, + Title, + Author, + Barcode +FROM + Books +WHERE + Title = 'Dracula' OR + Title = 'Gulliver''s Travels into Several Remote Nations of the World' +Order BY + Title ASC; + +INSERT INTO + Books (Title, Author, Published, Barcode) + VALUES + ('Dracula', 'Bram Stoker', 1897, 4819277482), + ('Gulliver''s Travels into Several Remote Nations of the World', 'Jonathan Swift', 1729, 4899254401) + ; + \ No newline at end of file diff --git a/02_library/challenges/02_03_checkout_ch.sql b/02_library/challenges/02_03_checkout_ch.sql index ca5efe93..03c144cf 100644 --- a/02_library/challenges/02_03_checkout_ch.sql +++ b/02_library/challenges/02_03_checkout_ch.sql @@ -2,4 +2,36 @@ -- Book 1: The Picture of Dorian Gray, 2855934983 -- Book 2: Great Expectations, 4043822646 -- The checkout date is August 25, 2022 and the --- due date is September 8, 2022. \ No newline at end of file +-- due date is September 8, 2022. + +-- First, the simple code -------- +SELECT + BookID, + Title +FROM + Books +WHERE + Barcode = 2855934983 OR + Barcode = 4043822646; + +SELECT PatronID FROM Patrons WHERE Email = 'jvaan@wisdompets.com'; + +-- ---------------- ** My integrated code ------------------ +INSERT INTO + Loans (BookID, PatronID, LoanDate, DueDate) + VALUES ( + (SELECT BookID FROM Books WHERE Barcode = 2855934983), + (SELECT PatronID FROM Patrons WHERE Email = 'jvaan@wisdompets.com'), + '2022-08-25', '2022-09-08' + ), + ( + (SELECT BookID FROM Books WHERE Barcode = 4043822646), + (SELECT PatronID FROM Patrons WHERE Email = 'jvaan@wisdompets.com'), + '2022-08-25', '2022-09-08' + ) + +-- -------------- Code for checking ----------------- +SELECT * +FROM Loans +ORDER BY LoanID DESC +LIMIT 5; diff --git a/02_library/challenges/02_04_booksdue_ch.sql b/02_library/challenges/02_04_booksdue_ch.sql index df5bdcb3..9a3923fd 100644 --- a/02_library/challenges/02_04_booksdue_ch.sql +++ b/02_library/challenges/02_04_booksdue_ch.sql @@ -1,4 +1,29 @@ -- Prepare a report of books due to be returned -- to the library on July 13, 2022. -- Provide the due date, the book title, and --- the borrower's first name and email address. \ No newline at end of file +-- the borrower's first name and email address. + +-- --------------** Did this integrated solution in one fell swoop this time ---------------- +SELECT + l.LoanID, + l.LoanDate, + l.DueDate, + l.ReturnedDate, + b.Title, + p.FirstName||' '||p.LastName as [Patron Name], + p.Email +FROM + Loans l +INNER JOIN + Books b +ON + l.BookID = b.BookID +INNER JOIN + Patrons p +ON + l.PatronID = p.PatronID +WHERE + DueDate <= '2022-07-13' -- I changed the operator to '<=' from '=' to see ALL overdue books. + AND ReturnedDate IS NULL +ORDER BY + l.LoanDate ASC; \ No newline at end of file diff --git a/02_library/challenges/02_05_returnbooks_ch.sql b/02_library/challenges/02_05_returnbooks_ch.sql index 8279e71c..79aba3f5 100644 --- a/02_library/challenges/02_05_returnbooks_ch.sql +++ b/02_library/challenges/02_05_returnbooks_ch.sql @@ -3,4 +3,40 @@ -- Return date: July 5, 2022 -- Book 1: 6435968624 -- Book 2: 5677520613 --- Book 3: 8730298424 \ No newline at end of file +-- Book 3: 8730298424 + +-- ---------------First is the dead simple look-up code ---- +SELECT + l.LoanID, + b.Title, + MAX(l.LoanDate), + l.DueDate, + l.ReturnedDate +FROM Loans l +INNER JOIN Books b +ON l.BookID = b.BookID +WHERE + b.Barcode = 6435968624 + OR b.Barcode = 5677520613 + OR b.Barcode = 8730298424 +GROUP BY + l.BookID +; +-- ------------------ 1-liner code for integration ------------ +-- this is the over-engineered part: +SELECT l.LoanID FROM Loans l INNER JOIN Books b ON l.BookID = b.BookID WHERE b.Barcode = 6435968624 GROUP BY l.BookID + +-- --------------------** my integrated code ------------------------ +-- ------------------ I initially over-engineered it. I just needed to find the "is null" returned dates +UPDATE + Loans +SET + ReturnedDate = '2022-07-05' +FROM + Loans l +WHERE + ((SELECT BookID FROM Books WHERE Barcode = 6435968624) AND l.ReturnedDate IS NULL) + OR ((SELECT BookID FROM Books WHERE Barcode = 5677520613) AND l.ReturnedDate IS NULL) + OR ((SELECT BookID FROM Books WHERE Barcode = 8730298424) AND l.ReturnedDate IS NULL) +; + diff --git a/02_library/challenges/02_06_visitreminder_ch.sql b/02_library/challenges/02_06_visitreminder_ch.sql index 867d15d4..a74cf9b2 100644 --- a/02_library/challenges/02_06_visitreminder_ch.sql +++ b/02_library/challenges/02_06_visitreminder_ch.sql @@ -1,2 +1,21 @@ -- Prepare a report of the library patrons --- who have checked out the fewest books. \ No newline at end of file +-- who have checked out the fewest books. + +-- ------------ This time, the dead-simple code did it on the first go ------------- +SELECT + p.FirstName||' ' ||p.LastName as [Patron Name], + p.Email, + l.PatronID, + Count(l.LoanID) as [Number of Loans] +FROM + Loans l +INNER JOIN + Patrons p +ON + p.PatronID = l.PatronID +GROUP BY + l.PatronID +ORDER BY + [Number of Loans] ASC +LIMIT + 20; diff --git a/02_library/challenges/02_07_featurebooks_ch.sql b/02_library/challenges/02_07_featurebooks_ch.sql index 541d39a4..7b68064a 100644 --- a/02_library/challenges/02_07_featurebooks_ch.sql +++ b/02_library/challenges/02_07_featurebooks_ch.sql @@ -1,4 +1,37 @@ -- Create a list of books to feature in an exhibit. -- Make a pick list of books published from 1890-1899 --- which are not currently checked out. \ No newline at end of file +-- which are not currently checked out. + +SELECT + BookID, + Title, + Author, + Published +FROM + Books +WHERE + Published BETWEEN 1890 AND 1899 +ORDER BY + Title ASC +-- -------------------------- +SELECT BookID FROM Books WHERE Published BETWEEN 1890 AND 1899 +-- -------------------------- +SELECT + l.BookID, + b.Title, + b.Author, + b.Published +FROM + Loans l +INNER JOIN + Books b +ON + l.BookID = b.BookID +WHERE + l.BookID in (SELECT BookID FROM Books WHERE Published BETWEEN 1890 AND 1899) AND + l.ReturnedDate NOT NULL +GROUP BY + Title +ORDER BY + Title ASC; \ No newline at end of file diff --git a/02_library/challenges/02_08_bookstats_ch.sql b/02_library/challenges/02_08_bookstats_ch.sql index 2e473f5d..cbbc60fe 100644 --- a/02_library/challenges/02_08_bookstats_ch.sql +++ b/02_library/challenges/02_08_bookstats_ch.sql @@ -4,4 +4,67 @@ -- in each year. -- Report 2: Show the five books that have been --- checked out the most. \ No newline at end of file +-- checked out the most. + +-- # of books published in each year: +SELECT + Count(DISTINCT Title) as [Number of Books], + Published +FROM + Books +--WHERE +-- (SELECT DISTINCT Published FROM Books) +GROUP BY + Published +Order BY + [Number of Books] DESC; + +-- top five most-checked out books +-- This ended up only accounting for individual copies, not multiple copies of same book. See "integrated code" for total answer +SELECT + Count(l.BookID) as [How Many Times], + l.BookID, + b.Title, + b.Author +FROM + Loans l +INNER JOIN + Books b +ON + l.BookID = b.BookID +GROUP BY + l.BookID +ORDER BY + [How Many Times] DESC +LIMIT 5; + +-- --- Top 5 checked out books -- My integrated code wh/accounts for multiple copies ---- +SELECT + b.Title, + b.Author, + COUNT (DISTINCT l.LoanID) AS [Total Checkouts] +FROM + Loans l +INNER JOIN + Books b +ON + l.BookID = b.BookID +--WHERE -- *** I was breaking my head to get this part, but in the end, we did not need it. +-- b.Title IN (Select DISTINCT(Title) FROM Books) +GROUP BY + b.Title +ORDER BY + [Total Checkouts] DESC +LIMIT 5 +; + +-- Simple code : This gets all the book IDs --- +SELECT +BookID +--Title +FROM +Books +WHERE +Title IN (Select DISTINCT(Title) FROM Books) +ORDER BY +Title \ No newline at end of file diff --git a/02_library/library.db b/02_library/library.db index 8af83972..5cf4fb3a 100644 Binary files a/02_library/library.db and b/02_library/library.db differ