Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I just posted the same question. Please respond to this again now that you have added the information. I will also be running this code

I just posted the same question. Please respond to this again now that you have added the information. I will also be running this code in a Python environment. Please reflect this.
Below is the code I ran to use sqlite.
!pip install sqlalchemy==1.3.23
import sqlite3
!pip install ipython-sql
%load_ext sql
from google.colab import drive
drive.mount('/content/drive')
%sql sqlite:////content/drive/MyDrive/northwind.sqlite3
%%sql
SELECT sqlite_version();the code I ran to use sqlite.
Finally, I'm including a modification to my previous code. Please check if this code is correct.
QI. Write a query to retrieve the top 10order amounts from customers, the product information for those orders, and calculate the sum of the order amounts.
import sqlite3
import pandas as pd
conn = sqlite3.connect('/content/drive/MyDrive/northwind.sqlite3') # 'your_database.db'.
query ='''
SELECT
o.OrderID,
c.CompanyName,
SUM(od.Quantity * od.UnitPrice) AS OrderAmount,
p.ProductName
FROM
Orders o
INNER JOIN
OrderDetails od ON o.OrderID = od.OrderID
INNER JOIN
Customers c ON o.CustomerID = c.CustomerID
INNER JOIN
Products p ON od.ProductID = p.ProductID
GROUP BY
o.OrderID, c.CompanyName, p.ProductName
ORDER BY
OrderAmount DESC
LIMIT 10;
'''
df = pd.read_sql_query(query, conn)
df
Q2.Write a SQL query to find the category in which a customer ordered the most and calculate the total of that category.
import sqlite3
import pandas as pd
conn = sqlite3.connect('/content/drive/MyDrive/northwind.sqlite3') # 'your_database.db'.
query1='''
SELECT c.CompanyName,
c.Country,
cat.CategoryName,
SUM(od.Quantity * od.UnitPrice) AS CategoryTotal
FROM Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID
INNER JOIN OrderDetails od ON o.OrderID = od.OrderID
INNER JOIN Products p ON od.ProductID = p.ProductID
INNER JOIN Categories cat ON p.CategoryID = cat.CategoryID
GROUP BY c.CompanyName, c.Country, cat.CategoryName
ORDER BY CategoryTotal DESC
'''
df1= pd.read_sql_query(query1, conn)
df1
Q3.Write an SQL query to get a list of orders placed by customers in Seattle and calculate the total amount of all orders.
import sqlite3
import pandas as pd
conn = sqlite3.connect('/content/drive/MyDrive/northwind.sqlite3') # 'your_database.db'.
query2='''
SELECT o.OrderID,
c.CompanyName,
SUM(od.Quantity * od.UnitPrice) AS TotalAmount
FROM Orders o
INNER JOIN OrderDetails od ON o.OrderID = od.OrderID
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
WHERE c.City = 'Seattle'
GROUP BY o.OrderID, c.CompanyName
ORDER BY TotalAmount DESC
'''
df2= pd.read_sql_query(query2, conn)
df2
Q4.Write a SQL query that lists the names of the vendor companies that provided the products and sorts them in order of total revenue if the revenue generated by the products taken by employees with the job title "Sales Representative" is more than $10,000.What is the total of all vendor sales?
import sqlite3
import pandas as pd
conn = sqlite3.connect('/content/drive/MyDrive/northwind.sqlite3') # 'your_database.db'.
query3='''
SELECT
s.SupplierID,
s.CompanyName,
SUM(od.Quantity * od.UnitPrice) AS Revenue
FROM
Suppliers s
INNER JOIN
Products p ON s.SupplierID = p.SupplierID
INNER JOIN
OrderDetails od ON p.ProductID = od.ProductID
INNER JOIN
Orders o ON od.OrderID = o.OrderID
INNER JOIN
Employees e ON o.EmployeeID = e.EmployeeID
WHERE
e.Title = 'Sales Representative' -- Corrected column name for job title
GROUP BY
s.SupplierID, s.CompanyName
ORDER BY
Revenue DESC;
'''
df3= pd.read_sql_query(query3, conn)
print("Revenue by Supplier:")
print(df3)
query4='''
SELECT
SUM(od.Quantity * od.UnitPrice) AS TotalVendorSales
FROM
OrderDetails od
INNER JOIN
Products p ON od.ProductID = p.ProductID
INNER JOIN
Suppliers s ON p.SupplierID = s.SupplierID;
'''
df4= pd.read_sql_query(query4, conn)
print("
Total Vendor Sales:")
print(df4)
Q5.The owner of Northwind Traders wants to know if his staff is deployed effectively. For example, he might assume that there are more employees in areas where there are more orders for goods, or that there are more employees in areas with high sales. You need to analyze the staffing status so that he can make a decision. You analyze the data in SQL and use the quantitative evidence from your analysis to form an opinion about the staffing.
import sqlite3
import pandas as pd
query5='''
SELECT Region, COUNT(*) AS NumOrders
FROM Customers
GROUP BY Region
ORDER BY NumOrders DESC;
'''
df5= pd.read_sql_query(query5, conn)
print(":")
print(df5)
query6='''
SELECT Region, COUNT(*) AS NumEmployees
FROM Employees
GROUP BY Region
ORDER BY NumEmployees DESC;
'''
df6= pd.read_sql_query(query6, conn)
print(":")
print(df6)
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

Students also viewed these Databases questions

Question

* -0.186 0.892 0.423 * * * *

Answered: 1 week ago

Question

Question 1 (a2) What is the reaction force Dx in [N]?

Answered: 1 week ago