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..4750d90 --- /dev/null +++ b/part2.sql @@ -0,0 +1,48 @@ +--* 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(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 new file mode 100644 index 0000000..d6928fb --- /dev/null +++ b/part3.sql @@ -0,0 +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 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 new file mode 100644 index 0000000..c2d5b34 --- /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', COUNT(od.quantityOrdered) '# Sold' +FROM products p INNER JOIN orderdetails od +ON p.productCode=od.productCode +GROUP BY productLine +ORDER BY COUNT(od.quantityOrdered) DESC; + \ No newline at end of file diff --git a/part5.sql b/part5.sql new file mode 100644 index 0000000..1cabf2d --- /dev/null +++ b/part5.sql @@ -0,0 +1,17 @@ +--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`. + +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 new file mode 100644 index 0000000..e8e8528 --- /dev/null +++ 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