0% found this document useful (0 votes)
119 views

Lab 4 Lab SQL Northwind Sol

The document contains 17 SQL queries that: 1) Select products packaged in boxes 2) Select customers who bought beverages from Andrew Fuller and received a discount 3) Print customer names who bought orders shipped to London via Federal Shipping
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Lab 4 Lab SQL Northwind Sol

The document contains 17 SQL queries that: 1) Select products packaged in boxes 2) Select customers who bought beverages from Andrew Fuller and received a discount 3) Print customer names who bought orders shipped to London via Federal Shipping
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) Select products that come in boxes

SELECT * FROM products p


where quantityperunit like "%box%";

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"

4) Find the dollar amount of the total number of sales.


select sum(unitprice*quantity)from orderdetails;

5) What is the most expensive product?

select productname from products where unitprice = (


select max(unitprice) from products);

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;

8) Retrieve the number of employees in each city


SELECT City, COUNT(EmployeeID) AS NumEmployees
FROM Employees
GROUP BY City;
9) Get the phone numbers of all shippers, customers, and suppliers

SELECT CompanyName, Phone


FROM Shippers
UNION
SELECT CompanyName, Phone
FROM Customers
UNION
SELECT CompanyName, Phone
FROM Suppliers
ORDER BY CompanyName;

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.

SELECT count(*), supplierid


FROM products p
group by supplierid
having count(*)>3
order by 1

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)?

SELECT Categories.CategoryName, Avg(Products.UnitPrice) AS AvgOfUnitPrice


FROM Categories, Products
Where Categories.CategoryID = Products.CategoryID
GROUP BY Categories.CategoryName
ORDER BY Categories.CategoryName, Avg(Products.UnitPrice);
15) How many kinds of products each customer has ordered – sorted by product? Sort high to low.

SELECT Customers.CompanyName, Count(Products.ProductName) AS CountOfProductName


FROM Customers, Orders , Products , Order Details
Where Customers.CustomerID = Orders.CustomerID AND
Products.ProductID = Order Details.ProductID AND
Orders.OrderID = Order Details.OrderID
GROUP BY Customers.CompanyName
ORDER BY Count(Products.ProductName) DESC;

16) What is the average price of the products each customer has ordered? Sort high to low.

SELECT Customers.CompanyName, Avg(Products.UnitPrice) AS AvgOfUnitPrice


FROM Customers , Orders , Products OrderDetails
WHERE Customers.CustomerID = Orders.CustomerID AND
Products.ProductID = OrderDetails.ProductID AND
Orders.OrderID = OrderDetails.OrderID
GROUP BY Customers.CompanyName
ORDER BY Avg(Products.UnitPrice) DESC;

17) What is the total amount purchased by each customer? (List in descending order)

SELECT Customers.CompanyName, Sum(OrderDetails.UnitPrice*OrderDetails.Quantity) AS Amount


FROM Customers , Orders , Products, OrderDetails
WHERE Customers.CustomerID = Orders.CustomerID AND
Products.ProductID = OrderDetails.ProductID AND
Orders.OrderID = OrderDetails.OrderID
GROUP BY Customers.CompanyName
ORDER BY Customers.CompanyName;

You might also like