If you have not carefully reviewed the 393_practice2_instructions.qmd document, do that before beginning this practice
#Same setup packages as last week.library(tidyverse)library(DBI)library(RSQLite)# Connect to the databasemydb <-dbConnect(RSQLite::SQLite(), "northwind.db")# Remember that you can view the database schema by clicking on the northwind_schema.png file in your files list on the lower-right side of the screen.#Use this command to view tables in a databasedbListTables(mydb)
-- Write a query that joins the 'products' and 'categories' tables. -- Return the name of each category and the names of products-- belonging to that category.-- remove the "--" to uncomment line 35 and save your data as myquery when done.SELECT Categories.CategoryName, Products.ProductNameFROM ProductsINNERJOIN CategoriesON Products.CategoryID = Categories.CategoryIDLIMIT10
-- Join the 'customers' and 'orders' tables to pull customer LastNames and OrderIDs. SELECT customers.CompanyName, orders.FreightFROM customersINNERJOIN ordersON customers.CustomerID = orders.CustomerIDWHERE customers.CompanyName LIKE"%Bottom-Dollar Markets%"OR customers.CompanyName LIKE"%Around the Horn%"LIMIT10
myquery2
CompanyName Freight
1 Around the Horn 22.50
2 Around the Horn 23.75
3 Bottom-Dollar Markets 30.25
4 Bottom-Dollar Markets 26.25
5 Bottom-Dollar Markets 28.50
6 Bottom-Dollar Markets 42.50
7 Around the Horn 20.00
8 Bottom-Dollar Markets 30.00
9 Around the Horn 34.00
10 Around the Horn 32.25
ggplot(data = myquery2,aes(x = Freight, y = CompanyName)) +geom_boxplot()
--Join the Suppliers and Products tables so that you can select --both the Country for each Supplier and the names of the Products they supply.SELECT Suppliers.Country, Products.ProductName, Products.DiscontinuedFROM SuppliersINNERJOIN ProductsON Suppliers.SupplierID = Products.SupplierIDLIMIT10
myquery3
Country ProductName Discontinued
1 UK Chai 0
2 UK Chang 0
3 UK Aniseed Syrup 0
4 USA Chef Anton's Cajun Seasoning 0
5 USA Chef Anton's Gumbo Mix 1
6 USA Grandma's Boysenberry Spread 0
7 USA Uncle Bob's Organic Dried Pears 0
8 USA Northwoods Cranberry Sauce 0
9 Japan Mishi Kobe Niku 1
10 Japan Ikura 0
ggplot(data = myquery3,aes(y = Country, fill = Discontinued)) +geom_bar()
-- Use this block to pull the data you need to create boxplots for each product category that show the "revenue" attributable to that product category, but only for orders with revenue less than $1000. You can create revenue using an alias, for example "SELECT (UnitPrice * Quantity) AS revenue". SELECT Categories.CategoryName, ("Order Details".UnitPrice *"Order Details".Quantity) AS revenueFROM"Order Details"INNERJOIN ProductsON"Order Details".ProductID = Products.ProductIDINNERJOIN CategoriesON Products.CategoryID = Categories.CategoryIDWHERE revenue <1000LIMIT10
ggplot(data = myquery4,aes(x = revenue, y = CategoryName)) +geom_boxplot()
-- We have had a product recall! -- Create a query that shows the contact name and phone number for customers whose orders shipped in December 2024.. SELECT customers.ContactName, customers.PhoneFROM customersINNERJOIN ordersON customers.CustomerID = orders.CustomerIDWHERE orders.ShippedDate LIKE"2024-12%"LIMIT10
Displaying records 1 - 10
ContactName
Phone
Rene Phillips
(907) 555-7584
Eduardo Saavedra
(93) 203 4560
Marie Bertrand
(1) 42.34.22.66
Mario Pontes
(21) 555-0091
Elizabeth Lincoln
(604) 555-4729
Lúcia Carvalho
(11) 555-1189
Guillermo Fernández
(5) 552-3745
Miguel Angel Paolino
(5) 555-2933
Sven Ottlieb
0241-039123
Thomas Hardy
(171) 555-7788
-- More information has been provided about the recall. -- Edit your call list to only include customers who purchased "Boston Crab Meat" in that same time frame. SELECTDISTINCT customers.ContactName, customers.PhoneFROM customersINNERJOIN ordersON customers.CustomerID = orders.CustomerIDINNERJOIN"Order Details"ON orders.OrderID ="Order Details".OrderIDINNERJOIN productsON"Order Details".ProductID = products.ProductIDWHERE orders.ShippedDate LIKE"2024-12%"AND products.ProductName ="Boston Crab Meat"LIMIT10
Displaying records 1 - 10
ContactName
Phone
Rene Phillips
(907) 555-7584
Eduardo Saavedra
(93) 203 4560
Guillermo Fernández
(5) 552-3745
Sven Ottlieb
0241-039123
Sergio Gutiérrez
(1) 123-5555
Palle Ibsen
86 21 32 43
Renate Messner
069-0245984
Miguel Angel Paolino
(5) 555-2933
Yvonne Moncada
(1) 135-5333
Laurence Lebihan
91.24.45.40
More Practice
Now write some custom queries of your own using an INNER jOIN statement and graphing some output in some manner with ggplot.