diff --git a/sqlJoinsPart1.sql b/sqlJoinsPart1.sql new file mode 100644 index 0000000..747966b --- /dev/null +++ b/sqlJoinsPart1.sql @@ -0,0 +1,10 @@ +/*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 c.customerName AS "Customer Name", CONCAT (e.lastName, ' ', e.firstname) AS "Sales Rep" +FROM customers c INNER JOIN employees e +ON c.salesRepEmployeeNumber = e.employeeNumber +ORDER BY c.customerName ASC; diff --git a/sqlJoinsPart2.sql b/sqlJoinsPart2.sql new file mode 100644 index 0000000..e5590b0 --- /dev/null +++ b/sqlJoinsPart2.sql @@ -0,0 +1,33 @@ +/*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 p.productName AS "Product Name", SUM(od.quantityOrdered) AS "Total # Ordered", (SUM(od.quantityOrdered)*od.priceEach) AS "Total Sale" +FROM products p +JOIN orderdetails od +ON p.productCode = od.productCode +GROUP BY p.productName +ORDER BY SUM(od.quantityOrdered)*od.priceEach DESC; + +/*SELECT p.productName AS "Product Name", COUNT(p.productCode) AS "Total # Ordered", (COUNT(p.productCode) * od.priceEach) AS "Total Sale" +FROM customers c, orders o, orderdetails od, products p +WHERE c.customerNumber = o.customerNumber +AND o.orderNumber = od.orderNumber +AND od.productCode = p.productCode +GROUP BY p.productCode +ORDER BY "Total Sale" DESC;*/ +/*SELECT p.productName AS "Product Name", SUM(od.quantityOrdered) AS "Total # Ordered", (SUM(od.quantityOrdered)*od.priceEach) AS "Total Sale" +FROM products p +JOIN orderdetails od +ON p.productCode = od.productCode +GROUP BY p.productCode +ORDER BY "ToTal Sale" DESC;*/ +/*SELECT productcode, quantityOrdered FROM orderdetails +GROUP BY productCode +HAVING productCode = "S10_1678";*/ +/*SELECT productcode, sum(quantityOrdered) FROM orderdetails +WHERE productcode = "S10_1678";*/ + diff --git a/sqlJoinsPart3.sql b/sqlJoinsPart3.sql new file mode 100644 index 0000000..0e61d1c --- /dev/null +++ b/sqlJoinsPart3.sql @@ -0,0 +1,8 @@ +/*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.*/ +SELECT STATUS AS "Order Status", COUNT(ordernumber) AS "# of orders" +FROM orders +GROUP BY STATUS +ORDER BY STATUS ASC; \ No newline at end of file diff --git a/sqlJoinsPart4.sql b/sqlJoinsPart4.sql new file mode 100644 index 0000000..bf84d97 --- /dev/null +++ b/sqlJoinsPart4.sql @@ -0,0 +1,8 @@ +/*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.*/ +SELECT productline AS "Product Line", COUNT(productcode) AS "# Sold" +FROM products +GROUP BY productline +ORDER BY COUNT(productcode) DESC; \ No newline at end of file diff --git a/sqlJoinsPart6.sql b/sqlJoinsPart6.sql new file mode 100644 index 0000000..a792674 --- /dev/null +++ b/sqlJoinsPart6.sql @@ -0,0 +1,8 @@ +/*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.*/ +SELECT EXTRACT(MONTH FROM paymentDate) AS Month, EXTRACT(YEAR FROM paymentDate) Year, FORMAT(SUM(amount), 2) AS 'Payments Received' +FROM payments +GROUP BY Month, YEAR