Lab 4 Lab SQL Northwind Sol
Lab 4 Lab SQL Northwind Sol
2) Select customers who bought beverages from Andrew Fuller and received a discount.
SELECT distinct c.customerid
FROM customers c, orders o, orderdetails od, products p, categories ct,
employees e
where c.customerid = o.customerid
and o.orderid=od.orderid
and od.productid = p.productid
and ct.categoryid = p.categoryid
and ct.categoryname="Beverages"
and e.employeeid = o.employeeid
and e.lastName="Fuller"
and e.firstName="Andrew"
and od.discount>0
3) Print customer names who bought orders shipped to London via Federal Shipping
select distinct o.customerid
from orders o, shippers s
where o.shipvia=s.shipperid
and s.companyName = "Federal Shipping"
and o.shipcity="London"
6)
SELECT DISTINCT ProductName, UnitPrice
FROM Products
WHERE UnitPrice > (SELECT avg(UnitPrice) FROM Products)
ORDER BY UnitPrice;
7) Create a report showing the first and last names of all employees whose region is unspecified.
SELECT FirstName, LastName
FROM Employees
WHERE Region IS NULL;
10) For each customer, print the total number of products he bought.
select count(distinct od.productid), o.customerid
from orders o, orderdetails od
where o.orderid=od.orderid
group by o.customerid
order by 1
11) Print the names of suppliers who supply more than 3 products.
12) Print the customer names who bought more than 45 products.
select count(distinct od.productid), o.customerid
from orders o, orderdetails od
where o.orderid=od.orderid
group by o.customerid
having count(distinct od.productid)>40
order by 1
13) Find the total amount of money each customer spent.
select sum(od.unitprice*od.quantity), o.customerid from orderdetails od, orders o
where o.orderid=od.orderid
group by o.customerid;
14) What is the average price in each category? (sort from highest to lowest)?
16) What is the average price of the products each customer has ordered? Sort high to low.
17) What is the total amount purchased by each customer? (List in descending order)