Homework2 solution

Author

Dr. Bartosiak

Coding and Database Managment

SQL - Homework 2 - solution

The following queries use the classicmodels schema.

Source: https://www.richardtwatson.com/

Source: https://www.richardtwatson.com/
  1. How many unique products are in the table products?
SELECT COUNT(DISTINCT(productCode)) FROM products;
  1. What is the total number of products and the total quantity of all products?
SELECT COUNT(productCode) AS TotalP, SUM(quantityInStock) AS TotalQ
FROM products;
  
  1. What are the minimum and maximum prices of products?
SELECT MIN(buyPrice)AS minprice, MAX(buyPrice) AS maxprice FROM products;

    
  1. Provide the number of products by product line (hint: use group by)
SELECT COUNT(productCode) AS NumberOfProducts, productline 
FROM products 
GROUP BY productline;
    
  1. Report the sales representative for each customer.
SELECT customers.customerName, employees.lastName,employees.firstName
FROM customers, employees 
WHERE customers.salesRepEmployeeNumber = employees.employeeNumber;
  1. Report the total payments by date.
SELECT SUM(payments.amount) AS TotalP, paymentDate 
FROM payments 
GROUP BY paymentDate;
    
  1. Report the products that have not been sold.
SELECT  productName FROM products WHERE
productCode NOT IN
 (SELECT DISTINCT(productCode) FROM orderdetails);
    
  1. List the amount paid by each customer.
SELECT customers.customerName, SUM(payments.amount) AS Total 
FROM customers, payments
WHERE customers.customerNumber = payments.customerNumber
GROUP BY customers.customerNumber;
    
  1. How many orders have been placed by Herkku Gifts?
SELECT COUNT(orderNumber) FROM orders WHERE customerNumber IN (
SELECT customerNumber FROM customers WHERE customerName = 'Herkku Gifts');
    
  1. Who are the employees in Boston?
SELECT employees.firstName, employees.lastName FROM employees WHERE officeCode IN(
SELECT officeCode FROM offices WHERE city = 'Boston');
  1. Report those payments greater than $100,000. Group the report by customer name. Sort the report so the customer who made the highest payment appears first.
SELECT customers.customerName, SUM(payments.amount) AS Total
FROM customers, payments 
WHERE customers.customerNumber = payments.customerNumber
AND payments.amount > 100000
GROUP BY customerName
ORDER BY Total DESC;
© 2025. All Rights Reserved.
Enabled by Dr. Marcin Bartosiak