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
12 changes: 12 additions & 0 deletions part1.sql
Original file line number Diff line number Diff line change
@@ -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;
48 changes: 48 additions & 0 deletions part2.sql
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions part3.sql
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions part4.sql
Original file line number Diff line number Diff line change
@@ -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;

17 changes: 17 additions & 0 deletions part5.sql
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions part6.sql
Original file line number Diff line number Diff line change
@@ -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;