Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sqlJoinsPart1.sql
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 33 additions & 0 deletions sqlJoinsPart2.sql
Original file line number Diff line number Diff line change
@@ -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";*/

8 changes: 8 additions & 0 deletions sqlJoinsPart3.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions sqlJoinsPart4.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions sqlJoinsPart6.sql
Original file line number Diff line number Diff line change
@@ -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