From 907f4436cb75cb48c8f76754441926aa5cec9c47 Mon Sep 17 00:00:00 2001 From: mondira-roy2020 Date: Mon, 13 Jul 2020 13:19:38 -0400 Subject: [PATCH 1/3] Part1 complete --- part1.sql | 12 ++++++++++++ part2.sql | 39 +++++++++++++++++++++++++++++++++++++++ part3.sql | 0 3 files changed, 51 insertions(+) create mode 100644 part1.sql create mode 100644 part2.sql create mode 100644 part3.sql diff --git a/part1.sql b/part1.sql new file mode 100644 index 0000000..2141165 --- /dev/null +++ b/part1.sql @@ -0,0 +1,12 @@ +--* Write a query to display each customer’s name (as`Customer Name`) +--alongside the name of the employee who is responsible for that +--customer’s orders.The employee name should be in a single `Sales Rep` +--column formatted as `lastName, firstName`. The output should be sorted alphabetically by customer name. + +USE mysqlsampledatabase; + +-- For space between the contact Last Name and contact First Name use ' ' +SELECT customerName AS 'Customer Name', +CONCAT(contactLastName,' ',contactFirstName) AS 'Sales Rep' +FROM customers +ORDER BY customerName; \ No newline at end of file diff --git a/part2.sql b/part2.sql new file mode 100644 index 0000000..ce291cf --- /dev/null +++ b/part2.sql @@ -0,0 +1,39 @@ +--* Determine which products are most popular with our customers. +--// Assuming that the most popular product is the most ordered +USE mysqlsampledatabase; + +SELECT productCode, SUM(quantityOrdered) AS 'Popular Product' from orderdetails +GROUP BY productCode +ORDER BY SUM(quantityOrdered) DESC; + +-- Bringing in the customer angle from the customerNumber in Orders table +--Getting customerNumber instead of Customer Name and productCode intsead +--of Product Name using this query. +SELECT O.customernumber, OD.productCode, SUM(OD.quantityOrdered) Popular +FROM orderdetails OD INNER JOIN orders O +ON O.ordernumber = OD.ordernumber +GROUP BY O.customernumber, OD.productCode +ORDER BY Popular Desc; + +--Finally using a subquery, I could get the desired popular product +-- Names along with the Customer Names. + +Select C.customerName 'Customer Names', P.productName 'Product Names', Popularproduct.Popular +from customers C, products P, +( SELECT O.customernumber , OD.productCode, SUM(OD.quantityOrdered) Popular +FROM orderdetails OD INNER JOIN orders O +ON O.ordernumber = OD.ordernumber +GROUP BY O.customernumber, OD.productCode +ORDER BY Popular Desc ) Popularproduct +where C.customerNumber = Popularproduct.customernumber +AND P.productCode = Popularproduct.productCode +GROUP BY customerName; --Gives the most popular product for each customer + +--* For each product, list the total quantity ordered along with the total +--sale generated (total quantity ordered * priceEach) for that product. + +SELECT p.productName 'Product Name', sum(od.quantityOrdered) 'Total # Ordered', + sum() + +--* The column headers should be `Product Name`, `Total # Ordered` and +--`Total Sale`. List the products by `Total Sale` descending. diff --git a/part3.sql b/part3.sql new file mode 100644 index 0000000..e69de29 From c06d776712be5985479505ac102edfa0a2a9ae37 Mon Sep 17 00:00:00 2001 From: mondira-roy2020 Date: Mon, 13 Jul 2020 15:03:26 -0400 Subject: [PATCH 2/3] Part4 complete --- part2.sql | 11 ++++++++++- part3.sql | 6 ++++++ part4.sql | 11 +++++++++++ part5.sql | 7 +++++++ part6.sql | 0 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 part4.sql create mode 100644 part5.sql create mode 100644 part6.sql diff --git a/part2.sql b/part2.sql index ce291cf..4750d90 100644 --- a/part2.sql +++ b/part2.sql @@ -33,7 +33,16 @@ GROUP BY customerName; --Gives the most popular product for each customer --sale generated (total quantity ordered * priceEach) for that product. SELECT p.productName 'Product Name', sum(od.quantityOrdered) 'Total # Ordered', - sum() + sum(quantityOrdered*priceEach)'Total Sale' + FROM products p JOIN orderdetails od + ON p.productCode=od.productCode + GROUP BY productName; --* The column headers should be `Product Name`, `Total # Ordered` and --`Total Sale`. List the products by `Total Sale` descending. +SELECT p.productName 'Product Name', sum(od.quantityOrdered) 'Total # Ordered', + sum(quantityOrdered*priceEach)'Total Sale' + FROM products p JOIN orderdetails od + ON p.productCode=od.productCode + GROUP BY productName + ORDER BY 'Total Sale' DESC; \ No newline at end of file diff --git a/part3.sql b/part3.sql index e69de29..00de2af 100644 --- a/part3.sql +++ b/part3.sql @@ -0,0 +1,6 @@ +--A query which lists order status and the # of orders with that status. +--* Column headers should be `Order Status` and `# Orders`. +--* Sort alphabetically by `status`. +select status 'Order Status', sum(orderNumber) '# Orders' from orders +group by status +order By status ; diff --git a/part4.sql b/part4.sql new file mode 100644 index 0000000..52731bb --- /dev/null +++ b/part4.sql @@ -0,0 +1,11 @@ +--Write a query to list, for each product line, the total # of products +--sold from that product line. +--* The first column should be `Product Line` and the second should be +--`# Sold`.* Order by the second column descending. + +SELECT p.productline 'Product Line', SUM(od.quantityOrdered) '# Sold' +FROM products p INNER JOIN orderdetails od +ON p.productCode=od.productCode +GROUP BY productLine +ORDER BY sum(od.quantityOrdered) DESC; + \ No newline at end of file diff --git a/part5.sql b/part5.sql new file mode 100644 index 0000000..b793d99 --- /dev/null +++ b/part5.sql @@ -0,0 +1,7 @@ +--For each employee who represents customers, output the total # of orders +--that employee’s customers have placed alongside the total sale amount of those orders. +--* The employee name should be output as a single column named `Sales Rep` formatted + --as `lastName, firstName`. +--* The second column should be titled `# Orders` and the third should be `Total Sales`. +--* Sort the output by `Total Sales` descending. +--* Only (and all) employees with the job title `Sales Rep` should be included in the output, and if the employee made no sales the `Total Sales` should display as `0.00`. \ No newline at end of file diff --git a/part6.sql b/part6.sql new file mode 100644 index 0000000..e69de29 From b4f9ef6a2b061f8561cc9dd9a25b6126ef604015 Mon Sep 17 00:00:00 2001 From: mondira-roy2020 Date: Mon, 13 Jul 2020 15:33:57 -0400 Subject: [PATCH 3/3] Assignment complete --- part3.sql | 7 ++++--- part4.sql | 4 ++-- part5.sql | 12 +++++++++++- part6.sql | 12 ++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/part3.sql b/part3.sql index 00de2af..d6928fb 100644 --- a/part3.sql +++ b/part3.sql @@ -1,6 +1,7 @@ --A query which lists order status and the # of orders with that status. --* Column headers should be `Order Status` and `# Orders`. --* Sort alphabetically by `status`. -select status 'Order Status', sum(orderNumber) '# Orders' from orders -group by status -order By status ; +SELECT o.status 'Order Status', COUNT(orderNumber) '# Orders' +FROM orders o +GROUP BY status; +ORDER BY status; --sorting alphabetically \ No newline at end of file diff --git a/part4.sql b/part4.sql index 52731bb..c2d5b34 100644 --- a/part4.sql +++ b/part4.sql @@ -3,9 +3,9 @@ --* The first column should be `Product Line` and the second should be --`# Sold`.* Order by the second column descending. -SELECT p.productline 'Product Line', SUM(od.quantityOrdered) '# Sold' +SELECT p.productline 'Product Line', COUNT(od.quantityOrdered) '# Sold' FROM products p INNER JOIN orderdetails od ON p.productCode=od.productCode GROUP BY productLine -ORDER BY sum(od.quantityOrdered) DESC; +ORDER BY COUNT(od.quantityOrdered) DESC; \ No newline at end of file diff --git a/part5.sql b/part5.sql index b793d99..1cabf2d 100644 --- a/part5.sql +++ b/part5.sql @@ -4,4 +4,14 @@ --as `lastName, firstName`. --* The second column should be titled `# Orders` and the third should be `Total Sales`. --* Sort the output by `Total Sales` descending. ---* Only (and all) employees with the job title `Sales Rep` should be included in the output, and if the employee made no sales the `Total Sales` should display as `0.00`. \ No newline at end of file +--* Only (and all) employees with the job title `Sales Rep` should be included in the output, +--and if the employee made no sales the `Total Sales` should display as `0.00`. + +SELECT concat(lastName,' ',firstName) AS 'Sales Rep', COUNT(quantityOrdered) +AS 'Total # Ordered', COUNT(quantityOrdered)*SUM(priceEach) AS 'Total Sales' +FROM employees e,customers c,orderdetails od, orders o +WHERE c.salesRepEmployeeNumber = e.employeeNumber +AND o.orderNumber = od.orderNumber +AND e.jobTitle = 'Sales Rep' +GROUP BY employeeNumber +ORDER BY COUNT(quantityOrdered)*SUM(priceEach) DESC; \ No newline at end of file diff --git a/part6.sql b/part6.sql index e69de29..e8e8528 100644 --- a/part6.sql +++ b/part6.sql @@ -0,0 +1,12 @@ +--Write a query to output the month (January, February, etc.), 4-digit year, and total + --sales for that month. +--* The first column should be labeled `Month`, the second `Year`, and the third +-- should be `Payments Received`. +--* Values in the third column should be formatted as numbers with two decimals – +--for example: `694,292.68`. + +SELECT MONTH(paymentDate) AS Month, YEAR(paymentDate) AS Year, SUM(amount) AS +'Payments Received' +FROM payments +GROUP by Month +ORDER BY Year, Month; \ No newline at end of file