0% found this document useful (0 votes)
13 views4 pages

Comp1168 Lab 7-1

The document contains SQL queries for a lab assignment focused on analyzing vendor payments, customer orders, and employee counts in various departments. It includes queries to calculate totals, averages, and group data by vendor and state, as well as to join tables for comprehensive reporting. Additionally, it demonstrates the use of the ROLL UP operator to obtain grand totals for employee counts by department.

Uploaded by

Anuj Choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Comp1168 Lab 7-1

The document contains SQL queries for a lab assignment focused on analyzing vendor payments, customer orders, and employee counts in various departments. It includes queries to calculate totals, averages, and group data by vendor and state, as well as to join tables for comprehensive reporting. Additionally, it demonstrates the use of the ROLL UP operator to obtain grand totals for employee counts by department.

Uploaded by

Anuj Choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Comp1168 Lab 7

Name: Yash
Anuj Zinzuvadiya
Moond

CRN: 50146
Student ID: 101475983
101544121

Date: 03-16-2025

Question 1: Create a query that returns the total number of


payments (payment_total) made by the vendors, the total
amount paid, average, minimum and maximum amount that
was paid by a vendor for their invoices (exclude zero
payments).

Query:
SELECT
COUNT(*) AS No_of_Payments,
SUM(payment_total) AS Total_amount_paid,
AVG(payment_total) AS Average_Amount,
MIN(payment_total) AS Minmum_payment,
MAX(payment_total) AS Maximum_payment
FROM ap.invoices
WHERE payment_total > 0;

Question 1a: Let us


do it per Vendor
Query:
SELECT
vendor_id,
COUNT(*) AS Total_No_of_Payments,
SUM(payment_total) AS Total_amount_paid,
AVG(payment_total) AS Average_Amount,
MIN(payment_total) AS Minmum_payment,
MAX(payment_total) AS Maximum_payment
FROM ap.invoices
WHERE payment_total > 0 GROUP BY vendor_id;

Question 1b: Let us display vendors’ names also (Join


Vendors and Invoices tables) Query:
SELECT
v.vendor_id,
v.vendor_name,
COUNT(i.payment_total) AS payment_total,
SUM(i.payment_total) AS total_amount_paid,
AVG(i.payment_total) AS average_payment,
MIN(i.payment_total) AS min_payment,
MAX(i.payment_total) AS max_payment
FROM ap.vendors v
JOIN ap.invoices i ON v.vendor_id = i.vendor_id
WHERE i.payment_total > 0
GROUP BY v.vendor_id, v.vendor_name ORDER BY v.vendor_id ASC;
Question 2: Create a query that returns the total number of vendors
located in each state (Hint: use Distinct) and the total amount of payments
made by them [use ap] Query:
SELECT
vendor_state,
COUNT(DISTINCT V.vendor_id) AS Total_vendors,
SUM(i.payment_total) AS Total_payments
FROM ap.vendors v
JOIN ap.invoices i ON v.vendor_id = i.vendor_id
GROUP BY vendor_state;
Question 2a: Now refine the previous query to add city and
Zipcode (3 levels of grouping) **** count(Distinct((Vendor_id))

Query:
SELECT
vendor_state,
vendor_city,
vendor_zip_code,
COUNT(DISTINCT v.vendor_id) AS Total_vendors,
SUM(i.payment_total) AS Total_payments
FROM ap.vendors v
JOIN ap.invoices i ON v.vendor_id = i.vendor_id
GROUP BY vendor_state,vendor_city,vendor_zip_code ORDER BY
vendor_state ASC;

Question 3: Create a query that returns the Customers’ full names, and
total number of items ordered by them for each order date and the total
invoiced amount for all orders date-wise for those days only when the
company processed more than 1 order.[use om]

Query:

SELECT
CONCAT(c.customer_last_name, ',',c.customer_first_name) AS
Customer_name, o.order_date,
COUNT(od.order_qty) AS Total_items_ordered,
SUM(i.unit_price) AS Total_invoiced_amount
FROM om.customers c
JOIN om.orders o ON c.customer_id = o.customer_id
JOIN om.order_details od ON o.order_id = od.order_id
JOIN om.items i ON od.item_id = i.item_id
GROUP BY c.customer_first_name, c.customer_last_name,o.order_date
HAVING COUNT(od.order_qty)>1
ORDER BY Customer_name ASC;

Question4: Create a query that returns department names and


the total number of employees working in that department
[use ex]
Query:
SELECT
d.department_name,
COUNT(e.employee_id) AS total_employees
FROM ex.departments d
JOIN ex.employees e ON d.department_number = e.department_number
GROUP BY d.department_name
ORDER BY department_name ASC;

Question 4a: We can also find out the Grand Total for the
aggregated column by using the ROLL UP operator
Query:
SELECT
IFNULL(d.department_name, 'TOTAL=') AS Department_name,
COUNT(e.employee_id) AS Total_employees
FROM ex.departments d
JOIN ex.employees e ON d.department_number = e.department_number
GROUP BY d.department_name WITH ROLLUP
ORDER BY Total_employees;

You might also like