From f707cbafc2365e37d6b5066a43dbc85c05550d7c Mon Sep 17 00:00:00 2001 From: Steven Snyder Date: Mon, 13 Jul 2020 10:10:34 -0400 Subject: [PATCH 1/6] first exercise completed --- solution_queries.sql | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 solution_queries.sql diff --git a/solution_queries.sql b/solution_queries.sql new file mode 100644 index 0000000..23e823a --- /dev/null +++ b/solution_queries.sql @@ -0,0 +1,36 @@ +-- Part 1 +-- Write a query to display each customer’s name (asCustomer 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. + +SELECT customers.customerName, CONCAT(employees.lastName,", ", employees.firstName) AS Sales_Rep +FROM CUSTOMERS +LEFT JOIN EMPLOYEES ON CUSTOMERS.salesRepEmployeeNumber=employees.employeeNumber +ORDER BY customers.customerName ASC; + + + + +-- Part 2 +-- Determine which products are most popular with our customers. +-- For each product, list the total quantity ordered along with the total sale generated (total quantity ordered * priceEach) for that product. +-- The column headers should be Product Name, Total # Ordered and Total Sale. +-- List the products by Total Sale descending. +-- Part 3 +-- Write 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. +-- Part 4 +-- 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. +-- Part 5 +-- 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. +-- Part 6 +-- Your product team is requesting data to help them create a bar-chart of monthly sales since the company’s inception. +-- 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. \ No newline at end of file From 557c7495182576eb6accc9ecdbdf74551cac0ced Mon Sep 17 00:00:00 2001 From: Steven Snyder Date: Mon, 13 Jul 2020 10:27:37 -0400 Subject: [PATCH 2/6] Part Two completed --- solution_queries.sql | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/solution_queries.sql b/solution_queries.sql index 23e823a..ef5c20a 100644 --- a/solution_queries.sql +++ b/solution_queries.sql @@ -8,13 +8,19 @@ LEFT JOIN EMPLOYEES ON CUSTOMERS.salesRepEmployeeNumber=employees.employeeNumber ORDER BY customers.customerName ASC; - - -- Part 2 -- Determine which products are most popular with our customers. -- For each product, list the total quantity ordered along with the total sale generated (total quantity ordered * priceEach) for that product. -- The column headers should be Product Name, Total # Ordered and Total Sale. -- List the products by Total Sale descending. + +SELECT * FROM products; +SELECT * FROM payments; +SELECT * FROM orderdetails; +SELECT productName AS 'Product Name',(quantityOrdered * priceEach) AS 'Total sale' +FROM products JOIN orderdetails ON products.productcode=orderdetails.productcode +ORDER BY (quantityOrdered * priceEach) desc; + -- Part 3 -- Write a query which lists order status and the # of orders with that status. -- Column headers should be Order Status and # Orders. From 176edf2e4b858ade66b25cd68ca27f54cfabfec0 Mon Sep 17 00:00:00 2001 From: Steven Snyder Date: Mon, 13 Jul 2020 10:54:00 -0400 Subject: [PATCH 3/6] Part Three completed --- solution_queries.sql | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/solution_queries.sql b/solution_queries.sql index ef5c20a..5dd6124 100644 --- a/solution_queries.sql +++ b/solution_queries.sql @@ -25,10 +25,18 @@ ORDER BY (quantityOrdered * priceEach) desc; -- Write 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 AS "ORDER STATUS", count(*) AS "# ORDERS" FROM orders GROUP BY STATUS ORDER BY STATUS ASC; + + + + -- Part 4 -- 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. + + -- Part 5 -- 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. From d944bc38f4c132a4451dd921c887be756c3b4619 Mon Sep 17 00:00:00 2001 From: Steven Snyder Date: Mon, 13 Jul 2020 11:13:40 -0400 Subject: [PATCH 4/6] Part 4 completed --- solution_queries.sql | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/solution_queries.sql b/solution_queries.sql index 5dd6124..7243954 100644 --- a/solution_queries.sql +++ b/solution_queries.sql @@ -36,6 +36,12 @@ SELECT status AS "ORDER STATUS", count(*) AS "# ORDERS" FROM orders GROUP BY STA -- The first column should be Product Line and the second should be # Sold. -- Order by the second column descending. +SELECT products.productLine AS 'Product Line', sum(quantityOrdered) AS '#Sold' +FROM +products JOIN orderdetails ON products.productCode = orderdetails.productCode +JOIN productlines ON productlines.productLine = products.productLine +GROUP BY products.productline +ORDER BY sum(quantityOrdered) DESC; -- Part 5 -- 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. From 2413b38911d74085ac0adec00283de3bd21cb34c Mon Sep 17 00:00:00 2001 From: Steven Snyder Date: Mon, 13 Jul 2020 12:14:26 -0400 Subject: [PATCH 5/6] Part 5 Complete --- solution_queries.sql | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/solution_queries.sql b/solution_queries.sql index 7243954..0292bae 100644 --- a/solution_queries.sql +++ b/solution_queries.sql @@ -37,8 +37,8 @@ SELECT status AS "ORDER STATUS", count(*) AS "# ORDERS" FROM orders GROUP BY STA -- Order by the second column descending. SELECT products.productLine AS 'Product Line', sum(quantityOrdered) AS '#Sold' -FROM -products JOIN orderdetails ON products.productCode = orderdetails.productCode +FROM products +JOIN orderdetails ON products.productCode = orderdetails.productCode JOIN productlines ON productlines.productLine = products.productLine GROUP BY products.productline ORDER BY sum(quantityOrdered) DESC; @@ -49,6 +49,18 @@ ORDER BY sum(quantityOrdered) DESC; -- 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. + +SELECT CONCAT(employees.lastName,employees.firstName) AS 'Sales Rep', +COUNT(quantityOrdered) AS '#ORDERS', +IFNULL((quantityOrdered*priceEach), 0.00) AS 'Total Sales' +FROM employees +LEFT JOIN customers ON employees.employeeNumber = customers.salesRepEmployeeNumber +LEFT JOIN orders ON customers.customerNumber=orders.customerNumber +LEFT JOIN orderdetails ON orders.orderNumber=orderdetails.orderNumber +WHERE employees.jobTitle = 'Sales Rep' +GROUP BY CONCAT(employees.employeeNumber) ,'#ORDERS' +ORDER BY (quantityOrdered*priceEach) DESC; + -- Part 6 -- Your product team is requesting data to help them create a bar-chart of monthly sales since the company’s inception. -- Write a query to output the month (January, February, etc.), 4-digit year, and total sales for that month. From 957e5ad1b6855a9c8a5404f8049a25d4d237f022 Mon Sep 17 00:00:00 2001 From: Steven Snyder Date: Mon, 13 Jul 2020 12:26:26 -0400 Subject: [PATCH 6/6] completed part 6 --- solution_queries.sql | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/solution_queries.sql b/solution_queries.sql index 0292bae..a25366b 100644 --- a/solution_queries.sql +++ b/solution_queries.sql @@ -65,4 +65,6 @@ ORDER BY (quantityOrdered*priceEach) DESC; -- Your product team is requesting data to help them create a bar-chart of monthly sales since the company’s inception. -- 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. \ No newline at end of file +-- Values in the third column should be formatted as numbers with two decimals – for example: 694,292.68. +SELECT MONTHNAME(paymentdate) AS Month,extract(year FROM paymentdate) AS Year, sum(amount) AS 'Payments Received' +FROM payments GROUP BY 1,2 ORDER BY Year(paymentdate), MONTH(paymentdate) ASC; \ No newline at end of file